Medium👤 3-5 years👤 8-15 years 2 min read

Stack Frames & StackOverflowError — Interview Questions

Asked inAmazonMicrosoftDeloitteOracle
#stack#stackoverflowerror#outofmemoryerror#recursion#xss
Report issue

⚡ 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.

Frame holdslocals, operand stack, return address
-Xssper-thread stack size (~512KB–1MB)
StackOverflowErrorone thread recursed too deep
OOM: native threadtoo 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).

⌨️Hands-on Keyboard

Learn by Doing

java
// 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);
    }
}
Output
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:

per-thread stack, shared heapframe = locals + operand stackStackOverflowError = depthnative-thread OOM = thread countvirtual 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

  • 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

-Xssvirtual threadsJFRthread dumps

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.
Open inChatGPTGeminiClaude

Was this answer helpful?

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.

Related Questions