Heap and Stack

Stack and Heap are the main areas of memory in Java.

Before Understanding Stack and Heap , Lets understand below terms in Java.

Local Variables -> variables which are declared inside a method or passed as a method parameter.
Instance Variables -> Variables which are declared directly inside a class

Let’s Take an Example

1
2
3
4
5
6
7
8
9
10
11
package com.kb.memorymanagement;
 
public class Example1 {
 
    int x;
    
    public void display(int y){
        System.out.println(y);
    }
 
}
package com.kb.memorymanagement;

public class Example1 {

	int x;
	
	public void display(int y){
		System.out.println(y);
	}

}

In the above example, we have one instance variable ‘x’ and one local variable ‘y’.

Now let’s see where these variables reside/live.

All local variables reside inside the Stack as long as the method is executing.
Once the method completes the execution, all the variables will be removed from the stack.

All instance variables represents the fields of each Object(which can be filled with different value for each instance).

They live inside the object they belong to.

All Objects live inside the Heap.

All method invocations reside in Stack.

Stack memory size is very less compared to Heap memory.

Let’s see it in detail

1
2
3
4
5
6
7
8
9
10
11
12
package com.kb.memorymanagement;
 
public class Example2 {
float x;
Rectangle r1=new Rectangle();
 
public static void main(String[] args) {
    Example2 ex2 = new Example2();
    ex2.x=ex2.r1.calculateAreaOfRectangle(10, 5);
}
    
}
package com.kb.memorymanagement;

public class Example2 {
float x;
Rectangle r1=new Rectangle();

public static void main(String[] args) {
	Example2 ex2 = new Example2();
	ex2.x=ex2.r1.calculateAreaOfRectangle(10, 5);
}
	
}

Rectangle.java

1
2
3
4
5
6
7
class Rectangle{
    
    public float calculateAreaOfRectangle(int length,int breadth){
        
        return length*breadth;
    }
}
class Rectangle{
	
	public float calculateAreaOfRectangle(int length,int breadth){
		
		return length*breadth;
	}
}

The stack and Heap representation of the above program is as below

memory_managment

First Main method starts the execution and hence this method is loaded into the stack.

args and ex2 are local variables of Main method, hence they get stored in Stack.
Next calculateAreaOfRectangle method getting called by the main method and hence main method stops the execution and calculateAreaOfRectangle method gets loaded into the Stack.

Length and breadth are local variables of calculateAreaOfRectangle method and hence gets stored on Stack.

Since we have 2 objects in the Heap, one is Example2 object and other one is Rectangle object . both are stored on Heap.
Instance variables x and r1 gets stored inside the Example2 Object inside Heap.

Ground Rule :
All Local Variables gets stored in Stack, method calls gets stored in Stack but Instance variables and Objects reside inside the Heap.

Note :
We can use -Xms and -Xmx JVM option to define the startup size and maximum size of heap memory.

We can use -Xss to define the stack memory size.

When stack memory is full, Java runtime throws java.lang.StackOverFlowError
whereas if heap memory is full, it throws java.lang.OutOfMemoryError: Java Heap Space error

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on