The G1 Garbage Collector - Sip of Java

If you are on JDK 11 or later and unsure what garbage collector you are using, you are probably using G1. The G1, Garbage First, garbage collector (GC) has been the default GC since JDK 9 1. In this article, we will briefly examine G1 and a couple of tips on how you might tune it.

G1 GC Overview

G1 was initially added to the JDK with JDK 7 before becoming the default GC with JDK 9. G1 tries to balance pause times and throughput.

G1 is a concurrent and parallel GC. While this might sound like I am repeating myself, these are distinct concepts within GCs. Concurrent refers to a GC that can perform work while an application is running, though G1 cannot perform all of its work while the application is running, and there are still stop the world events. Parallel means a GC can use multiple threads to perform its work.

G1 GC Development

G1 is still under active development. Each JDK release sees several improvements to throughput, startup, pause times, and memory footprint. Recent changes include:

Major planned future changes include:

G1 GC Tuning

The default configuration for G1 should provide optimal performance in most cases. However, if your application has unique performance requirements or other performance optimizations have already been done, GC tuning might be needed. G1 has many tuning options, but the two most impactful would be:

  • XX:MaxGCPauseTimeMillis - as the name suggests, setting the maximum amount of time the GC can pause the VM.
  • -Xmx - the max heap space allowed to the VM.

As you tune a GC, measure results using a tool like JFR. Also, check the links below for more information about G1, including tuning options.

Additional Reading

Happy coding!

  1. Hotspot JVM Ergonomics might choose a different GC in some scenarios.