Java Heap Memory
Heap memory is used by Java runtime to allocate memory to Objects and JRE classes. Whenever we create any object, it's always created in the Heap space. Garbage Collection runs on the heap memory to free the memory used by objects, that doesn ' t has any reference. Any object created in the heap space has global access and can is referenced from anywhere of the application.
Java Stack Memory
Java Stack memory is used for execution of a thread. They contain method specific values that is short-lived and references to other objects in the heap that is getting refe Rred from the method. Stack memory is always referenced in LIFO (last-in-first-out) Order. Whenever a method is invoked, a new block was created in the stack memory for the method to hold local primitive values and Reference to and objects in the method. As soon as method ends, the block becomes unused and become available for next method.
Stack memory size is very less compared to Heap memory.
Let's understand the Heap and Stack memory usage with a simple program.
Package com.journaldev.test; public class Memory {public static void Main (string[] args) {//Line 1 int. I=1;//Line 2 Object obj = new O Bject (); Line 3 Memory mem = new Memory (), line 4 mem.foo (obj),//Line 5 }//Line 9 private void foo (Object param) {//Line 6 String str = param.tostring (),////Line 7 System.out.println (str), }//Line 8}
Below image shows the Stack and Heap memory with reference to above program and how they is being used to store primitive , Objects and reference variables.
Let's go through the steps of execution of the program.
- As soon as we run the program, it loads all the Runtime classes into the Heap space. When main () method was found at line 1, the Java Runtime creates stack memory to being used by main () method thread.
- We is creating primitive local variable at line 2, so it's created and stored in the stack memory of main () method.
- Since we are creating a Object in line 3, it's created in Heap memory and stacks memory contains the reference for it. Similar process occurs when we create a Memory object in line 4.
- Now if we call Foo () method on line 5, a block in the top of the stack is created to being used by Foo () method. Since Java is pass by value, a new reference to Object was created in the Foo () stack block on line 6.
- A string is created on line 7, it goes in the string Pool in the heap space and a reference are created in the Foo () stack Space for it.
- Foo () method is terminated on line 8, at the time memory block allocated for Foo () on stack becomes free.
- In line 9, main () method terminates and the stack memory created for main () method is destroyed. Also The program ends at the "this", hence Java Runtime frees all the memory and end of the execution of the program.
Difference between Heap and Stack Memory
Based on the above explanations, we can easily conclude following differences between Heap and Stack memory.
- Heap memory is used by all the parts of the application whereas stack memory are used only by one thread of execution.
- Whenever an object is created, it's always stored in the Heap space and stacks memory contains the reference to it. Stack memory is contains local primitive variables and reference variables to objects in heap space.
- Objects stored in the heap is globally accessible whereas stack memory can ' t is accessed by other threads.
- Memory Management in Stacks is do in LIFO manner whereas it's more complex in Heap memory because it ' s used globally. Heap memory is divided to young-generation, old-generation etc, more details at Java Garbage Collection.
- Stack memory is short-lived whereas heap memory lives from the start till the end of application execution.
- We can use -xms and -xmx JVM option to define the startup size and maximum size of the heap memory. We can use -XSS to define the stack memory size.
- When stack memory was full, Java runtime throws
java.lang.StackOverFlowError whereas if heap memory was full, it throws java.lang.OutOfMemoryError: Java Heap Space error.
- Stack memory size is very less when compared to Heap memory. Because of simplicity in memory allocation (LIFO), stack memory was very fast when compared to heap memory.
That's all for stacks vs Heap Memory in terms of Java application, I hope it'll clear your doubts regarding memo RY allocation When any Java program is executed.
Hara Pankaj
Good text sharing the difference between _java stacks