Stack Frames & StackOverflowError — Interview Questions
Reviewed by Gurusankar M.
Short Answer
Each Java thread gets its own stack (size set by -Xss, ~512KB–1MB default) holding a frame per active method call — local variables, operand stack, and the return address. Too-deep recursion overflows one thread's stack → StackOverflowError. Creating too many threads exhausts native memory for all those stacks → OutOfMemoryError: unable to create new native thread. One is per-thread depth; the other is process-wide thread count.
On this page
Concept Made Simple
Coffee Chat Question
“How do JVM thread stacks work, and what's the difference between StackOverflowError and OutOfMemoryError?”
Remember It Faster
Mind Map Answer
The stack is per thread; the heap is shared. A method call pushes a frame; returning pops it. Primitives and references live in the frame — the objects they point to live on the shared heap.
- Frame holds
- locals, operand stack, return address
- -Xss
- per-thread stack size (~512KB–1MB)
- StackOverflowError
- one thread recursed too deep
- OOM: native thread
- too many threads × stack size
This is exactly why millions of platform threads are impossible (each reserves ~1MB) and why virtual threads — with tiny, resizable, heap-stored stacks — change the game for high-concurrency I/O.
Key takeaway: StackOverflowError = go deeper than one stack allows (fix the recursion or raise -Xss); native-thread OOM = you created too many threads (pool them or use virtual threads).
Learn by Doing
Hands-on Keyboard
// StackOverflowError: unbounded recursion blows ONE thread's stack
static int depth(int n) {
return depth(n + 1); // no base case
}
public static void main(String[] a) {
try {
depth(0);
} catch (StackOverflowError e) {
System.out.println("Blew the stack: " + e);
}
}Blew the stack: java.lang.StackOverflowError
Think Beyond the Expected
What If?
Your service dies with 'OutOfMemoryError: unable to create new native thread' but heap looks fine — why?
Because that OOM isn't about the heap at all — it's native memory for thread stacks. Each thread reserves ~1MB (-Xss), so a few thousand threads (a leaking thread-per-request design, or an unbounded pool) exhaust the OS limit even with a healthy heap. Fix it by bounding the pool, lowering -Xss, or switching I/O-bound work to virtual threads — not by raising -Xmx.
Real World
Two of the most common production incidents map directly here: a recursive parser/serializer hitting cyclic data (StackOverflowError), and a thread-per-connection server or leaking executor exhausting native thread memory under load. Knowing which OOM you're looking at tells you whether to fix code depth or thread count.
Interviewer's Expectation
Keywords they're listening for:
- per-thread stack, shared heap
- frame = locals + operand stack
- StackOverflowError = depth
- native-thread OOM = thread count
- virtual threads change the math
Common Mistakes
- Confusing StackOverflowError with heap OutOfMemoryError
- Raising -Xmx to fix a native-thread OOM
- Ignoring recursion depth on untrusted/cyclic input
Best Practices
- Bound recursion or convert deep recursion to iteration
- Cap thread pools; never create unbounded threads
- Use virtual threads for high-concurrency blocking I/O
Follow-up Questions
- How do virtual threads avoid the ~1MB stack cost?
- When would you increase -Xss instead of fixing recursion?
- What are the different OutOfMemoryError subtypes?
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: Memory (Advanced Java) Interview question: "How do JVM thread stacks work, and what's the difference between StackOverflowError and OutOfMemoryError?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair