Sub topic 6
Choosing a collector: Serial, Parallel, G1, and ZGC
This chapter assumes that you know basic Java syntax, objects, references, and how to run Java programs from the command line. A garbage collector is the JVM component that finds unreachable objects and reclaims their heap memory.
Choosing a collector means balancing throughput, pause time, heap size, and operational simplicity. There is no universally best collector. Start with the default, measure the workload, and change the collector when measurements show a problem.
The main choices
| Collector | Main goal | Good fit |
|---|---|---|
| Serial | Simple collection with one GC thread | Small heaps, command-line tools, and small applications |
| Parallel | High application throughput | Batch jobs and services where pauses are acceptable |
| G1 | Predictable pauses across larger heaps | General server applications |
| ZGC | Very short pauses | Large heaps and latency-sensitive services |
A pause is a period when application threads stop while the JVM performs part of garbage collection. Throughput is the amount of useful application work completed over time. Increasing one often affects the other.
Serial collector
The Serial collector uses one thread for garbage collection. During collection, application threads usually stop. This design has low coordination overhead, so it can work well for a small application with a small heap.
Use it when the application has limited memory, runs on one processor, or values simple behavior more than low pauses.
java -XX:+UseSerialGC -Xms256m -Xmx256m MyApplicationParallel collector
The Parallel collector uses multiple threads to collect garbage. Its goal is high throughput rather than consistently short pauses. A batch process that spends most of its time calculating may prefer Parallel collection, even if occasional pauses last longer.
Parallel is a reasonable choice when the application can tolerate pauses and its main requirement is to finish as much work as possible per hour.
java -XX:+UseParallelGC -Xms2g -Xmx2g MyApplicationG1 collector
G1, or Garbage-First, divides the heap into regions instead of treating the young and old areas as one continuous space. It selects regions with the most reclaimable space first. G1 can perform much of its work concurrently with application threads.
G1 became the default collector in Java 9. It is a strong general-purpose choice for server applications and medium-to-large heaps. You can provide a pause target, but the target is a goal, not a guarantee.
java -XX:+UseG1GC -Xms4g -Xmx4g -XX:MaxGCPauseMillis=200 MyApplicationThe pause target can cause G1 to collect less work per pause. If the heap is too small or the allocation rate is too high, G1 cannot meet the target indefinitely.
ZGC
ZGC is a concurrent collector designed for very short pauses, even with large heaps. It performs most expensive work concurrently with the application. This reduces pause time but adds coordination and metadata overhead.
In Java 11, ZGC is an experimental feature. It was supported mainly on Linux x64 in that release, so check the exact JDK 11 distribution and platform before using it in production.
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xms8g -Xmx8g MyApplicationZGC is appropriate when latency matters more than maximum throughput and the application has a sufficiently large heap. It does not fix excessive allocation or a memory leak.
A practical selection process
Use this process as a starting point:
- Keep G1 if you have a typical server application and no measured GC problem.
- Try Parallel for throughput-focused batch work.
- Try Serial for small, simple programs.
- Evaluate ZGC for large heaps with strict pause requirements, while remembering its Java 11 status.
- Compare collectors using the same workload, heap limits, and GC logs.
Common selection errors
Do not choose a collector from a single observed pause. Check pause distributions, allocation rate, CPU use, and application response time over a representative run.
Do not set -Xmx to all available system memory. The JVM also needs native memory for threads, class metadata, code caches, and the operating system. Leave headroom for these areas.
Do not assume -XX:MaxGCPauseMillis guarantees a maximum pause. G1 uses it as a soft target, and application behavior can make the target impossible.
Exercise
Which collector is a sensible first choice for a typical Java 11 server application when no measurements show a specific problem?
Show answer
G1, because it is the Java 11 default and provides a general balance between throughput and pause control. Measure the workload before changing it.