Java Zero to All All questionsPractise free
Interview questions › Interview Rapid-Fire — Top Q&A › JVM Internals — Top Interview Q&A

What is the difference between Heap and Stack memory?

Level I · BeginnerJVM Internals — Top Interview Q&A
Short answer

The heap is a single shared region where all objects and arrays live; it is managed by the garbage collector and is where GC pauses happen.

Explanation

The stack is per-thread: each thread has its own stack of frames holding local variables, operand values, and return addresses, freed automatically when a method returns (LIFO). Primitives and object references sit on the stack, but the objects they point to live on the heap. Stack overflow (deep recursion) throws StackOverflowError; exhausting the heap throws OutOfMemoryError: Java heap space.

Common mistakes
  • Saying objects can live on the stack.
  • Saying the stack is shared between threads.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for JVM Internals — Top Interview Q&A.

Practise free

More on JVM Internals — Top Interview Q&A