Stack and Heap Memory

Heap vs Stack Memory in Java: What's the Difference?

Heap vs stack memory in Java: where primitives, references and objects live, how each is managed, and the most common errors each one can throw.

On this page
  1. The short answer for Java
  2. What goes on the stack
  3. What goes on the heap
  4. Heap vs stack in Java at a glance
  5. Common errors
  6. Why this model is a simplification
  7. Key takeaways

The short answer for Java

Java programs use two main memory areas for the data the code creates. The stack is kept for each thread and holds the frames of the methods that are currently running. The heap is shared by all threads and holds objects and arrays. Java’s garbage collector reclaims heap objects the program can no longer reach, so you don’t call free or delete yourself.

What goes on the stack

When a method is called, the JVM creates a frame for it on the current thread’s stack. The frame holds:

  • primitive local variables such as int, long, boolean and double, whose values are stored in the frame
  • local variables that refer to objects, which store a reference to the object rather than the object itself
  • the method’s parameters

The frame is removed when the method returns. Each thread has its own stack, and its size is set by the JVM. You can adjust it with the -Xss option.

Here is a small example:

public int countItems(List<String> items) {
    int count = 0;                 // primitive: the value lives in this method's frame
    for (String item : items) {    // item is a reference to a String object elsewhere
        count++;
    }
    return count;                  // the frame is removed once the method returns
}

The items list was created by whoever called this method, so it lives on the heap. This method only holds a reference to it in its frame.

What goes on the heap

Anything created with new goes on the heap. That includes:

  • objects of any class you define
  • arrays, which are objects in Java
  • the fields of an object, which are stored inside that object on the heap
  • objects from the standard library, such as strings and collections

The heap is shared by all threads in the program. An object that no live reference can reach is eligible for garbage collection, and the collector reclaims it at some later point that you don’t control.

Heap vs stack in Java at a glance

Stack Heap
Holds Method frames: primitives, references and parameters Objects and arrays
Scope One stack per thread Shared by all threads
Lifetime Until the method returns Until the garbage collector finds the object unreachable
Managed by The JVM, as methods are called and return The garbage collector
Size setting -Xss, per thread -Xmx sets the maximum heap size
Typical error StackOverflowError OutOfMemoryError: Java heap space

Common errors

  • StackOverflowError usually means method calls have nested too deeply, often because of recursion with no proper stopping point.
  • OutOfMemoryError: Java heap space usually means the program is keeping too many objects reachable. A collection that keeps growing and is never cleared is a typical cause. Adding more memory isn’t always the answer, because the objects may still be needed or may be a leak.

Why this model is a simplification

The split between primitives and references on the stack, and objects on the heap, is a useful way to reason about Java code. It isn’t a complete picture. The JVM may optimize how memory is used, including avoiding heap allocation for some objects, so don’t rely on exact placement when you measure performance.

The idea also connects to primitive vs non primitive data types. Primitives are copied by value, while objects are reached through references. If you want the full list of which types count as primitive, read about what are the 8 primitive data types.

For the general comparison that applies outside Java, see the difference between stack and heap memory.

Key takeaways

  • Each Java thread has its own stack of method frames. Primitives, references and parameters live there.
  • Objects and arrays live on the heap, which is shared and cleaned up by the garbage collector.
  • StackOverflowError and OutOfMemoryError point to different problems, and the fixes differ too.

The complete guide to stack and heap memory

Difference Between Stack and Heap Memory
Read the guide

Keep exploring

Guides from other topics

All topics →

Is an Array a Data Structure?

Yes, an array is a data structure. Learn what an array is, how it stores elements in memory, what its operations cost, and how it fits into classifications.

Arrays 4 min read