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.
βCoffee Chat Question
Concept Made Simple
βHow do JVM thread stacks work, and what's the difference between StackOverflowError and OutOfMemoryError?β
π§ Mind Map Answer
Remember It Faster
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.
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).
β¨οΈHands-on Keyboard
Learn by Doing
// 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
π₯What If?
Think Beyond the Expected
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:
β οΈ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
- 1How do virtual threads avoid the ~1MB stack cost?
- 2When would you increase -Xss instead of fixing recursion?
- 3What are the different OutOfMemoryError subtypes?
π§©Related Technologies
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?
β Featured Products
Support our platform by exploring our recommended products.
As an Amazon affiliate, purchases through these links may earn us a small commission β at no extra cost to you. It helps keep Full Stack Interview Guru free.