Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 776
Jude Avatar asked Mar 13 '14 18:03

Jude


People also ask

What is the use of heap memory in Java?

Heap space in Java is used for dynamic memory allocation for Java objects and JRE classes at the runtime. New objects are always created in heap space and the references to this objects are stored in stack memory. Heap memory and Stack memory both are placed at RAM.

What is the difference between stack memory and heap memory?

In stack memory, the variable of class type stores the address of an object. In heap memory, a physical copy of an object is stored and the reference or address of this copy in the variable of the associated class. Objects that can no longer be referenced are cleaned up by Garbage collector.

How are Java objects stored in memory?

- GeeksforGeeks How are Java objects stored in memory? In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In JAVA , when we allocate the object using new (), the object is allocated on Heap, otherwise on Stack if not global or static.

What is the use of heap in C++?

The heap is used for dynamic memory allocation, either local memory (i.e. malloc) or shared memory that is mapped to the local address space. A third category exists: global and static variables, which are stored in the DATA segment of the memory map.


3 Answers

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 newed 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 ints, 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.

like image 71
Will Chen Avatar answered Oct 13 '22 07:10

Will Chen


Bicycle bicycle = new Bicycle(20,10)

The reference bicycle will be store in stack whereas the object and instance variables will be store in heap and the address of the heap is assigned in the stack so mean to say stack will link to heap.

like image 38
Kick Avatar answered Oct 13 '22 06:10

Kick


First of all, you should understand the 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.

Memory Area

Read more on Java Memory Model

like image 33
Akhilesh Dhar Dubey Avatar answered Oct 13 '22 07:10

Akhilesh Dhar Dubey