← Back to Java

Heap vs Stack Memory

JavaMiddata-structuresjava

The Question

What is the difference between Heap and Stack memory in Java?

What a Strong Answer Covers

  • stack = per-thread LIFO local variables fast
  • heap = shared all objects GC managed
  • stack automatically managed
  • StackOverflowError vs OutOfMemoryError
  • references in stack objects in heap
Heap vs Stack Memory diagram

Senior-Level Answer

In Java, memory is divided into two primary regions: the stack and the heap.

Stack memory is allocated per thread. It stores method call frames, each containing local variables, parameters, and the return address. When a method is called, a new frame is pushed; when it returns, the frame is popped and memory is instantly reclaimed. This makes stack allocation extremely fast — just a pointer adjustment. Local primitive variables live entirely on the stack. Local reference variables also live on the stack, but they point to objects on the heap. Stack size is typically small (512KB to 1MB by default, configurable with -Xss). If a thread's stack runs out of space, Java throws a StackOverflowError.

Heap memory is shared across all threads. It is where all objects and their instance variables are stored via the new keyword. The heap is managed by the garbage collector, which periodically reclaims objects no longer reachable from any live reference. The heap is divided into generations: young generation (Eden + Survivor spaces) for short-lived objects, and old generation for long-lived objects. Heap size is configurable with -Xms (initial) and -Xmx (maximum). When exhausted, Java throws OutOfMemoryError.

The distinction has important implications for thread safety. Stack memory is inherently thread-safe because each thread has its own stack. Heap memory is shared, so objects on the heap need synchronization for concurrent access.

The JVM can perform escape analysis to determine if an object created inside a method never escapes that method's scope. If so, the JVM may allocate it on the stack instead of the heap (scalar replacement), avoiding GC overhead entirely.

Key Differences

AspectStack MemoryHeap Memory
ScopePer-thread, privateShared across all threads
StoresLocal variables, method frames, referencesObjects and their instance fields
Allocation speedVery fast (pointer adjustment)Slower (GC-managed)
DeallocationAutomatic on method return (LIFO)Garbage collector
SizeSmall, fixed per thread (-Xss)Large, configurable (-Xms, -Xmx)
Error on exhaustionStackOverflowErrorOutOfMemoryError
Thread safetyInherently safe (thread-private)Requires explicit synchronization

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Correctly identifies what is stored in each, explains that stack is per-thread and heap is shared, and mentions the two error types.

3/3 — Strong Answer

Covers storage contents, allocation/deallocation mechanisms, thread isolation vs sharing, error types with causes, GC and generational heap structure, escape analysis, and JVM tuning parameters.

Common Mistakes

  • Saying reference variables are stored on the heap — the reference itself lives on the stack
  • Confusing StackOverflowError with OutOfMemoryError
  • Not mentioning that stack is per-thread and therefore inherently thread-safe
  • Ignoring escape analysis — modern JVMs can allocate short-lived objects on the stack
  • Claiming the string pool is on the stack — it is on the heap

Follow-Up Questions

  • What is escape analysis? — JIT compiler detects objects that never escape a method and may allocate them on the stack.
  • What are the generations of the Java heap? — Young gen (Eden + Survivor) and Old gen. Generational hypothesis: most objects die young.
  • How would you diagnose an OutOfMemoryError in production? — Heap dump analysis (jmap, -XX:+HeapDumpOnOutOfMemoryError), look for objects retaining large memory.
  • What is the difference between StackOverflowError and OutOfMemoryError: unable to create new native thread? — StackOverflow = one thread's stack is full. Unable to create native thread = OS cannot allocate memory for a new thread's stack.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout