StackOverflowError — What It Looks Like and Why It Happens
Fast recall
Quick revision
Core answer
StackOverflowError means one thread exhausted its Java call stack. Repeated frames usually reveal direct or mutual recursion with no reachable stop, but very deep legitimate call chains or unusually large frames can also exhaust the stack. Find the repeating cycle and input, fix the termination/progress rule or use an iterative design where appropriate; do not merely increase stack size before understanding the cause.Interview-ready guide
Interview answer
StackOverflowError means the current thread could not allocate another Java stack frame.
A repeated method or short cycle in the trace usually reveals direct or mutual recursion that is not making progress toward a base case.
For example, a tree walk can work on balanced data but overflow on a highly skewed tree with unbounded depth. I find the shortest repeated frame cycle and the input that causes it, then check both termination and progress. Infinite recursion is common, but a finite and extremely deep call chain or large frames can also exhaust the stack.
Where input depth is not safely bounded, an explicit heap-based stack or iterative algorithm is often better. Raising -Xss only moves the limit and can reduce the number of threads the process can support, so it is not the first fix.
StackOverflowError — What It Looks Like and Why It Happens
Direct Answer
StackOverflowError means one thread exhausted its Java call stack. Repeated frames usually reveal direct or mutual recursion with no reachable stop, but very deep legitimate call chains or unusually large frames can also exhaust the stack. Find the repeating cycle and input, fix the termination/progress rule or use an iterative design where appropriate; do not merely increase stack size before understanding the cause.
Deep Dive & Architecture
Every Java method call consumes a stack frame on the current thread. When the thread cannot allocate another frame, the JVM throws StackOverflowError.
A trace often repeats one method for direct recursion or a small group for mutual recursion: a → b → a. Locate the shortest repeated cycle and the input that fails to make progress toward its base case. Recursion that works for small trees can still overflow on a highly skewed or attacker-controlled structure.
Not every overflow is an infinite loop. A finite but very deep graph traversal, recursive parser, generated proxy path, or large per-frame state may be enough. An iterative algorithm with an explicit heap-based stack can remove call-stack depth when appropriate. Raising -Xss only changes the limit and can reduce how many threads fit in memory, so measure it after correcting the design.
Common Mistakes & Pitfalls
- No base case (or one that's unreachable, e.g.
n != 0with a step that skips 0). - Mutual recursion:
a()callsb()which callsa()with no termination. - Accidental recursion: a getter calling itself, or
toString()/equals()referencing themselves.
Check your understanding
Answer these without looking back at the article.
- How do you read a Java stack trace to find the source of an error?
- What does 'Caused by' in a Java stack trace mean and how do you use it?
- How do you read a NullPointerException stack trace in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.