Friday, January 28, 2011

Chapter 15: Stack and Heap

You might be wondering, is Stack and Heap such a large topic that I have dedicated one full chapter to it? Actually it isnt such a big topic but it is extremely important to understand them fully and clearly in order to grasp the forthcoming topics clearly. And combining this with any other topic would make it a bit confusing or rather off-place. So it is given a separate topic status. (We will be touching about these as and when required in the forthcoming chapters and this chapter would just be a quick overview)

For the most part, the various pieces (methods, variables, and objects) of Java programs live in one of two places in memory: either the stack or the heap. For now, let us worry about only three types of things: instance variables, local variables, and objects:
• Instance variables and objects live on the heap.
• Local variables live on the stack.

Let’s take a look at a Java program, and how its various pieces are created and map into the stack and the heap:

1. class Maine { }
2.
3. class Lion {
4. Maine c; // instance variable
5. String name; // instance variable
6.
7. public static void main(String [] args) {
8.
9. Lion d; // local variable: d
10. d = new Lion();
11. d.go(d);
12. }
13. void go(Lion Lion) { // local variable: Lion
14. c = new Maine();
15. Lion.setName("Aiko");
16. }
17. void setName(String LionName) { // local var: LionName
18. name = LionName;
19. // do more stuff
20. }
21. }

This is how the variables and methods get placed in the stack and heap during execution of the above piece of code.

• Line 7—main() is placed on the stack.
• Line 9—reference variable d is created on the stack, but there’s no Lion object yet.
• Line 10—a new Lion object is created and is assigned to the d reference variable.
• Line 11—a copy of the reference variable d is passed to the go() method.
• Line 13—the go() method is placed on the stack, with the Lion parameter as a local variable.
• Line 14—a new Maine object is created on the heap, and assigned to Lion’s instance variable.
• Line 17—setName() is added to the stack, with the LionName parameter as its local variable.
• Line 18—the name instance variable now also refers to the String object.
• Notice that two different local variables refer to the same Lion object.
• Notice that one local variable and one instance variable both refer to the same String Aiko.
• After Line 19 completes, setName() completes and is removed from the stack. At this point the local variable LionName disappears too, although the String object it referred to is still on the heap.

Previous Chapter: Self Test - Chapters 6 to 14

Next Chapter: Chapter 16: Literals, Assignments and Variables

8 comments:

  1. On the SUN JVM implementation, is PermGen part of heap or stack, or something else?

    ReplyDelete
    Replies
    1. PermGen Space is not part of Heap and its a separate memory area configurable using JVM options -XX:PermGenSize and -XX:MaxPermGenSize. I have also shared few points about Heap Space in Java, you may find useful.

      Delete
  2. I think as per the latest versions PerGen is part of neither. Its seperate

    ReplyDelete
  3. I see some documents mentioned PermGen as heap but others as non-heap. In Sun/Oracle document on "Java SE 6 HotSpot Virtual Machine Garbage Collection Tuning" section 4 actual contains ".. different parts of the heap(permanent generation, tenured ... and young...)". I think the mix-up might be due to the value of -Xmx DOES NOT include PermGen at all.(from 1.4.2 or even earlier to current version)

    With "objects describing classes and methods" are stored in PermGen that it will be good that you can update this example code to point out what actual put in the PermGen.

    ReplyDelete
  4. I have one question: since we all know that Object class is super class of all class. Since we also know that String is class then as per java theory String class is child class of object class. As we also know that Parent class donot return child class object. Then How toString() method of Object return String object?

    ReplyDelete
  5. A String object is inturn an Object because String class extends the Object class. So, an object returning an object of itself is perfectly fine.

    Remember - everything in Java extends the Object class and even the String class does the same.

    So, the toString() method returning a String object is perfectly fine.


    Buddy - you gave the answer yourself. Your last line says = Then How toString() method of Object return String object?

    Object class can always return an object

    ReplyDelete
  6. As i know all objects creation memory allocated in Heap and its reference are either in Heap or stack depends upon it scope.

    Like in above example :
    ine 9—reference variable d is created on the stack, but there’s no Lion object yet.

    Only reference of d is in Stack but memory get allocated in Heap.

    Please correct me if i am wrong

    ReplyDelete

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers