Quick revision
The short answer
We can tune a garbage collector for three goals: throughput (how much of the CPU time goes to our own code instead of GC), latency (how long and how often the application is paused or stalled), and footprint (how much memory the heap and the collector's own structures take). We cannot maximise all three because each one is paid for with the others: shorter pauses need more concurrent work and more spare heap, and a smaller heap means GC runs more often.
What to remember
In this answer, we saw that a garbage collector can be tuned for throughput, latency or footprint, and that these goals pull against each other. Shorter pauses need concurrent work, which costs CPU through barriers and collector threads, and headroom, which costs memory. A smaller heap makes GC run more often. The way through is to start from the service's actual requirement, pick the collector that suits that corner of the triangle, and measure all three goals after each change.
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.
We can tune a garbage collector for three goals: throughput (how much of the CPU time goes to our own code instead of GC), latency (how long and how often the application is paused or stalled), and footprint (how much memory the heap and the collector's own structures take). We cannot maximise all three because each one is paid for with the others: shorter pauses need more concurrent work and more spare heap, and a smaller heap means GC runs more often.
Why this question is asked
Every JVM flag discussion eventually comes down to this triangle, so it is worth understanding before touching any setting. Interviewers ask it because it shows whether we tune with a goal in mind or copy flags from a blog post. A batch job, a trading gateway and a small sidecar want very different things from the same collector. In this answer, we will define each goal with numbers, see why improving one costs another, look at how the HotSpot collectors sit on the triangle, and finish with how to pick the goal for a real service.
1A Quick Recap of What a Collector Does
A garbage collector finds objects that the program can no longer reach and gives their memory back. To do that it has to trace the object graph starting from roots such as thread stacks and static fields, and it often moves the surviving objects so that free memory stays in one block. All of this is work, and the work has to happen somewhere: either while our threads are stopped, which we call a pause, or at the same time as our threads, which we call concurrent work.
So the question is never whether GC costs something. The question is where we let the cost land: in CPU time, in pause time, or in memory. That is exactly what the three tuning goals describe.
2The Three Goals, Measured
Throughput is the fraction of total time the application spends running its own code. If a service runs for 100 seconds and the collector uses 3 seconds of that, throughput is 97 percent. Batch jobs care about this most, because they only care when the whole job finishes.
Latency is about pauses and stalls: how long each one lasts and, more importantly, how they are distributed. An average pause of 5 ms means little if the p99.9 pause is 400 ms. Request-serving services care about latency because a pause adds directly to the response time of every request in flight. Notice that latency also includes stalls caused by concurrent collectors, for example a thread that has to wait because the collector could not free memory fast enough.
Footprint is how much memory the process uses for the heap plus the collector's own data, such as remembered sets in G1 or the extra space a copying collector needs. Footprint matters when we pay per gigabyte, pack many containers onto a node, or run small sidecars.
| Goal | What we measure | Who cares most |
|---|---|---|
| Throughput | percent of time not spent in GC, e.g. 97% | batch jobs, data pipelines |
| Latency | pause and stall length at p99 and p99.9 | APIs, trading, interactive services |
| Footprint | heap plus GC structures, e.g. 2.4 GB RSS | dense containers, sidecars, small pods |
3Why Improving One Costs Another
Take latency first. To make pauses shorter, a collector does more of its work concurrently, while our threads keep running. But concurrent work is not free. The collector needs barriers, small pieces of code the JIT adds to reads or writes of references so the collector can track changes, and those barriers slow our code a little. The collector threads also use CPU cores that our code could have used. So shorter pauses usually mean lower throughput.
Concurrent collection also needs headroom. While the collector is still marking, the application keeps allocating. If the heap fills up before the cycle finishes, the collector has to stop everything or stall threads. To avoid that, we give it spare memory, so shorter pauses usually mean a larger footprint too.
Now take footprint. If we shrink the heap, garbage fills it faster, so the collector runs more often. Each run costs CPU, and with less free space each run also finds less to reclaim relative to the live data it must trace. So a smaller footprint usually means lower throughput and often more pauses. The reverse is also true: a bigger heap lets GC run less often, which helps throughput, but with a stop-the-world old collection a bigger heap can mean a longer pause when it finally happens.
Notice the pattern in the example: every step toward shorter pauses bought it with more CPU and more memory. The exact numbers vary by workload, which is the reason we measure our own service instead of trusting a table.
4Where the HotSpot Collectors Sit
The collectors in current JDKs are designed for different corners of the triangle. Parallel GC stops the application and uses all cores to collect as fast as possible, which gives the best throughput and a small footprint but pauses that grow with the heap. G1, the default, collects the heap in regions and aims for a pause-time target (200 ms by default, set with MaxGCPauseMillis), balancing all three. ZGC, generational since JDK 21 and the only mode since JDK 24, does almost everything concurrently and keeps pauses well under a millisecond, paying with CPU and headroom. Serial GC uses one thread and the least memory, which suits tiny containers.
| Collector | Strength | What it costs |
|---|---|---|
| Serial | smallest footprint, simple | long pauses on bigger heaps |
| Parallel | best throughput | pauses grow with live data |
| G1 (default) | balanced, pause target | some barrier and remembered-set cost |
| ZGC | sub-millisecond pauses | more CPU and memory headroom |
5Choosing the Goal Before Choosing Flags
The practical rule is to start from the service's requirement, not from the collector. If the requirement is that a nightly job finishes within an hour, the goal is throughput, and Parallel GC with a generous heap is a good first test. If the requirement is a p99 response time of 50 ms, the goal is latency, so we need pauses well under that budget and we choose G1 with a pause target or ZGC. If we must run 40 pods on a node, footprint is the constraint and we accept some throughput loss.
Once the goal is clear, we pick the collector, set the heap size, and change one setting at a time while measuring all three goals. Measuring all three matters, because a change that improves the goal we care about may quietly hurt another one we also need.
6Follow-up questions
Which goal does the default G1 collector optimise for?
G1 tries to balance all three, with a pause-time goal as its main control. By default it aims for pauses of about 200 ms and adapts the size of the young generation to meet that goal, while keeping throughput reasonable. If we lower the goal, it collects smaller pieces more often, which costs some throughput.
Can adding more heap ever make latency worse?
Yes, with a collector whose old-generation collection stops the world, such as Parallel GC. A bigger heap means collections happen less often, but each full collection has more live data to mark and compact, so the pause is longer. With concurrent collectors such as ZGC, more heap usually helps latency because it gives the collector more headroom.
Is GC CPU usage the same as throughput loss?
Mostly, but not entirely. Throughput loss also includes the cost of barriers inside our own code and cache effects from moving objects. So a collector can show modest GC thread time and still slow the application slightly. That is why we compare end-to-end requests per second or job duration, not only GC logs.
7Conclusion
In this answer, we saw that a garbage collector can be tuned for throughput, latency or footprint, and that these goals pull against each other. Shorter pauses need concurrent work, which costs CPU through barriers and collector threads, and headroom, which costs memory. A smaller heap makes GC run more often. The way through is to start from the service's actual requirement, pick the collector that suits that corner of the triangle, and measure all three goals after each change.
Open with
There are three things we can tune a collector for. Throughput is the share of time spent running application code instead of GC, so 97 percent throughput means three percent goes to the collector.
Then, if they want more
Latency is the length and distribution of pauses and stalls, and what matters is the tail, p99 or p99.9, not the average. Footprint is how much memory the heap and the collector's own structures use. We cannot have the best of all three because the work has to land somewhere.
If we want short pauses, the collector does its work concurrently, which needs barriers and background threads that cost CPU, and it needs spare heap so the application does not run out of memory during a cycle. If we shrink the heap, GC runs more often, which costs throughput.
So the first step is always the service requirement: a batch job wants throughput, so Parallel GC; an API with a 50 millisecond p99 wants latency, so G1 with a pause target or ZGC; dense small containers want footprint.
Then I pick the collector and heap size, change one thing at a time, and measure all three goals, because improving one usually moves the others.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 24
- 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 "What are the main goals you can tune a garbage collector for, and why can you not...": https://scrivio-interview-library.netlify.app/java/garbage-collection/7104e12b2e/