StackOverflowError Causes and Fixes
Fast recall
Quick revision
Core answer
StackOverflowError is thrown when a thread needs more stack space for another operation, usually because recursion never reaches a base case or valid input is deeper than the available stack can support. Fix the termination or cycle bug, or replace deep recursion with an explicit stack or iterative algorithm. Stack size and safe recursion depth depend on the JVM, options, method frames, and environment.Understanding of how the call stack works, recursion mechanics, and the difference between Error and Exception
Confusing StackOverflowError with OutOfMemoryError — they affect different memory areas and have different causes
Explain the call stack mechanism clearly and mention that tail recursion optimization (not in Java) could theoretically help, showing deeper understanding
Interview-ready guide
Interview answer
StackOverflowError means one thread exhausted the stack space available for its active call chain. Infinite recursion is a common cause, but a correct recursive algorithm can also fail when valid input is much deeper than the available stack supports. Java gives no portable safe recursion count and does not promise tail-call optimization.
For example, walk(node.next) without a terminating null check keeps adding calls until the stack is exhausted. A cycle in a graph can cause the same symptom even when the code has a simple base case. I first verify the base case and cycle handling. If very deep input is legitimate, I replace recursion with a loop or an explicit stack. Increasing the configured thread-stack size may help a measured, bounded workload, but it consumes more memory per thread and does not repair an infinite recursion bug.
StackOverflowError Causes and Fixes
Direct Answer
StackOverflowError is thrown when a thread needs more stack space for another operation, usually because recursion never reaches a base case or valid input is deeper than the available stack can support. Fix the termination or cycle bug, or replace deep recursion with an explicit stack or iterative algorithm. Stack size and safe recursion depth depend on the JVM, options, method frames, and environment.
Code Example
public class RecursionExample {
// WRONG: infinite recursion, no base case
public int badFactorial(int n) {
return n * badFactorial(n - 1); // never stops, StackOverflowError after ~1000 calls
}
// CORRECT: proper base case terminates recursion
public int goodFactorial(int n) {
if (n <= 1) { // base case: stop recursion
return 1;
}
return n * goodFactorial(n - 1);
}
// EVEN BETTER: iterative approach avoids stack depth entirely
public int iterativeFactorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result; // no stack frames accumulated
}
// ACCIDENTAL: indirect recursion can also cause it
public void methodA() {
methodB();
}
public void methodB() {
methodA(); // A calls B calls A calls B... StackOverflowError
}
}
Common Mistakes & Pitfalls
- Missing base case: Freshers write recursive methods without a condition that stops recursion. For example, a factorial method that just calls itself with n minus one but never checks if n is one or zero. The method calls itself thousands of times until the stack overflows.
- Confusing with OutOfMemoryError: StackOverflowError affects the call stack (method invocations), while OutOfMemoryError affects the heap (object storage). They're different memory areas with different causes. Creating too many objects causes OutOfMemoryError, not StackOverflowError.
- Base case that never triggers: Writing a base case like if n equals zero but calling with negative numbers, so the condition never matches and recursion continues infinitely. For example, factorial of negative five keeps subtracting one forever. Always validate inputs and ensure your base case actually gets reached.
Check your understanding
Answer these without looking back at the article.
- How deep can the Java call stack go before StackOverflowError?
- What is the -Xss JVM flag?
- How do you convert a recursive algorithm to iterative?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.