Use of Heap and Stack memory in java

Stack Memory Basics-:

Stack memory used in java to store datatype , function calls and refference varibles for objects ( handle to objects ).
its contains short-live (weak) refferences for objects stored in heap. whenever a function called , a block of memory reserved for that function and local primitive values and reference to other objects stored inside that functions block space. when function excecution ends function block space will free and available for other methods.
When all space in stack is full then jvm will throw java.lang.StackOverFlowError exception.

See below example in image - stack memory space

Heap Memory Basics-:

Heap memory used in java for store Objects and java classes. when we will create an object it will take space in heap memory. Any object created in heap accessed by all threads, it has global access. Inside application anytime we can make refference for that object.
Time to Time Garbage Collection runs and free those objects space that have no references from heap memory.
When all space in heap is full then jvm will throw java.lang.OutOfMemoryError exception.
Use of Heap dump Analyzer tool and profiler we can know how much memory consumed by object and also get how much memory remains/consumed in heap.

See below example in image -

Heap memory space

Combined example to show stack and heap memory storage :-

    package com.java_example.tutorial;
    public class Heap_Stack {
            //main() method thread creates space in stack memory
            public static void main(String[] args) {
                // primitive datatype created inside main() method space in stack memory
                int i=1; 
                // Object created in heap memory and its refference obj in stack memory
                Object obj = new Object(); 
                // Heap_Stack Object created in heap memory and its refference objnew in stack memory
                Heap_Stack objnew = new Heap_Stack(); 
                // New space for foo() method created in the top of the stack memory
            private void foo(Object p) { 
               //  String for p.toString() is created in String Pool and refference str created in stack memory
                String str = p.toString(); 

heap stack


Difference Between Heap vs Stack Memory :-

1. Primitive Datatypes , and refferences to objects are store in Stack Memory.
2. Each Object take space in Heap Memory.
3. Stack memory is less than Heap Memory.
4. Stack memory contains local varaibles for an application. Heap memory contains objects accessible
 global in an application.
5. In case of Stack memory, Variables are removed and memory space free for other variables and
 function after end code excecution.
   In case of Heap memory, if any one refference is pointing toward object then that
 object will retain space in heap memory. When object has no refference available then object called dead object and
 garbage collector will remove that object from heap memory to retain space for other objects.


Share this: