Scrivio Interview library
ScenarioAdvanced6 min read · updated 12 November 2026

An organisation plans to cut memory across 200 services by moving request handling from large platform thread pools to virtual threads. What memory effects do you expect, and how do you roll it out?

We should expect native memory to drop, because large pools of platform threads with their own OS stacks go away, but heap use and GC work may rise, because virtual thread stacks live on the heap while they wait, and ThreadLocal values can multiply. Unbounded concurrency also moves pressure onto downstream systems, so we keep explicit limits. We roll out service by service on a JDK where synchronized no longer pins, measuring resident memory, heap after GC and GC CPU before and after each step.

Why this question is asked

Moving to virtual threads is one of the most attractive changes in modern Java, because it promises simpler code and lower memory with a configuration switch. At the scale of 200 services, though, the effects are not uniform: some services shrink a lot, some see no change, and a few get worse. Interviewers ask this to see whether we understand where the memory actually goes and whether we can plan a rollout that proves the benefit instead of assuming it. In this answer, we will recap how virtual threads use memory, look at the savings and the new costs, see why ThreadLocal and unbounded concurrency are the main risks, and then plan the rollout.

1A Quick Recap of Where Thread Memory Goes

A platform thread is a thin wrapper around an operating system thread. Each one reserves its own native stack, 1 MB by default on 64-bit Linux, and the pages it touches as calls go deep stay resident. A Tomcat pool of 200 threads that each touch 200 KB holds about 40 MB of stacks, and services with several pools of hundreds of threads can hold a few hundred megabytes.

A virtual thread is managed by the JVM instead. It runs on a carrier, one of a small pool of platform threads, usually one per CPU core. When it blocks on I/O, the JVM copies its frames into a heap object, a stack chunk, and frees the carrier for another virtual thread. When the I/O completes, the frames are mounted again on some carrier and execution continues.

Step throughA virtual thread waiting on I/O
Virtual threadCarrier threadHeap
mounted: runs the request handler
blocks on a socket read
frames copied into a stack chunk
carrier picks up another virtual thread
data arrived: frames mounted again
handler continues where it stopped

6 messages in order. Play them, or step through them one at a time.

While it waits, the virtual thread costs heap memory proportional to its stack depth, not an OS stack.

2What We Expect to Save, and What We Expect to Pay

The saving is native memory. Replacing request pools with virtual threads removes hundreds of OS threads per service, with their stacks, their kernel structures and some of their malloc arenas. For a service with 800 pool threads, resident memory often drops by 100 to 300 MB.

The new cost is on the heap. Every waiting virtual thread holds its frames in a stack chunk, typically a few kilobytes but more for deep frameworks. With 200 platform threads we could never have more than 200 requests in flight. With virtual threads we might have 5,000, and 5,000 stack chunks of 10 KB each are 50 MB of heap that the collector must scan and eventually reclaim. So heap after GC and GC CPU may rise, especially in services with high concurrency and deep call stacks.

Expected effects per service
MeasureExpected directionWhy
Resident memory (RSS)DownFewer OS threads and native stacks
Heap after GCUp, sometimesStack chunks of waiting virtual threads
GC CPUUp, sometimesMore short-lived heap objects to scan
ThreadLocal memoryUp, sometimes a lotOne value per virtual thread, not per pool thread
Downstream loadUp if unboundedMore requests in flight at once

3Why ThreadLocal Becomes Expensive

Many libraries cache objects in ThreadLocal on the assumption that there are only a few hundred long-lived threads. A date formatter, a serialization buffer or a per-thread connection context is created once per pool thread and reused for thousands of requests. With virtual threads, there is one thread per task, so the same code creates a new value for every request and keeps it for as long as the virtual thread lives.

Java
private static final ThreadLocal<byte[]> BUFFER =
ThreadLocal.withInitial(() -> new byte[64 * 1024]); // 64 KB per thread
// Platform pool of 200 threads: 200 x 64 KB = 12.5 MB, reused forever
// 10,000 concurrent virtual threads: 10,000 x 64 KB = 625 MB of short-lived garbage
// Better: pass the context explicitly, pool the buffer, or use ScopedValue (final in JDK 25)
static final ScopedValue<RequestContext> CTX = ScopedValue.newInstance();
ScopedValue.where(CTX, ctx).run(() -> handle(request));

Notice that the cost is not a leak; the values die with their threads. It is pure allocation and GC churn, and it can wipe out the native memory saving. So before switching a service, we search its code and its libraries for heavy ThreadLocal use.

4Keeping Concurrency Bounded

A thread pool was also a concurrency limit, whether or not anyone intended it. With 200 threads, at most 200 requests could call the database at once. With virtual threads, that implicit limit is gone: a traffic spike can start thousands of calls, and the pressure moves to the connection pool, to downstream services and to the heap, where all those waiting requests hold their data.

So we put the limit back on purpose, where it belongs. A Semaphore around a slow downstream call, a bounded connection pool, and rate limits at the edge keep the system predictable.

Java
private final Semaphore paymentsLimit = new Semaphore(100); // at most 100 calls in flight
PaymentResult charge(Order o) throws InterruptedException {
paymentsLimit.acquire(); // a virtual thread waits cheaply here
try { return paymentsClient.charge(o); }
finally { paymentsLimit.release(); }
}

5Rolling Out Across 200 Services

We start with the JDK. On JDK 21, a virtual thread that blocks inside a synchronized block stays pinned to its carrier, which can stall the carriers and erase the benefit. JDK 24 removed that limitation, so the fleet should be on JDK 24 or later, in practice JDK 25 LTS, before broad adoption. We can still watch for remaining pinning cases, for example in native calls, with the JFR event jdk.VirtualThreadPinned.

Then we pick a few services with large blocking pools and modest ThreadLocal use, where the gain should be clearest. For each one we record a baseline for a week: resident memory, heap after GC, GC CPU, p99 latency and downstream error rates. We switch it on, in Spring Boot with spring.threads.virtual.enabled=true, in a canary, compare the same numbers, and only then expand to the rest of the service and to similar services. Services that are CPU-bound gain nothing and can stay as they are.

Step throughRollout for each service
  1. Upgrade to JDK 24 or latersynchronized no longer pins
  2. Record a one-week baselineRSS, heap after GC, GC CPU, p99, downstream errors
  3. Check ThreadLocal use and concurrency limitsLibraries and own code
  4. Enable virtual threads in a canaryspring.threads.virtual.enabled=true
  5. Numbers better or equal?Compare with the baseline
    YesRoll out to the service, then similar onesNoRoll back and investigate

5 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Why do CPU-bound services gain little from virtual threads?

Virtual threads help when tasks spend most of their time waiting, because a waiting virtual thread frees its carrier. A CPU-bound task keeps the carrier busy, so there is no waiting time to reuse, and the number of carriers equals the number of cores anyway.

Should we pool virtual threads?

No. They are cheap to create and are meant to be one per task. Pooling them brings back the old limits without the benefits. If we need to limit concurrency, a Semaphore or a bounded resource pool is the right tool.

What happens to existing thread dumps and monitoring?

Thread dumps through jstack do not list every virtual thread in the classic format; jcmd <pid> Thread.dump_to_file -format=json gives a full picture. Metrics that counted busy pool threads as a proxy for load lose their meaning, so dashboards should switch to in-flight request counts.

7Conclusion

In this answer, we saw that moving to virtual threads lowers native memory by removing OS stacks, but can raise heap use and GC work, because waiting virtual threads keep their frames on the heap and ThreadLocal values multiply. Unbounded concurrency moves pressure to downstreams, so explicit limits stay. A safe rollout upgrades to a JDK where synchronized does not pin, measures resident memory, heap after GC and GC CPU per service, and expands from canaries to similar services.

What to say in the interviewAbout 80 seconds aloud

Open with

I would expect two effects in opposite directions. Native memory goes down, because large pools of platform threads, each with its own OS stack, disappear, and for a service with several hundred pool threads that is often 100 to 300 MB of resident memory.

Then, if they want more

But heap use and GC work can go up, because a virtual thread that is waiting keeps its frames in a heap stack chunk, and with no pool limit we may have thousands of requests in flight.

ThreadLocal is the biggest risk: code that cached a buffer per pool thread now creates one per virtual thread, so I would audit ThreadLocal use and prefer passing context explicitly or ScopedValue. I would also keep explicit concurrency limits, with semaphores or bounded connection pools, because the old pool was silently protecting downstreams.

For the rollout, I would first get the fleet to JDK 24 or later, ideally 25, because from JDK 24 synchronized no longer pins virtual threads to carriers.

Then I would go service by service, starting with I/O-heavy ones: a week of baseline for RSS, heap after GC, GC CPU and p99, a canary with virtual threads enabled, a comparison, and only then a wider rollout. CPU-bound services I would leave alone.

About this answer

Last updated
12 November 2026
Versions it names
Java 24, Java 25, 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 "An organisation plans to cut memory across 200 services by moving request handling from...": https://scrivio-interview-library.netlify.app/java/memory-layout/fca411e7ea/