Quick revision
The short answer
A virtual thread's stack is stored as heap objects that grow with call depth, so many virtual threads with deep stacks cost real heap, and ThreadLocal values exist per virtual thread, so per-thread caches multiply with concurrency. Traditional thread dumps such as jstack do not list virtual threads; jcmd <pid> Thread.dump_to_file -format=json <file> does. JFR has virtual-thread events such as jdk.VirtualThreadPinned and jdk.VirtualThreadSubmitFailed, and because thread counts no longer measure saturation, we watch downstream pools, queues and latency instead.
What to remember
In this answer, we saw that virtual thread stacks live on the heap as objects sized by actual call depth, so many blocked threads with deep stacks cost heap and GC work, and that ThreadLocal caches multiply with concurrency because each task is a new thread. jstack does not list all virtual threads, while jcmd <pid> Thread.dump_to_file -format=json <file> does. JFR adds jdk.VirtualThreadPinned and jdk.VirtualThreadSubmitFailed, and since thread counts no longer signal saturation, we watch connection pools, bulkhead rejections, latency and heap instead.
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 virtual thread's stack is stored as heap objects that grow with call depth, so many virtual threads with deep stacks cost real heap, and ThreadLocal values exist per virtual thread, so per-thread caches multiply with concurrency. Traditional thread dumps such as jstack do not list virtual threads; jcmd <pid> Thread.dump_to_file -format=json <file> does. JFR has virtual-thread events such as jdk.VirtualThreadPinned and jdk.VirtualThreadSubmitFailed, and because thread counts no longer measure saturation, we watch downstream pools, queues and latency instead.
Why this question is asked
Moving a service to virtual threads changes more than throughput. Memory moves from fixed OS stacks to the Java heap, familiar diagnostic tools show only part of the picture, and dashboards built around thread pool usage stop meaning what they used to. Teams that miss this find out during their first incident, when the thread dump they rely on does not show the threads that are stuck. Interviewers ask this to see whether we have operated virtual threads, not only read about them. In this answer, we will look at where a virtual thread's memory goes, how ThreadLocal changes the picture, how to get a thread dump that includes virtual threads, which JFR events matter, and what to monitor instead of thread counts.
1Where a Virtual Thread's Memory Lives
A platform thread reserves a fixed stack outside the heap when it starts, often 1 MB of address space, whether it needs it or not. A virtual thread has no such reservation. While it is mounted, its frames run on the carrier's native stack; when it unmounts, the frames it needs are copied into stack chunk objects on the Java heap. Those objects are as large as the stack actually is at that moment, so a virtual thread blocked three frames deep costs very little, and one blocked 200 frames deep inside a framework costs much more.
So the memory cost is proportional to how many virtual threads are blocked at once times how deep their stacks are. A service with 50,000 concurrent requests, each blocked at a depth of 150 frames, can hold hundreds of megabytes of stack chunks on the heap, which the garbage collector must also scan and move. That is still far cheaper than 50,000 platform threads, but it is not free, and it shows up as heap use and GC work rather than as native memory.
- Carrier threadsa few platform threads, fixed native stacks
- Stack chunks on the heapgrows with blocked threads x stack depth
- ThreadLocal valuesone copy per virtual thread that set them
- Request objectsbuffers, DTOs, held while blocked
- Downstream resourcesconnections, sockets, often the real limit
5 layers, from the top down. Step through them one at a time.
With virtual threads, concurrency costs heap rather than native thread stacks.
2ThreadLocal Multiplies With Concurrency
ThreadLocal works with virtual threads exactly as before: each thread has its own copy of every ThreadLocal it sets. The trouble is the numbers. Code written for pools often used ThreadLocal as a cache, for example a 64 KB buffer or a formatter per thread, on the assumption that there are 200 threads, so 200 copies. With virtual threads, every task is a new thread, so the cache is created once per task and thrown away when the task ends.
Notice that nothing is wrong functionally; the code still works. It just stops saving anything and starts costing memory proportional to concurrency. The fix is to replace per-thread caches with something shared, such as a bounded pool of buffers or an immutable, thread-safe object like DateTimeFormatter. For passing context down a call chain, scoped values, final in Java 25, are designed for virtual threads and avoid the mutable, long-lived nature of ThreadLocal.
3Getting a Thread Dump That Includes Virtual Threads
jstack and the Thread.print command of jcmd were designed for platform threads, and they do not list all virtual threads. In an incident, that means the threads stuck waiting for a database may simply be missing from the dump. The JDK added a new dump command for this.
The JSON format groups threads by container, such as the virtual thread executor that created them, which helps when there are 50,000 of them. With that many threads, reading the dump by eye is impractical, so a small script that groups stacks by their top frames, and counts them, quickly shows that, for example, 41,000 threads are waiting in the same PostgreSQL read. Spring Boot's Actuator also has a thread dump endpoint; check your version for whether it includes virtual threads.
4JFR Events for Virtual Threads
Java Flight Recorder has events specific to virtual threads. jdk.VirtualThreadPinned is recorded when a virtual thread blocks while pinned to its carrier for longer than a threshold, 20 milliseconds by default, and includes the stack, so it points straight at the code responsible. jdk.VirtualThreadSubmitFailed is recorded when the scheduler could not accept a virtual thread to run, which signals a serious resource problem. jdk.VirtualThreadStart and jdk.VirtualThreadEnd exist too but are disabled by default, because at millions of threads they would produce enormous recordings.
A low, steady count of pinned events of a few milliseconds is harmless; many long ones under load explain why throughput collapses. On JDK 24 and later, after JEP 491, synchronized no longer pins in most cases, so pinned events usually point to native code.
5What to Monitor Instead of Thread Counts
With a pool of 200 threads, active threads was a good saturation metric: at 200 of 200, the service was full. With virtual threads, the number of threads simply follows the number of concurrent requests; 30,000 threads may be perfectly healthy. The JMX ThreadMXBean, which most dashboards use for thread counts, reports platform threads only, so it barely moves at all.
Saturation now shows up downstream, so that is where we look: the database connection pool's active connections and waiting requests, HTTP client pools, semaphores and bulkheads with their rejection counts, request latency percentiles and heap use. Recent JDKs also expose scheduler metrics, such as how many virtual threads are mounted or queued, through a management bean; check your version for its availability.
| Signal | Platform thread pool | Virtual threads |
|---|---|---|
| Active threads | saturation: 200 of 200 means full | follows load, not a saturation signal |
| Connection pool waiting | useful | primary saturation signal |
| Bulkhead rejections | useful | primary saturation signal |
| Heap use and GC | stable | grows with blocked threads x stack depth |
| Pinned events (JFR) | not applicable | watch duration and count under load |
6Follow-up questions
Can a deep recursion in a virtual thread cause StackOverflowError?
Yes. A virtual thread's stack can grow up to the same maximum size as a platform thread's, so very deep recursion still overflows. The difference is only that the memory is not reserved up front.
Do heap dumps show virtual threads?
A heap dump shows the stack chunk objects and the objects they reference, so memory held by blocked virtual threads is visible there. It is not as easy to read as a thread dump, which is why the new jcmd dump is the first tool for finding where threads are stuck.
Should we set a limit on the number of virtual threads?
Not on the threads themselves. We limit the resources they use: connection pools, semaphores around fragile dependencies, and request limits at the edge. Those limits keep heap use bounded as a side effect.
7Conclusion
In this answer, we saw that virtual thread stacks live on the heap as objects sized by actual call depth, so many blocked threads with deep stacks cost heap and GC work, and that ThreadLocal caches multiply with concurrency because each task is a new thread. jstack does not list all virtual threads, while jcmd <pid> Thread.dump_to_file -format=json <file> does. JFR adds jdk.VirtualThreadPinned and jdk.VirtualThreadSubmitFailed, and since thread counts no longer signal saturation, we watch connection pools, bulkhead rejections, latency and heap instead.
Open with
There are three things I'd call out. First, memory: a virtual thread's stack is copied to the heap as stack chunk objects when it unmounts, sized by its actual depth.
Then, if they want more
So it's cheap per thread, but 50,000 blocked requests at 150 frames deep is real heap and GC work. Related to that, ThreadLocal is per virtual thread, and since every task is a new thread, any ThreadLocal used as a cache, like a 64 KB buffer, is now created per request and multiplies with concurrency.
I'd replace those with shared pools or immutable objects, and use scoped values for context. Second, diagnostics: jstack and the old Thread.print don't list all virtual threads, so in an incident the stuck ones can be missing.
The command to use is jcmd with the pid, Thread.dump_to_file, with format json and a file name, and with tens of thousands of threads I'd group stacks by top frames with a script. JFR has virtual-thread events: jdk.VirtualThreadPinned, recorded when a thread is pinned for over 20 milliseconds by default, with the stack, and jdk.VirtualThreadSubmitFailed.
Third, metrics: thread count used to be a saturation signal, but now it just follows load, and ThreadMXBean only counts platform threads anyway. So I'd watch connection pool waits, bulkhead rejections, latency percentiles and heap instead.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 25
- 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.
- Official documentation
- JEP 444, Virtual Threads
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 are the memory and observability characteristics of virtual threads that matter in...": https://scrivio-interview-library.netlify.app/java/virtual-threads/0c855601db/