How is an object stored in heap?

How exactly an object is stored in heap. For example, a bicycle class can be defined like this:

public class Bicycle {

   public int gear;
   public int speed;

   public Bicycle(int startSpeed, int startGear) {
       gear = startGear;
       speed = startSpeed;
   }

   public void setGear(int newValue) {
       gear = newValue;
   } 

   public void applyBrake(int decrement) {
       speed -= decrement;
   }

   public void speedUp(int increment) {
      speed += increment;
   }   
}

then I can create an bicycle object:

Bicycle bicycle = new Bicycle(20,10)

Then this bicycle object should be stored in heap. But I don't understand how heap exactly store these instance variables and methods such as speed and gear.I understand that heap should be implemented as tree. So how the object is stored in a tree? Also when you use bicycle.speed to find the value of speed, what will be time complexity?


Want someone else to do your CS homework eh? ;)

Depending on the language the exact implementation will be different (how it's stored in memory), but the general concept is the same.

You have your stack memory and your heap memory, the local variables and parameters go on the stack, and whenever you new something it goes into the heap. It's called a stack because values are pushed onto it when you declare it or call a function, and popped off and they go out of scope.

                    +--------------+
|              |    |              |
|              |    |              |
|              |    |              |
|              |    |              |
|              |    |              |
|              |    |              |
+--------------+    
     Stack                Heap

Each instance variable takes up however much memory its type does (depending on the language), the compiler adds it all up and that's the sizeof the type (à la C++). Methods go into code space and does not get new ed into the object (I think for now you'll be better off to not consider this in learning about how memory is organized, just think of it as magic).

So in your example:

Bicycle bicycle = new Bicycle(20,10)
  • bicycle is a reference to the heap memory address, today in most languages/systems it will cost you either 32 and 64 bits on the stack.
  • The new allocates memory in the heap. The compiler figures out the size of Bicycle and creates assembly/machine code that allocates the amount of memory it requires.
  • This is how the memory looks after this line:

                        +--------------+
    |              |    | Bicycle obj  |
    |              |    |--------------|
    |              |    |              |
    |              |    |              |
    |--------------|    |              |
    | bicycle ref  |    |              |
    +--------------+    
         Stack                Heap
    

    More specifically, since the Bicycle class has two instance variables (or fields as they are called in Java) and both are int s, and an int in Java is 32 bits or 4 bytes, the size of your Bicycle object is 4 bytes * 2 fields = 8 bytes.

                       +-------------+
    |             |   0| gear        | 
    |             |   4| speed       |
    |             |    |-------------|
    |             |   8|             |
    |-------------|  12|             |
    | bicycle=0x4 |    |             |
    +--------------+    
         Stack                Heap
    

    Time complexity to access memory is O(1). The compiler is able to figure out the exact memory address of speed , since as the second int field in the object, is at bicycle+0x4.


    Bicycle bicycle = new Bicycle(20,10)
    

    参考自行车将被存储在stack而对象和实例变量将被存储在heapheap的地址被分配到堆栈中,因此意味着堆栈将链接到堆。


    First of all, you should understand meaning of Object in terms of Java.

    Object is nothing but just a buffer(memory area) in Heap. That buffer or memory area is called Object.

    Object contains all the non-static data member of the class.

    All the-

    Object stores in Heap.

    Static data member stores in Class Area.

    Reference variable stores in Stack.

    Method (static or non-static) stores in Method Area.

    内存区域

    链接地址: http://www.djcxy.com/p/82834.html

    上一篇: 什么是动态内存?

    下一篇: 对象如何存储在堆中?