Quick revision
The short answer
A Java thread stack holds one frame per method call in progress, and each frame holds that method's local variables, its operand stack and some bookkeeping data; locals hold primitive values and references, while the objects themselves live on the heap. StackOverflowError means one thread's call chain grew deeper than its stack, usually through unbounded recursion, while OutOfMemoryError: unable to create native thread means the operating system refused to create another thread at all, because of process or container limits or a lack of memory for its stack.
What to remember
In this answer, we saw that a thread stack holds one frame per active method call, with locals, an operand stack and frame data, and that frames hold references while objects live on the heap. StackOverflowError means one call chain was too deep for its stack, controlled by -Xss, while unable to create native thread means the OS refused a new thread because of pid limits, ulimit or memory. Virtual threads keep their frames on the heap while unmounted, and PrintFlagsFinal shows the default stack size.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A Java thread stack holds one frame per method call in progress, and each frame holds that method's local variables, its operand stack and some bookkeeping data; locals hold primitive values and references, while the objects themselves live on the heap. StackOverflowError means one thread's call chain grew deeper than its stack, usually through unbounded recursion, while OutOfMemoryError: unable to create native thread means the operating system refused to create another thread at all, because of process or container limits or a lack of memory for its stack.
Why this question is asked
Both errors mention running out of something, so people often confuse them, and the fixes are opposite: one needs a shallower call chain or a bigger stack, the other needs fewer threads. Interviewers ask this to see whether we know where data really lives in a running Java program. In this answer, we will recap what a stack frame contains, reproduce StackOverflowError, see where unable to create native thread comes from, look at how virtual threads change the picture, and find out how big a stack is by default.
1What a Stack Frame Holds
Every thread has its own stack, and every time a method is called, the JVM pushes a new frame onto it. When the method returns, its frame is popped. A frame has three parts. The local variable array holds the method's parameters and local variables. The operand stack is a small scratch area the bytecode uses to compute values, for example pushing two ints and adding them. The frame data holds things such as the return address and a link to the class's constant pool.
Notice what is not in the frame: objects. A local variable of type int holds the number itself, but a local variable of type String or List holds only a reference, a small pointer to an object on the heap. That is why two threads can share an object but never share a local variable.
2Reproducing StackOverflowError
Each thread's stack has a fixed maximum size. If a call chain is deeper than the stack can hold, the JVM throws StackOverflowError. The usual cause is recursion with no working base case.
The exact depth depends on the frame size and the JIT, so it varies between runs. The flag -Xss sets the stack size for new threads. Raising it is a valid fix when the recursion is legitimate but deep, for example parsing deeply nested JSON, but if the recursion never ends, no stack is big enough and the code must be fixed.
3Where unable to create native thread Comes From
Every platform thread in Java is backed by an operating system thread. When we call start(), the JVM asks the OS to create one and to reserve address space for its stack. If the OS says no, the JVM throws an OutOfMemoryError with the message unable to create native thread, and in recent JDKs it adds that the cause may be memory or process limits.
The OS can refuse for several reasons. The user may have hit its maximum number of processes, set by ulimit -u. In a container, the cgroup pids limit may be reached, which is common in Kubernetes where a pod may be capped at a few thousand processes and threads. Or the system may not be able to reserve memory for another stack. In every case, the problem is the number of threads, not the depth of any one of them.
| StackOverflowError | OOM: unable to create native thread | |
|---|---|---|
| What ran out | Space in one thread's stack | Permission or memory for a new thread |
| Typical cause | Unbounded or very deep recursion | Thread leak or huge pools |
| Limits involved | -Xss, frame size | ulimit -u, cgroup pids, memory |
| Fix | Base case, iteration, or bigger -Xss | Fewer threads, bounded pools |
The fix is to find out why there are so many threads: a thread dump with jcmd <pid> Thread.print shows them and their names. Bounded, shared pools, or virtual threads for blocking work, solve it.
4How Virtual Threads Change the Picture
Virtual threads, final since JDK 21, are not tied to an OS thread for their whole life. While a virtual thread runs, it is mounted on a carrier, a platform thread from a small pool. When it blocks, for example waiting on a socket, its stack frames are copied into ordinary heap objects and the carrier is freed for another virtual thread. So a million virtual threads need a million small heap objects, not a million OS stacks, and they do not count against the pids limit. A deep recursion in a virtual thread can still cause StackOverflowError, because each one has a maximum stack depth too.
5Finding the Default Stack Size
The default stack size depends on the platform. On 64-bit Linux it is usually 1 MB. We can print the value the JVM will use.
Notice that the reservation is mostly virtual memory. The OS only gives a thread real pages as its stack actually grows, so a thread that never goes deep uses far less than 1 MB of physical memory.
6Follow-up questions
Why is lowering -Xss a risky fix for running out of threads?
Because it treats the wrong thing. If the limit that was hit is the process or cgroup pids limit, smaller stacks do not help at all, since the count is the problem. Even when memory is the issue, a stack only uses physical pages for the depth it actually reaches, so lowering the reservation saves less than people expect. Meanwhile, every thread in the application, including framework code with deep call chains such as Spring proxies, Jackson or Hibernate, now has less room and may fail with StackOverflowError under rare inputs. The safer fix is to reduce the number of threads.
Are objects ever stored on the stack?
Not as Java objects. HotSpot's C2 compiler can use escape analysis to avoid creating an object that never leaves a method, keeping its fields in registers or stack slots instead, but that is an optimisation of compiled code, not a place where objects live.
Does StackOverflowError leave the application in a safe state?
It is an Error, and catching it is only safe in narrow cases, such as a parser that turns it into a clear message. The stack has unwound by the time we catch it, but any code that was half way through updating shared state may have left it inconsistent.
7Conclusion
In this answer, we saw that a thread stack holds one frame per active method call, with locals, an operand stack and frame data, and that frames hold references while objects live on the heap. StackOverflowError means one call chain was too deep for its stack, controlled by -Xss, while unable to create native thread means the OS refused a new thread because of pid limits, ulimit or memory. Virtual threads keep their frames on the heap while unmounted, and PrintFlagsFinal shows the default stack size.
Open with
A thread stack holds one frame for each method call that is in progress. Each frame has the local variable array with parameters and locals, the operand stack the bytecode uses for intermediate values, and frame data like the return address.
Then, if they want more
Locals hold primitive values and references, but the objects those references point to always live on the heap. StackOverflowError happens when one thread's call chain gets deeper than its stack, which is almost always recursion without a working base case.
The stack size is set with Xss, by default 1 MB on 64-bit Linux, and I can check it with PrintFlagsFinal and look at ThreadStackSize. OutOfMemoryError unable to create native thread is a different problem: the operating system refused to create another platform thread.
That happens when we hit the process limit from ulimit, the cgroup pids limit in a container, or there is not enough memory to reserve another stack.
So for that one I look at the thread count, usually with a thread dump, and fix leaks or unbounded pools, rather than shrinking Xss, which risks stack overflows and often does not help.
Virtual threads help here because they keep their frames in heap objects while they are unmounted, so we can have very many without one OS stack each.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is stored on a Java thread stack, and what causes StackOverflowError compared with...": https://scrivio-interview-library.netlify.app/java/memory-layout/967706818e/
Java fundamentals, question 15 of 16 Back to the path
More in Memory areas and object layout