DEV Community

Cover image for Understanding the Java Memory Model: Heap and Stack Explained
Bellamer
Bellamer

Posted on

Understanding the Java Memory Model: Heap and Stack Explained

After understanding how Java handles pass by value and pass by reference, the next step is to dive deeper into Java’s memory model. Specifically, we’ll explore the heap and the stack—two critical components of memory management in Java. A clear understanding of these concepts will help you write efficient code.

What are Heap and Stack in Java?

In Java, the memory used by a program is divided into two main areas:

1.Heap Memory: Used for dynamic allocation of objects and class instances.

2.Stack Memory: Used for storing method call details, local variables, and references.

Heap Memory: The Dynamic Memory Pool

  • Purpose: The heap is where all objects and their instance variables (fields) are stored.
  • Characteristics: Shared by all threads. Objects remain in the heap until they are no longer referenced, at which point they become eligible for garbage collection. Allocated at runtime.
  • Example Usage: Any object created with the new keyword resides in the heap.

Stack Memory: The Execution Context

  • Purpose: The stack is used to manage method execution and local variables (primitives and object references).
  • Characteristics: Each thread has its own stack (thread-local memory). Follows the Last In, First Out (LIFO) principle. Automatically allocated and deallocated when methods are called and return.
  • Example Usage: Method calls, local variables, and references to objects are stored in the stack.

Heap vs. Stack: Key Differences

Feature Stack Heap
Storage Method calls, local variables Objects and instance variables
Access LIFO (fast) Dynamic access (slower)
Thread Ownership Thread-local Shared across all threads
Lifetime Limited to method execution Exists until garbage collected
Size Smaller and fixed Larger and grows dynamically
Management Automatically managed Managed by garbage collector

How Heap and Stack Work Together

To understand how the heap and stack interact, let’s revisit pass by value and pass by reference:

  1. Primitive Types: Stored directly in the stack. Passed by value as a copy.
  2. Object References: The reference (memory address) is stored in the stack. The actual object is stored in the heap. When passed to a method, the reference is copied, but it still points to the same heap object.

Code Example: Heap and Stack in Action

Let’s analyze a simple program to understand how heap and stack memory are used.

class Person {
    String name;

    Person(String name) {
        this.name = name;
    }
}

public class HeapStackExample {
    public static void main(String[] args) {
        int number = 10; // Stored in the stack
        Person person = new Person("Alice"); // Reference in stack, object in heap

        System.out.println("Before: " + person.name); // Output: Alice
        modifyPerson(person);
        System.out.println("After: " + person.name); // Output: Bob
    }

    public static void modifyPerson(Person p) {
        p.name = "Bob"; // Modifies the object in the heap
    }
}

Enter fullscreen mode Exit fullscreen mode

Memory Breakdown:

  • Stack Memory:
    • number: Stored directly in the stack.
    • person: A reference stored in the stack, pointing to the Person object in the heap.
    • p: A copy of the person reference, also stored in the stack.
  • Heap Memory:
    • A Person object with its name field ("Alice" initially, then "Bob" after modification).

How Method Calls Affect the Stack

When a method is invoked:

  • A new stack frame is created to store:
    • Method arguments.
    • Local variables.
    • A reference to the current object (this) for instance methods.
  • When the method returns, its stack frame is removed, freeing up memory.

Here’s an example:

public class StackDemo {
    public static void main(String[] args) {
        int result = calculate(5); // Call method, push stack frame
        System.out.println(result); // Output: 25
    }

    public static int calculate(int n) {
        return n * n; // Stack frame includes 'n'
    }
}

Enter fullscreen mode Exit fullscreen mode

Memory Usage:

  • The main method creates a stack frame for itself.
  • When calculate(5) is called, a new stack frame is pushed.
  • Once calculate finishes, its frame is popped, and memory is freed.

Common Issues with Heap and Stack

  • StackOverflowError:
    Occurs when the stack runs out of memory, usually due to deep recursion or infinite method calls.

  • Example:

public class StackOverflowExample {
    public static void recursiveMethod() {
        recursiveMethod(); // Infinite recursion
    }

    public static void main(String[] args) {
        recursiveMethod();
    }
}

Enter fullscreen mode Exit fullscreen mode
  • OutOfMemoryError: Heap Space:
    Occurs when the heap is full and cannot allocate more memory for new objects.

  • Example:

import java.util.ArrayList;

public class HeapOverflowExample {
    public static void main(String[] args) {
        ArrayList<int[]> list = new ArrayList<>();
        while (true) {
            list.add(new int[100000]); // Keep allocating memory
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

Optimizing Heap and Stack Usage

Heap:

  • Use objects only when necessary.
  • Make use of object pooling for expensive or repetitive objects.
  • Avoid creating unnecessary references that prevent garbage collection.

Stack:

  • Avoid deep recursion.
  • Use efficient algorithms that reduce the number of method calls.

Key Takeaways

  • Heap and Stack Work Together:
    • Objects are stored in the heap, while references and local variables are stored in the stack.
  • Stack is Faster but Limited:
    • It grows and shrinks automatically with method calls.
    • Designed for temporary, thread-local data.
  • Heap is Larger but Shared:
    • Used for objects that need to persist beyond a single method call.
    • Managed by the garbage collector.
  • Know the Limits:
    • Avoid stack overflows with controlled recursion.
    • Optimise object creation to avoid heap memory issues.

With a solid understanding of how heap and stack memory work in Java, you’ll be better equipped to write efficient, bug-free programs and understand how Java manages memory behind the scenes.

Top comments (0)