Quick revision
The short answer
Because CPU is the expensive resource and memory per core keeps getting cheaper, our GC strategy should judge collectors by cost per request, including the CPU the collector itself burns, and not by pause times alone. Concurrent collectors like ZGC spend background CPU on every core, while extra heap headroom lets any collector work less often, so spending memory to save CPU is often the better deal. Since JDK releases keep shifting this trade-off, we review the strategy at every LTS with fleet data and allow collector exceptions only where a measured need justifies them.
What to remember
In this answer, we saw that cloud pricing makes CPU the expensive resource, and that concurrent collectors spend CPU and memory headroom to shorten pauses. Since extra heap lets a collector work less often, buying memory to save CPU is often worthwhile. The right metric is cost per request at the required latency, with GC CPU included. Because JDK releases such as generational ZGC keep shifting the trade-off, we rerun the comparison at every LTS with fleet data and keep exceptions only where a measured need justifies them.
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.
Because CPU is the expensive resource and memory per core keeps getting cheaper, our GC strategy should judge collectors by cost per request, including the CPU the collector itself burns, and not by pause times alone. Concurrent collectors like ZGC spend background CPU on every core, while extra heap headroom lets any collector work less often, so spending memory to save CPU is often the better deal. Since JDK releases keep shifting this trade-off, we review the strategy at every LTS with fleet data and allow collector exceptions only where a measured need justifies them.
Why this question is asked
GC choices used to be made by engineers chasing pause times, and once made they stayed for years. At fleet scale that is expensive: the collector decides how many cores and gigabytes we buy. Interviewers ask this at a senior level because it tests whether we can connect a technical trade-off to a business cost and build a decision process that stays correct as both prices and the JDK change. In this answer, we will recap what concurrent collectors cost, look at the economics of CPU and memory, define the metric we should decide with, see how JDK versions move the picture, and set up a review process that keeps the strategy current.
1A Quick Recap of What Concurrent Collectors Pay
A concurrent collector, such as ZGC or Shenandoah, does its marking and moving of objects while the application threads keep running. That is how it keeps pauses below a millisecond. The price is paid in two currencies. First, CPU: the collector's own threads run in the background on our cores, and our code runs barriers, small extra checks on reference loads or stores, so the collector can work safely alongside it. Second, memory: the application keeps allocating during a cycle, so the heap needs headroom, free space the collector can use while it catches up. Without enough headroom, threads stall waiting for memory.
A stop-the-world collector like Parallel GC pays differently: it uses all cores in short bursts and very little in between, which is efficient in CPU, but every burst is a pause.
2The Economics: CPU Per Core, Memory Getting Cheaper
Cloud instances are priced mainly by vCPU, and the amount of memory offered per vCPU has grown over time, with memory-optimised families offering 8 GB per vCPU or more. For many services the binding constraint is CPU: we add pods because cores are saturated, while memory sits partly unused.
That changes the arithmetic. Suppose a service runs 200 pods of 2 vCPU each, and its collector uses 8 percent of CPU. That is 32 cores doing GC. If doubling the heap from 4 GB to 8 GB per pod lets the collector run half as often, GC CPU drops to about 4 percent and we free 16 cores, which may let us remove 8 pods. If the extra 800 GB of memory costs less than those cores, spending memory to save CPU is the right call. The numbers vary by provider and contract, which is exactly why the decision should be made with our own prices.
| Option | Heap per pod | GC CPU | Net effect |
|---|---|---|---|
| ZGC, tight heap | 4 GB | 10% | short pauses, most cores, risk of stalls |
| ZGC, more headroom | 8 GB | 5% | short pauses, fewer cores, more memory |
| G1, pause goal 50 ms | 6 GB | 4% | pauses within budget, lowest total cost here |
3Deciding With Cost Per Request
So the metric for choosing collectors and heap sizes should be cost per request, or per unit of work, at the latency the service must meet. We compute it as the total cost of cores and memory for the service divided by the requests it served, and we include GC CPU in that cost rather than treating it as invisible overhead. Pause time is a constraint, not the goal: the service needs its p99 within budget, and among the configurations that meet it we pick the cheapest.
Notice how this changes conversations. A team asking for ZGC because it has the shortest pauses has to show that their latency budget needs it. If G1 meets their 100 ms p99 at lower cost, G1 wins. If their budget is 10 ms, ZGC is justified and its extra CPU is simply the price of the requirement.
4Why the Trade-Off Moves With JDK Versions
Collector behaviour is not fixed. Generational ZGC, added in JDK 21 and the only ZGC mode from JDK 24, collects young objects more often and old ones rarely, which greatly reduced its CPU and memory overhead compared with the original single-generation design and narrowed the throughput gap with G1. G1 keeps improving too, in remembered set size and region handling. So a comparison done on JDK 17 may give the wrong answer on JDK 25. A strategy that records only the conclusion, use G1, will drift out of date; a strategy that records the measurement method can be rerun.
5Keeping the Strategy Current
We tie the review to the JDK release cycle. At every LTS release, and at least once a year, the platform team reruns a small benchmark set on representative services, compares cost per request for the standard profiles, and updates the defaults. Fleet data from production, GC CPU share, heap use after collection and pause distributions, shows whether the defaults still fit.
Exceptions are allowed, but each is tied to a measured need and has an owner and a review date. A service running ZGC records which latency requirement it meets that G1 did not; a service with a large heap records the live set that requires it. When the reason disappears, the exception goes away. This keeps the fleet simple without forbidding the cases that really need something different.
- New LTS or yearly checkpointtrigger the review
- Rerun benchmarks on representative servicescost per request at required latency
- Compare with fleet production dataGC CPU, live set, pause tails
- Defaults still cheapest?Yeskeep them, record the evidenceNoupdate profiles and roll out by canary
- Review each exceptionneed still measured? owner still there?
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Is ZGC ever cheaper than G1?
It can be. If the alternative to ZGC is running G1 with many extra pods to keep tail latency down, or if a service's G1 pauses cause timeouts and retries that cost CPU elsewhere, ZGC's background CPU may be cheaper overall. With generational ZGC the overhead is smaller than it used to be, so we measure rather than assume.
How do you get teams to accept fleet defaults?
By making the defaults evidence-based and easy to leave when there is a real reason. The platform publishes the benchmark data and the cost per request for each profile. A team that needs an exception writes down the measured need and gets it, with a review date. Most teams are happy not to own GC tuning at all.
What metrics would you put on the fleet dashboard?
GC CPU share per service, converted into cores and money; heap occupancy after collection, which approximates the live set; pause and stall percentiles; allocation rate; and the JDK version and collector in use. Together they show where cost is, where risk is, and which services lag behind the current defaults.
7Conclusion
In this answer, we saw that cloud pricing makes CPU the expensive resource, and that concurrent collectors spend CPU and memory headroom to shorten pauses. Since extra heap lets a collector work less often, buying memory to save CPU is often worthwhile. The right metric is cost per request at the required latency, with GC CPU included. Because JDK releases such as generational ZGC keep shifting the trade-off, we rerun the comparison at every LTS with fleet data and keep exceptions only where a measured need justifies them.
Open with
I would make cost per request the deciding metric, with latency as a constraint. Concurrent collectors such as ZGC keep pauses tiny, but they pay with background CPU threads and barriers on every core, and they need heap headroom.
Then, if they want more
Since CPU is what we pay most for and memory per core keeps growing, spending memory to save CPU is often the better trade: a bigger heap means the collector runs less often, so it frees cores.
For example, if doubling the heap halves GC CPU from 8 to 4 percent across 200 pods, that can remove several pods, and we check whether the memory costs less than the cores. So for each workload class we compare configurations that meet the latency budget and pick the cheapest, counting GC CPU as part of the cost.
The trade-off is not fixed: generational ZGC in JDK 21 narrowed the throughput gap with G1, and later releases keep moving it. So I would review the strategy at every LTS release, rerun a benchmark set on representative services and compare with production fleet data, and update the defaults.
Exceptions stay allowed, but each one is tied to a measured need, has an owner and a review date.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 17, Java 21, 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
- 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 "Cloud providers price CPU per core while memory per core keeps growing, and concurrent...": https://scrivio-interview-library.netlify.app/java/garbage-collection/653f308807/