JDK GCs Comparison - Sip of Java

As of JDK 18, the JDK comes packaged with four garbage collectors (GC); Serial GC, Parallel GC, G1 GC, and ZGC. In most scenarios, G1 GC, the default GC, will be the best choice. However, understanding the design goals of the GCs can be beneficial and might help you achieve the performance goals for your applications. This article will take a high-level look at each GC and when they should be used.

Serial Garbage Collector

The Serial GC is the “simplest” of the GCs. It performs all of its work on a single thread, hence why it is named “serial”.

The Serial GC is best suited for applications running in environments with limited resources and a live set not much larger than 100 MB.

The Serial GC can be enabled with the VM flag: -XX:+UseSerialGC.

Parallel Garbage Collector

The Parallel GC is architecturally similar to the Serial GC but can utilize multiple threads when performing its work.

The Parallel GC is designed to maximize throughput. So applications where throughput is the highest priority, even at the cost of longer pause times, are the ideal use case.

The Parallel GC can be enabled with the VM flag: -XX:+UseParallelGC.

G1 Garbage Collector

The Garbage-First, (G1) GC was designated the default GC with JDK 9 1. G1 GC is a mostly concurrent GC, meaning it can perform work while the application is running.

The G1 GC tries to strike a balance between latency and throughput and can scale from environments with minimal resources to environments with a lot of resources.

G1 GC is the default GC but can be explicitly enabled with the VM flag: -XX:+UseG1GC.

Z Garbage Collector

The newest GC, ZGC, was introduced as a production feature in JDK 15. ZGC is also a concurrent GC.

ZGC focuses on low-latency, with pause times rarely exceeding 250 microseconds, and can scale heap size from 8MB to 16TB.

ZGC can be enabled with the VM flag: -XX:+UseZGC.

Additional Reading

Happy coding!

  1. The HotSpot JVM ergonomics might influence the default GC.