Ticker

6/recent/ticker-posts

Java memory management | Heap Memory v/s Stack Memory

Java memory management 

Java Heap Memory v/s Stack Memory 

Java Heap Memory 

This is the memory where the Objects and JRE classes are stored at the runtime. When the object is created it is stored in the heap. Garbage Collection runs on the heap memory to free the memory used by objects that doesn’t have any reference. Any object created in the heap space has global access and can be referenced from anywhere of the application. In multithreading the objects are available for all the threads, means all the threads share the same heap 

Java Stack Memory 

Java Stack memory is used for execution of a thread. They contain method specific values that are short-lived and references to other objects in the heap that are getting referred from the method. Stack memory is always referenced in LIFO (Last-In-First-Out) order. Whenever a method is invoked, a new block is created in the stack memory for the method to hold local primitive values and reference to other 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. 

public class Memory { 

        public static void main(String[] args) { // Line 1 

        int i=1; // Line 2 

        Object obj = new Object(); // 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 

}

When we run a program it loads all the runtime class into the heap space 

The main method is found at the Line-1 now the memory for the main method is allocated in the stack region by the main thread. 

We are creating the local variables at Line-2 it is going to be created in the stack region of main block. 

In Line-3 we are creating the object the object is going to be created in the heap region and its reference is stored in the stack region, similarly in Line-4 we are creating one more object. 

In Line-5 we are calling the method foo( ) than the memory for the method is going to be allocated in the stack region ( the block of memory is going to be allocated in the stack region). Since java is pass by value the one more reference is created at the Line-6 

From the foo( ) method we are creating one more String object it is going to be created in the heap region, the reference is present in the stack region inside the stack. ( String objects are going to be created in the String pool in the heap) 

Has soon has the control reach Line-8 the method looses the scope, than the memory that was allocated to the method foo( ) becomes free 

When the Line-9 is executed than the main method also going to loose the scope, and the memory allocated to it also get free. Than java runtime will free al the memory allocated and the execution completes.

Difference between Heap v/s Stack 

Objects stored in the heap are globally accessible whereas stack memory is not accessed by other threads. 

Heap is the place where objects are stored, the stack region is the place where the local variables are stored and the reference to the objects that are stored in heap region. 

Stack memory is short lived where has heap memory is going to exists till the end of application execution.

When stack memory is full it throws java.lang.StackOverFlowError , where has if heap memory is full java.lang.OutOfMemoryError 

Stack memory is very less when compared to Heap 

Stack is very fast when we compared to Heap 

ALSO SEARCH:

"java memory management "

"java memory model"

"heap memory vs stack memory java"

"is stack memory faster than heap"

"heap vs stack memory java"

"memory management heap vs stack"

"heap memory vs stack memory java"