Java Zero to All All questionsPractise free
Interview questions › JVM Internals & Runtime › JVM Architecture

Why does deep recursion throw StackOverflowError rather than OutOfMemoryError?

Level II · JuniorJVM Architecture
Short answer

Each method call pushes a frame onto the fixed-size per-thread JVM stack sized by -Xss, and exceeding that bound is a stack overflow, a distinct condition from running out of heap.

Explanation

OutOfMemoryError signals the shared heap or Metaspace is exhausted, which is a process-wide problem. The practical implication is that you fix StackOverflowError by reducing recursion depth or converting to iteration, not by raising -Xmx.

Common mistakes
  • Increasing -Xmx to fix StackOverflowError.
  • Not knowing -Xss controls thread stack size.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for JVM Architecture.

Practise free

More on JVM Architecture