12

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?

Freiheit
  • 7,407
  • 6
  • 52
  • 95
Jude
  • 133
  • 1
  • 6
  • possible duplicate of [What and where are the stack and heap?](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – EkoostikMartin Mar 13 '14 at 18:48
  • @fge I understand that heap should be implemented as a tree. So how the object is stored in a tree? – Jude Mar 13 '14 at 18:55
  • 1
    Heap is just a giant array of bytes. An object is a structure -- a sequence of fields which are treated as a single entity and allocated together. To create an object you find a suitable sized bit of "free" heap, set up the object header (pointer to it's class and some other "bookkeeping" info), and perform the initializations described in the class. – Hot Licks Mar 13 '14 at 19:21

3 Answers3

18

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.

Will Chen
  • 316
  • 1
  • 3
  • 1
    thanks for your explanation, Will! I got confused because of one data structure also called "heap". http://stackoverflow.com/questions/1699057/why-are-two-different-concepts-both-called-heap this link helps me a lot too. I have one question for you: where are the member functions are stored? are they also stored in the heap? – Jude Mar 13 '14 at 21:48
  • Member function are store in stack @Jude – Kick Mar 14 '14 at 05:14
  • Yeah the Heap ADT is totally different. I think you need to build more knowledge in lower level computer architecture before it will all make sense. 1. The actual code (bytecode or assembly) is stored in codespace elsewhere. 2. The parameter values are pushed onto the stack (alone with some callback/return memory address) before the current line pointer is moved. 3. Objects in the heap stay as they are until they are destroyed. As the picture above a reference is really just an int of memory address. – Will Chen Apr 02 '14 at 00:06
  • very complete answer. – dreamcrash Apr 04 '14 at 00:57
3

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

Akhilesh Dhar Dubey
  • 2,106
  • 2
  • 24
  • 39
2
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.

Kick
  • 4,689
  • 1
  • 18
  • 24
  • 1
    I understand that the reference will be in stack. I don't know how exactly the object is stored in heap. – Jude Mar 13 '14 at 18:53