What Causes StackOverflowError?
Fast recall
Quick revision
Core answer
StackOverflowError means a thread's computation requires a larger JVM stack than the runtime permits. The usual cause is infinite or unexpectedly deep recursion, though a very deep call chain or unusually large frames can contribute. Fix the call structure or bound the input before treating stack-size tuning as the solution.Interview-ready guide
Interview answer
StackOverflowError means one thread needs more JVM stack space than the runtime allows. Every unfinished method call keeps a frame, so a call chain that grows too far eventually reaches the limit.
The usual cause is recursion with a missing or unreachable base case. Mutual recursion can do the same thing. Even a correct recursive algorithm can fail on very deep input, such as a highly skewed tree with hundreds of thousands of nodes.
For example, countDown(n) must move n toward the value that stops recursion. Calling countDown(n + 1) moves in the wrong direction and keeps creating frames. A graph traversal can also recurse forever when it does not track already visited nodes.
The stack trace normally shows the same method, or a small group of methods, repeated many times. Fix the base case, detect cycles, limit unreasonable depth, or use iteration with an explicit stack. Increasing -Xss may help a known bounded workload, but it cannot make endless recursion safe.
What Causes StackOverflowError
Direct Answer
StackOverflowError means a thread's computation requires a larger JVM stack than the runtime permits. The usual cause is infinite or unexpectedly deep recursion, though a very deep call chain or unusually large frames can contribute. Fix the call structure or bound the input before treating stack-size tuning as the solution.
Deep Dive & Architecture
Recursive progress and the base case
A recursive method needs a base case and a step that moves every path toward it. visit(node.left) and visit(node.right) terminate for a true tree because each call moves to a smaller subtree and null is the base case. If the input is actually a graph with a back edge, the same code can revisit a node forever unless it keeps a visited set.
Repeated stack frames in the trace
Direct recursion repeats one method name. Mutual recursion repeats a small cycle such as parseExpression → parseTerm → parseExpression. The first useful frame in application code often identifies the input and condition that stopped progress. Do not hide the evidence by broadly catching Error.
Structural fixes for excessive recursion
Correct a missing base case, enforce a nesting limit, detect cycles, or replace recursion with an explicit work stack. The iterative form still uses O(depth) auxiliary storage, but depth becomes data that code can inspect and limit. Increasing -Xss may support a known bounded algorithm; it only delays failure for unbounded calls and consumes more memory per platform thread.
Two methods can overflow at different depths because their frames, runtime, platform, and compilation differ. Published universal depth or default-stack numbers are unreliable. Virtual threads change stack representation and mounting but do not make infinite recursion safe.
Code Example
class StackOverflowCause {
static int sumTo(int n) {
if (n < 0) throw new IllegalArgumentException("n must be non-negative");
if (n == 0) return 0;
return n + sumTo(n - 1);
}
public static void main(String[] args) {
System.out.println(sumTo(10));
}
}
Changing n - 1 to n + 1 removes progress and demonstrates the bug conceptually; do not run the broken form in a shared service.
Check your understanding
Answer these without looking back at the article.
- How do you distinguish StackOverflowError from heap exhaustion?
- Why is increasing -Xss not a complete fix for infinite recursion?
- How would you make a recursive graph traversal safe for cycles?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.