Quick revision
The short answer
As of Java 25, HotSpot's main collectors are G1, the default, which balances throughput and pause times for most services; ZGC, a low-latency collector with pauses well under a millisecond, which since JDK 24 exists only in its generational mode; Shenandoah, another low-pause collector available in many OpenJDK builds; Parallel, which maximizes throughput for batch jobs that tolerate pauses; and Serial, for small heaps and single-CPU containers. We choose by latency goal and heap size, then confirm with GC logs and load tests.
What to remember
In this answer, we saw what a garbage collector trades off, and the five main HotSpot collectors in Java 25: G1 as the balanced default, generational ZGC and Shenandoah for very short pauses, Parallel for throughput and Serial for small footprints. We choose by latency goal, heap size and workload type, and we confirm the choice with GC logs and realistic load tests.
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.
As of Java 25, HotSpot's main collectors are G1, the default, which balances throughput and pause times for most services; ZGC, a low-latency collector with pauses well under a millisecond, which since JDK 24 exists only in its generational mode; Shenandoah, another low-pause collector available in many OpenJDK builds; Parallel, which maximizes throughput for batch jobs that tolerate pauses; and Serial, for small heaps and single-CPU containers. We choose by latency goal and heap size, then confirm with GC logs and load tests.
Why this question is asked
Garbage collection decides how the JVM frees memory, and the choice of collector affects latency, throughput, CPU use and memory footprint. Most services run happily on the default, but knowing the alternatives matters when a service has strict latency goals, a very large heap, or a tiny container. Interviewers ask this to see whether we know the current options rather than the ones from Java 8, and whether we choose by measurement instead of by habit. In this answer, we will recap what a collector does and the trade-off between pauses and throughput, go through each collector, and then lay out how we would choose and validate.
1A Quick Recap: What a Collector Trades Off
Our program allocates objects on the heap, and the garbage collector finds the ones nothing refers to any more and reclaims their memory. Most collectors in HotSpot are generational: they split the heap into a young area for new objects and an old area for objects that survived several collections, because most objects die young and the young area can be cleaned cheaply and often.
Every collector balances three things. Pause time is how long application threads are stopped during a collection. Throughput is the share of CPU time spent running our code rather than collecting. Footprint is how much extra memory the collector needs to work well. A collector that does more work concurrently, while our threads run, has shorter pauses but uses more CPU and memory in total. There is no collector that wins all three, which is why HotSpot offers several.
2G1: The Default for Most Services
G1, the Garbage-First collector, has been the default since Java 9. It divides the heap into many equal regions, typically 1 to 32 MB each, and it collects the regions with the most garbage first, which gives it its name. It does marking of the old generation concurrently and copies live objects in short stop-the-world pauses, aiming at a pause-time goal, 200 ms by default, set with -XX:MaxGCPauseMillis.
For a typical Spring Boot service with a heap of a few gigabytes, G1 gives pauses in the tens of milliseconds and good throughput without tuning. Notice one detail: the JVM picks Serial instead of G1 automatically on a machine it considers small, roughly fewer than two CPUs or less than about 1.8 GB of memory, so a small container may not be running G1 unless we set -XX:+UseG1GC explicitly.
3ZGC and Shenandoah: Low-Latency Collectors
ZGC does almost all its work concurrently, including moving objects, using colored pointers and load barriers, small pieces of code the JIT adds to every reference load so threads always see an object's current location. Its pauses stay well under a millisecond and do not grow with heap size, from a few hundred megabytes up to terabytes. ZGC became generational in JDK 21, generational mode became its default in JDK 23, and in JDK 24 the old non-generational mode was removed, so -XX:+UseZGC now always means generational ZGC. The cost is more CPU spent on barriers and concurrent work, and it likes some heap headroom.
Shenandoah, developed largely by Red Hat, is another concurrent compacting collector with very short pauses. It is included in many OpenJDK builds, such as those from Red Hat, Amazon Corretto and Eclipse Temurin, but not in every vendor's build, so check yours. A generational mode for Shenandoah has also been developed in recent releases; check your version for its status. Both collectors are the right tools when p99 latency matters more than raw throughput.
4Parallel and Serial: Throughput and Small Footprint
Parallel GC stops the application and collects with many threads at once. Its pauses can be long, hundreds of milliseconds or seconds on large heaps, but it spends the least total CPU on collection, so it gives the highest throughput. It suits batch jobs, data processing and offline work where nobody waits on an individual request.
Serial GC collects with a single thread. It has the smallest memory and CPU overhead, so it suits small heaps of a few hundred megabytes and containers limited to one CPU, such as small sidecars, command-line tools and functions. There is also Epsilon, a collector that never reclaims anything, which is only useful for testing and very short-lived processes.
| Collector | Flag | Typical pauses | Best for |
|---|---|---|---|
| G1 (default) | -XX:+UseG1GC | tens of ms | most services, heaps of a few GB to tens of GB |
| ZGC (generational) | -XX:+UseZGC | under 1 ms | strict latency goals, large heaps |
| Shenandoah | -XX:+UseShenandoahGC | a few ms or less | low latency, where the build includes it |
| Parallel | -XX:+UseParallelGC | 100s of ms to seconds | batch jobs, maximum throughput |
| Serial | -XX:+UseSerialGC | short on small heaps | small heaps, one-CPU containers |
5Choosing and Validating a Collector
We start from the service's goal. If it is an online service with a latency target like p99 under 200 ms and a heap of a few gigabytes, G1 is the right default, and we only change it with evidence. If the target is p99.9 under 10 ms, or the heap is tens of gigabytes and G1's pauses grow, we try ZGC. If it is a nightly batch job, we try Parallel and look at total run time. If it is a tiny container, Serial may use less memory and CPU than anything else.
Then we validate, because the right choice depends on allocation rate, object lifetimes and heap size. We enable GC logging, run a realistic load test with each candidate, and compare pause times, throughput, CPU and memory. We keep everything else the same, including heap size, and we look at the application's latency percentiles, not just GC numbers.
A GC log line like this one tells us the kind of pause, how much memory was freed and how long it took. Comparing these across candidates, alongside request latency from the load test, turns the choice into a measurement instead of a debate.
6Follow-up questions
Should we tune G1 heavily before switching to ZGC?
A little tuning is reasonable: setting the heap size correctly, checking MaxGCPauseMillis, and fixing excessive allocation in the code often solve the problem. Deep tuning of many G1 flags is usually a sign that the workload wants a different collector, and trying ZGC with only the heap size set is often faster than tuning G1 for weeks.
Does ZGC need a bigger heap?
It works best with some headroom, because it collects concurrently while the application keeps allocating; if allocation outruns collection, threads are briefly stalled until memory is freed. So we usually give ZGC a little more heap than G1 needed, and watch for allocation stalls in the GC log.
How does the container's CPU limit affect the choice?
Concurrent collectors such as G1, ZGC and Shenandoah need spare CPU for their background threads. In a container limited to one CPU, that work competes directly with requests, and Serial or a carefully sized G1 may perform better. The JVM reads the container limits to size its GC threads, so the CPU limit should reflect what the service really has.
7Conclusion
In this answer, we saw what a garbage collector trades off, and the five main HotSpot collectors in Java 25: G1 as the balanced default, generational ZGC and Shenandoah for very short pauses, Parallel for throughput and Serial for small footprints. We choose by latency goal, heap size and workload type, and we confirm the choice with GC logs and realistic load tests.
Open with
In Java 25 there are five collectors I would consider. G1 is the default since Java 9. It splits the heap into regions, collects the ones with the most garbage first and aims at a pause target, 200 ms by default, so it suits most services with heaps of a few gigabytes.
Then, if they want more
One catch is that on a small container the JVM may pick Serial automatically, so I set the collector explicitly. ZGC is the low-latency option: it does nearly everything concurrently, so pauses stay under a millisecond regardless of heap size, and since JDK 24 only the generational mode exists. The cost is more CPU and a bit more heap headroom.
Shenandoah is a similar low-pause collector that many OpenJDK builds include, though not all. Parallel stops the world but spends the least CPU on GC, so it fits batch jobs where throughput matters and pauses do not. Serial uses one thread and suits small heaps and one-CPU containers.
To choose, I start from the goal: G1 unless we have a strict tail-latency target or a very large heap, where I would try ZGC, and Parallel for batch. Then I validate with GC logging switched on and a realistic load test, comparing pauses, application latency percentiles, CPU and memory with everything else unchanged.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 25, Java 21, Java 23
- 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 "What are the main garbage collectors in HotSpot as of Java 25, and how would you choose...": https://scrivio-interview-library.netlify.app/java/jvm-architecture/4cc3df468d/