Stack is a portion of the memory where local primitive variables or local object reference variables are stored.
Such local variables live
on the Stack until a block or method in which they are declared is being executed in a live thread.
Heap
Heap is a portion of the memory where instances/objects and their instance variables are stored.
Objects and instance variables continue to live on Heap until they are referenced by their reference variables.
Once an object is no longer referenced by any reference variable, it is eventually removed from the Heap by Garbage Collector.
An example of Stack and Heap with object and local variables.
//Java - Example of Stack and Heap with object and local variables
class A
{
public void add(int a, int b)// a and b are local variables of add method & they live on Stack.
{
int total = a+b; //total is also a local variable of add method that lives on the Stack.
System.out.println(total);
}
public void message(String str) //str is a local reference variable that points to a String object
{
System.out.println(str);
}
public static void main(String... ar)
{
A ob= new A(); //Object of A is created on heap but its reference variable, ob, lives on stack.
ob.add(2,3);
ob.message("Hello");
}
}
Let's divide the code mentioned above into several parts and understand it with diagrams to have a clear
understanding of Stack and Heap -
A ob= new A();
The currently executing method is always placed on the top of the Stack. As the program begins its execution,
the main() method becomes the currently executing method, hence, it is placed in the Stack.
Within the main() method, we have created an object of A class. This object exists on the heap and it is referenced by a reference variable, ob, which exists on the Stack. See, Figure-1.
ob.add(2,3);
Within the main() method, the add() method is called, which becomes the currently executing method and hence it gets placed at the top of the Stack with its local variables a,b and total. See Figure-2.
ob.message("Hello");
The add() method finishes its execution and it is taken off the Stack, including its local variables.
Next, within the main() method, the message() method is called and it is passed a String object Hello. which is created on the Heap.
When the message() method starts its execution, it gets placed on the top of Stack and the String object Hello which is passed to this method, is being referenced by the local reference variable, str. See Figure-3.
Next, as soon as the message() method finishes its execution and it is taken off the Stack including its local reference variable, str.
At this moment, String object has no reference and it is eligible for garbage collection. See, Figure-4.
Next, the main() method finishes its execution and it is taken off the Stack, leaving it empty.
At this point of time, String object and the object of A class have no references variables pointing to them on Heap, hence they are eligible for garbage collection. See, Figure-5.
Advertisement
An example of Stack and Heap with object, local variables and instance variables.
//Java - An example of Stack and Heap with object, local variables and instance variables
class A
{
int x,y; //instance variables are always on the Heap.
public void add(int a, int b) // a and b are local variables and they live on the Stack.
{
x = a;
y = b;
int total = x+y;
System.out.println(total);
message("Hello");
}
public void message(String str)
{
System.out.println("Message is : " + str);
}
public static void main(String... ar)
{
A ob= new A(); // an object is created that lives on the heap but its reference variable lives on the Stack.
ob.add(2,3);
}
}
Let's understand the preceding code in several parts with diagrams to have a clear understanding of the Stack and Heap -
A ob= new A();
Being the currently executing method, the main() method is placed in the Stack.
Within the main() method, we have created an object of A class. This object exists on the heap and it is referenced by a reference variable, ob, which exists on the Stack. See, Figure-2.1.
ob.add(2,3);
Within the main() method, the add() method is called, which becomes the currently executing method, and
therefore, it gets placed at the top of the Stack with its local variables a,b and total.
Within the add() method, the instance variables x and y are given the values 2 and 3 by the local variables, a and b. The sum of these two variables is calculated in a local variable, total. See Figure-2.2.
Next, within the add() method, the message() method is called, which becomes the currently executing method and hence it gets placed at the top of the Stack with its local variable, str.
A String object, Hello is created on the Heap and it is being referenced by the local reference variable, str, of the message() method.
Note: Instance variables are always a part of an object.
See Figure-2.3.
Next, the method message() finishes its execution and it is taken off the Stack including its local object reference variable, str.
At this point of time, String object has no reference and it is eligible for garbage collection. See, Figure-2.4.
Next, the method add() finishes its execution and it is taken off the Stack including its local variables - a, b, and total.
At this point of time, String object has no reference and it is eligible for garbage collection. See, Figure-2.5.
Eventually, the method main() finishes its execution and it is taken off the Stack.
At this point of time, String object and the object of A class have no references and they are eligible for garbage collection. See, Figure-2.6.