The Garbage-First (G1) Garbage Collector


Introduction

The Garbage-First (G1) Garbage Collector is the default garbage collector since JDK 9, and is the default in the JDK 8 Enterprise Performance Pack. G1 can also be explicitly enabled using -XX:+UseG1GC.

G1 is designed to scale from small machines to large, multiprocessor systems with large amounts of memory. G1 attempts to meet garbage collection pause-time goals with high probability while achieving high throughput with little need for configuration. G1 aims to provide the best balance between latency and throughput.

G1 is a “mostly concurrent” collector, so named because it performs part of its work concurrently to the application. Mostly concurrent collectors trade processor resources (which would otherwise be available to the application) for shorter major collection pause time.


Overhead of G1

G1 will use one or more processors during the concurrent parts of garbage collection. On an N processor system, the concurrent part of the collection uses K/N of the available processors, where 1 <= K <= ceiling{N/4}. In addition to the use of processors during concurrent phases, additional overhead is incurred to enable concurrency. Thus, while garbage collection pauses are typically much shorter with G1, application throughput also tends to be slightly lower than with the other collectors.

On a machine with more than one processing core, processors are available for application threads during the concurrent part of the collection, so the concurrent garbage collector thread doesn’t pause the application. This usually results in shorter pauses, but again fewer processor resources are available to the application and some slowdown should be expected, especially if the application uses all of the processing cores maximally. As N increases, the reduction in processor resources due to concurrent garbage collection becomes smaller, and the benefit from concurrent collection increases.


Further Reading

If you are interested in learning more about how G1 works, see Garbage-First (G1) Garbage Collector(GC) in the HotSpot Virtual Machine Garbage Collection Tuning Guide. (This link is to the documentation for JDK 21, but the information contained therein applies to G1 regardless of JDK version.)


Last reviewed on Sat Feb 01 2025 00:00:00 GMT+0000 (Coordinated Universal Time)