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
Example
- 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 understand in detail
Example2.java
- 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
- 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
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
.
Remember :
All Local Variables and method calls gets stored inStack
, But Instance variables and Objects reside inside theHeap
.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 throwsjava.lang.StackOverFlowError
whereas if
heap
memory is full, it throwsjava.lang.OutOfMemoryError: Java Heap Space error
Nice article KB. I am fallowing yours blog daily to enhance my skills.
Could you please, provide me Document kind of thing for JAVA internal details.!!
Thanks a lot !!!!
Hi sir,,, Sir if possible please add opps concept with real life scenario….!!
Thanks karibasappa.
Hi KB,
In Ex2 class u created Rectangle object,
How can we create an object outside the main method
Hi Vinay,
you can do that just like assigning values to variables.
These are called initial values.
I’ve got a problem in here, it shows nothing when you run the project !!
public static void main(String[] args) {
Example2 ex2 = new Example2();
ex2.x=ex2.r1.calculateAreaOfRectangle(10, 5);
}
the output is :
run:
BUILD SUCCESSFUL (total time: 1 second)
What’s the ground for that, please ?!
ex2.x=ex2.r1.calculateAreaOfRectangle(10, 5);
can u expain this line deeply bcs i have geeting a problem to understand why we need two object to calling method
Hi Veer Tiwari,
ex2.x=ex2.r1.calculateAreaOfRectangle(10, 5);
In the above line, The goal is to call the calculateAreaOfRectangle() method which is present in the Rectangle class.
we need object of Rectangle class to call the non static method, so used r1.calculateAreaOfRectangle(10, 5);
Now since r1 is a non static variable defined inside Example2 class,so to access r1, need to create an object of Example2, so i have created an object and used the variable ex2.
So overall it becomes as ex2.r1.calculateAreaOfRectangle(10, 5);
and assigning returned values to x variable of ex2 object.
Any doubts ?
I have not added any System.out.println() statements to print the result.
So it wont print anything.
I just explained how the variables are stored in the memory.
You just add
after below line
Run the program , you will get the output in console
you made very simple to understand the java concepts
Thanks !!