Java Stack vs Heap Memory Allocation

Last Updated : 1 Dec, 2026

Stack and heap are two JVM runtime data areas used for method execution and object allocation. They differ in how memory is organized, shared, and reclaimed during program execution.

  • Stack memory contains method frames, including local variables, parameters, and intermediate results.
  • Heap memory contains objects and arrays and is managed through garbage collection.

Example: Understanding object allocation and reference variables.

Java
class Employee{
    int id;
    String name;
    double salary;
    Employee(int id, String name, double salary){
        this.id = id;
        this.name = name;
        this.salary = salary;
    }
    void display(){
        System.out.println("Employee ID: " + id);
        System.out.println("Name: " + name);
        System.out.println("Salary: " + salary);
    }
}
public class Geeks{
    static void display(Employee e){
        e.display();
    }
    public static void main(String[] args){

        // Create Employee objects
        Employee emp1 = new Employee(101, "Maddy", 50000.0);
        Employee emp2 = new Employee(102, "Maddy", 60000.0);
        display(emp1);
        display(emp2);
    }
}

Output
Employee ID: 101
Name: Maddy
Salary: 50000.0
Employee ID: 102
Name: Maddy
Salary: 60000.0

Explanation:

  • emp1 and emp2 are local reference variables associated with the main() method's stack frame.
  • Both Employee objects contain their respective instance fields and are allocated from the heap.
  • Both name fields refer to the same interned String object containing "Maddy".
  • Calling display(emp1) creates a stack frame whose parameter e receives a copy of the reference held by emp1.
stack
Stack and Heap Memory Representation

Stack Memory Allocation

Each thread has its own JVM stack. A stack frame is created when a method is invoked and removed when the method completes.

Example: Understanding stack frames during method execution.

Java
public class Geeks{
    static int add(int a, int b){
        int res = a + b;
        return res;
    }
    public static void main(String[] args){
        int a = 10;
        int b = 20;
        int sum = add(a, b);
        System.out.println("The sum is: " + sum);
    }
}

Output
The sum is: 30

Explanation:

  • The main() stack frame contains the local variables a, b, and sum.
  • Calling add() creates another stack frame containing its parameters and local variable res.
  • When add() completes, its frame is removed, and the returned value is assigned to sum.

Heap Memory Allocation

Heap memory is shared among all threads and is used to allocate memory for objects and arrays. An object's memory requirements depend on its fields, object header, alignment, and JVM implementation.

  • Stores objects containing primitive and reference instance fields.
  • Garbage collection reclaims memory occupied by unreachable objects.
  • Insufficient heap memory can cause OutOfMemoryError.

Example: Creating an object in heap memory.

Scanner sc = new Scanner(System.in);

When declared inside a method, sc is a local reference variable that refers to the Scanner object allocated from the heap.

Heap Generations

Generational garbage collectors organize heap memory based on object age.

  • Young Generation generally contains newly allocated objects.
  • Old Generation generally contains objects promoted after surviving garbage collection cycles.

In collectors that use Eden and Survivor spaces, both spaces belong to the Young Generation.

Note: Not all garbage collectors use generational heap organization. The internal heap structure depends on the garbage collector being used.

Stack vs Heap Memory

Feature

Stack Memory

Heap Memory

Purpose

Stores method frames and local variables.

Stores objects and arrays.

Sharing

Separate for each thread.

Shared among all threads.

Allocation

Managed by the JVM during method invocation.

Managed by the JVM during object and array creation.

Deallocation

Frames are removed when methods complete.

Memory is reclaimed through garbage collection.

Lifetime

Stack frames exist for the duration of method execution.

Object lifetime depends on reachability and garbage collection.

Organization

Stack frames follow LIFO order.

Organization depends on the JVM and garbage collector.

Memory Size

Can be fixed or dynamically expandable, depending on the JVM implementation.

Can be fixed or dynamically expandable, depending on the JVM implementation.

Thread Access

Stack frames are private to their executing thread.

Objects can be accessed by multiple threads when shared.

Memory Errors

StackOverflowError or OutOfMemoryError.

OutOfMemoryError.

Comment