Quick revision
The short answer
For the REST API with a 2 GB heap and a 100 ms p99 target, G1 with default settings is usually enough: pauses on a 2 GB heap are typically well under 100 ms, and we can lower its 200 ms pause target if needed. For the 64 GB cache with a 5 ms p99.9 target, generational ZGC, or Shenandoah where the build has it, keeps pauses short regardless of heap size, with extra CPU and headroom budgeted. For the 1 vCPU, 256 MB sidecar, Serial GC has the smallest footprint and suits one CPU. In every case we validate under production-like load and first confirm that GC, not locks, I/O or downstream calls, is the real source of tail latency.
What to remember
In this answer, we matched three services to collectors. The 2 GB REST API with a 100 ms p99 target fits G1 with defaults, perhaps with a lower pause target. The 64 GB cache with a 5 ms p99.9 target needs generational ZGC or Shenandoah, with extra CPU and headroom budgeted. The 1 vCPU, 256 MB sidecar fits Serial, the cheapest collector. We validate each choice under production-like load, measuring pauses, stalls, CPU and throughput, and first confirm that GC is the real source of tail latency.
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.
For the REST API with a 2 GB heap and a 100 ms p99 target, G1 with default settings is usually enough: pauses on a 2 GB heap are typically well under 100 ms, and we can lower its 200 ms pause target if needed. For the 64 GB cache with a 5 ms p99.9 target, generational ZGC, or Shenandoah where the build has it, keeps pauses short regardless of heap size, with extra CPU and headroom budgeted. For the 1 vCPU, 256 MB sidecar, Serial GC has the smallest footprint and suits one CPU. In every case we validate under production-like load and first confirm that GC, not locks, I/O or downstream calls, is the real source of tail latency.
Background to this question
Choosing a collector is a matching exercise: what the service needs, what each collector costs, and how much hardware we have. This question gives three very different services and asks us to match each one, which is exactly what a platform engineer does when reviewing deployments. In this answer, we will recap the trade-offs between the collectors, work through each case with numbers, and then look at how to validate the choices and why GC is not always the culprit.
1A Quick Recap of the Trade-offs
Every collector balances three things: latency, meaning how long a single pause lasts; throughput, the share of CPU left to the application; and footprint, the memory and CPU the collector itself needs. Serial is cheapest but pauses grow with live data. G1 is the balanced default with pause targets, typically tens of milliseconds. Generational ZGC and Shenandoah move nearly all work out of pauses, keeping them around a millisecond or less, but need more CPU and heap headroom.
So the questions for each service are: how strict is the tail latency target compared with what each collector can hold, how large is the heap, and how much CPU and memory can we spend on GC.
| Service | Choice | Main reason |
|---|---|---|
| (a) REST API, 2 GB, p99 100 ms | G1, defaults | pauses on 2 GB are typically 10-50 ms |
| (b) Cache, 64 GB, p99.9 5 ms | Generational ZGC | pauses stay under 1 ms on any heap |
| (c) Sidecar, 1 vCPU, 256 MB | Serial | smallest footprint, no GC threads to compete |
2Case (a): The REST API
A 2 GB heap is small for G1. Young pauses copy only what survives, and for a typical REST service, where most objects live for one request, that is a few tens of megabytes, which G1 copies in 10 to 50 ms on a few cores. The 100 ms p99 target leaves room.
G1's default pause target is 200 ms, above our budget. On a 2 GB heap G1 rarely gets near it, because the heap limits how big the young generation can grow, but setting the target closer to the budget makes our intention explicit and steers G1 if the heap grows later.
Notice that we added almost nothing. With G1, fewer flags are usually better, because each fixed setting removes something G1 would otherwise adapt.
3Case (b): The 64 GB Cache
A 64 GB in-memory cache is the hard case for G1. The live set is large and long-lived, so old regions are full of live data, mixed collections copy a lot, and remark pauses and occasional evacuation trouble can reach hundreds of milliseconds. A p99.9 of 5 ms means only one request in a thousand may exceed 5 ms; even a single 50 ms pause every few seconds breaks that.
Generational ZGC's pauses do not grow with heap or live data and stay well under a millisecond, so it is the natural choice. Shenandoah is equally suitable if every build the service runs on includes it.
We must budget for the costs. ZGC needs headroom so the application can keep allocating during a cycle; with, say, 45 GB live, a 64 GB heap gives 19 GB of room, which we should check against the allocation rate. It uses more CPU for its concurrent threads, so the instance needs spare cores. And because ZGC does not use compressed oops, the cache may take more memory than it did under G1, which matters when sizing the heap.
4Case (c): The Sidecar
A sidecar with 1 vCPU and 256 MB has a tiny heap, perhaps 128 MB, and one CPU. With one CPU, any concurrent GC thread directly takes time from the application, so G1's and ZGC's concurrent work brings no benefit. Their extra memory for remembered sets, regions and headroom is also expensive at this size.
Serial has the lowest overhead of all: no concurrent threads, the cheapest barrier, almost no extra memory. A young collection on a 128 MB heap takes a few milliseconds. The JVM would pick Serial here by itself, because 1 CPU is not server-class, but setting -XX:+UseSerialGC explicitly keeps the choice stable if the limits change.
5Validating the Choices
Each choice is a hypothesis until it is measured. We run each service under production-like load, ideally a canary receiving real traffic, and measure four things: pause times from the GC log, allocation stalls for the ZGC service, GC CPU usage, and throughput or request rate at the target latency. For the cache, we also confirm headroom by watching heap usage over the peak hour.
Most important, we check that GC is the real latency source. A p99 of 100 ms on the API may come from database calls, a connection pool that runs dry, lock contention or a slow downstream service. We compare slow request traces with GC pause timestamps: if slow requests line up with pauses, GC matters; if they do not, changing the collector will not move p99 at all.
6Follow-up questions
What would make you switch (a) to ZGC?
Measurements showing that G1 pauses, not other causes, push p99 past 100 ms even after tuning, for example because the heap grew to 16 GB with a large live cache, or because the latency target tightened to 10 or 20 ms. A big growth in live data or a tighter budget are the usual triggers. I would then canary generational ZGC, confirm that p99 improves, and accept the extra CPU and heap it needs.
Why not use ZGC everywhere to be safe?
Because it is not free. For the API, ZGC would spend more CPU and memory without improving a target that G1 already meets. For the sidecar, its concurrent threads and headroom would take a large share of one CPU and 256 MB. Choosing the cheapest collector that meets the requirement keeps costs down across the fleet.
Is Parallel GC ever right for these services?
Not for these three. Parallel maximises throughput with stop-the-world pauses that grow with live data, which suits batch jobs. The API and the cache both have latency targets, and the sidecar has only one CPU, so parallel GC threads bring nothing.
7Conclusion
In this answer, we matched three services to collectors. The 2 GB REST API with a 100 ms p99 target fits G1 with defaults, perhaps with a lower pause target. The 64 GB cache with a 5 ms p99.9 target needs generational ZGC or Shenandoah, with extra CPU and headroom budgeted. The 1 vCPU, 256 MB sidecar fits Serial, the cheapest collector. We validate each choice under production-like load, measuring pauses, stalls, CPU and throughput, and first confirm that GC is the real source of tail latency.
Open with
For the REST API with a 2 GB heap and a 100 millisecond p99, I would use G1 with default settings. On a 2 GB heap young pauses for a request-oriented service are typically 10 to 50 milliseconds, so the budget is comfortable; I might set MaxGCPauseMillis to around 50 to make the intent explicit, but otherwise I would not add flags.
Then, if they want more
For the 64 GB in-memory cache with a 5 millisecond p99.9, G1 is the wrong tool, because a large long-lived live set means mixed and remark pauses that can reach hundreds of milliseconds. I would use generational ZGC, or Shenandoah if every build we run includes it, since their pauses stay under a millisecond regardless of heap size.
I would budget extra CPU for concurrent GC threads and check that the heap headroom covers the allocation rate during a cycle, and remember ZGC has no compressed oops. For the sidecar with one vCPU and 256 MB, Serial: smallest footprint, no concurrent threads competing for the single CPU, and short pauses on a tiny heap.
The JVM would pick it anyway, but I would set it explicitly. Then I would validate all three under production-like load, measuring pauses, allocation stalls, CPU and throughput, and check that GC really is the latency source, since locks, I/O and downstream calls often dominate p99.
About this answer
- Last updated
- 12 November 2026
- 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
- HotSpot Garbage Collection Tuning Guide, JDK 21
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 "Choose a collector for each case and justify it: (a) a REST API with a 2 GB heap and p99...": https://scrivio-interview-library.netlify.app/java/garbage-collection/12ca0c1f85/