I was pair programming with an experienced Java Developer today and we again came up with the discussion of what goes on a stack and what goes on a heap. Though while coding application logic we are hardly concerned on what goes where but it definitely helps to know as it would help in better understanding of argument passing, polymorphism, threads, exceptions, and garbage collection.
Various pieces of Java program lie on one of the two places i.e. either on stack or on heap.
For a quick grasp read further
- Instance variables and Objects lie on Heap. Remember this is where the state is maintained and when you get memory leaks this is where your profiler helps you to find the allocation of memory.
- Local variables and methods lie on the Stack. So if we have a main method which calls the go() method which calls the gone() method then the stack from top to bottom would consist of
gone()
go()
main()
as soon as gone() has been processed it would be removed from the stack. Any corresponding local variables which are used in gone() would also be removed from the stack. Stack would have references to objects on the Heap.
laxmi
Monday, November 2, 2009
it is easy to understand.
Vikas Khengare
Thursday, December 17, 2009
Nice explanation.
Naveen
Sunday, December 11, 2011
Really great EXample.Thanks.But please that where referene will be stored?
Sumeet Singh
Saturday, January 9, 2010
very nice explanation… also refer http://www.sap-img.com/java/difference-between-stack-and-heap.htm
Neerav
Wednesday, November 9, 2011
@Sumeet..
Your link makes more sense.
James Do
Friday, July 9, 2010
Good job!
This article is very easy to understand!
Thank you.
Tom Plunket
Wednesday, August 25, 2010
How wide of a screen do I need to see the above image? At 1000 pixels I see only half at best.
banhbaochay
Thursday, December 9, 2010
It’s helpful to me 🙂 Thank you very much
@Tom: you can click to image to view full one
rukmal
Sunday, January 9, 2011
where does to static variables and methods lies on?
Ariston
Friday, February 4, 2011
Hello Vikas,
Thanks for this article. It’s quite helpful.
I know this post was made a long time ago, but I am puzzled by something and I was hoping you could clear this up for me:
I’m assuming setName() is a method (belonging to) and defined inside the Dog class. When you show it (setName()) on the stack, do you actually mean a POINTER to an address on the heap rather than the ACTUAL FUNCTION itself (because the actual function is created on the heap along with the Dog object d)?
Just to be clear, I’m assuming the code for your diagram looks something like this:
class DogTest(){
public static void main(){
Dog d = new Dog(); //d is a reference to a Dog object and is stored on the stack; the object itself is created on the heap
String dogName = “Aiko”; //created on the stack
d.setName(dogName); //what is created on the stack is actually a pointer to the actual code that is created on the heap when the Dog object d was created
}
}
class Dog(){
String name;
public void setName(String s){
name = s;
}
//also code here for the Collar object
}
Thanks for any help you can give,
Ariston.
boechi
Tuesday, April 5, 2011
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 Collar { }
2.
3. class Dog {
4. Collar c; // instance variable
5. String name; // instance variable
6.
7. public static void main(String [] args) {
8.
9. Dog d; // local variable: d
10. d = new Dog();
11. d.go(d);
12. }
13. void go(Dog dog) { // local variable: dog
14. c = new Collar();
15. dog.setName(“Aiko”);
16. }
17. void setName(String dogName) { // local var: dogName
18. name = dogName;
19. // do more stuff
20. }
21. }
■ Line 7—main() is placed on the stack.
■ Line 9—reference variable d is created on the stack, but there’s no Dog object yet.
■ Line 10—a new Dog 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 dog parameter as a local variable.
■ Line 14—a new Collar object is created on the heap, and assigned to Dog’s instance variable.
■ Line 17—setName() is added to the stack, with the dogName 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 Dog 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 dogName disappears too, although the String object it referred to is still on the heap.
Vikas Hazrati
Tuesday, April 19, 2011
Perfect! thanks for the comment boechi
Elder
Friday, July 15, 2011
Godd stuff!!!
JonathanRC2
Friday, September 16, 2011
This is perfect! A quick, short, focused tip on heap vs stack. End of tip. Good job Vikas! –Jonathan
K
Wednesday, April 25, 2012
Excellent explanation, thank you!!