Fundamentals 17 min read

Understanding G1 Garbage Collector: Design, Algorithms, and Tuning Practices

This article explains the design principles, region-based architecture, object allocation strategies, Young and Mixed G1 GC phases, concurrent marking, three‑color marking, and practical tuning parameters for the G1 garbage collector in Java, illustrating concepts with code snippets and diagrams.

58 Tech
58 Tech
58 Tech
Understanding G1 Garbage Collector: Design, Algorithms, and Tuning Practices

The original G1 paper (Appendix 1) was published in 2004 and became available in JDK 1.7u4 in 2012; Oracle made G1 the default garbage collector in JDK 9, replacing CMS. This article uncovers why Oracle switched to G1 and its advantages.

1. Understanding G1

Simple performance tuning principle

Developers only need to declare the following parameters:

-XX:+UseG1GC -Xmx32g -XX:MaxGCPauseMillis=200

-XX:+UseG1GC enables G1, -Xmx32g sets the maximum heap size, and -XX:MaxGCPauseMillis=200 limits the GC pause time. When tuning, only the pause target needs adjustment if the heap size is fixed.

Removal of separate young and old spaces

G1 eliminates the physical division between young and old generations, so developers no longer need to configure each generation’s size.

Instead, the heap is divided into many equal‑sized regions. G1 is still a generational collector: some regions hold young objects, which are collected by pausing all application threads and copying live objects to old or Survivor regions. Old‑generation regions are also divided, and G1 copies objects between regions to perform cleanup, achieving partial heap compaction and avoiding CMS fragmentation.

G1 also defines a special Humongous region for objects larger than 50% of a region. Such objects are allocated in the old generation, but short‑lived humongous objects can cause GC overhead; G1 therefore provides a dedicated Humongous area and may trigger a Full GC to find contiguous space.

Object allocation strategy

Allocation proceeds in four stages: stack allocation, TLAB (Thread‑Local Allocation Buffer), shared Eden region, and Humongous region. Escape analysis determines if an object can be allocated on the stack; otherwise TLAB is used for fast thread‑local allocation. If TLAB is exhausted, allocation falls back to the shared Eden space, and large objects go directly to the Humongous region.

G1 offers two GC modes, Young GC and Mixed GC, both Stop‑The‑World (STW).

2. G1 Young GC

Young GC triggers when the Eden region is exhausted, moving live objects to Survivor regions or promoting them to old regions if Survivor space is insufficient. After evacuation, Eden becomes empty and application threads resume.

To find root objects efficiently, G1 introduces the Remembered Set (RSet) which tracks references from one region to another. Unlike CMS’s point‑out approach, G1 uses a point‑in strategy: each region records which other regions reference objects inside it, reducing unnecessary scanning.

G1 also employs a Card Table, dividing each region into small fixed‑size cards (128–512 bytes). A card is marked dirty when a reference is written, and the RSet records the card indices, enabling precise tracking of inter‑region references.

3. G1 Mixed GC

Mixed GC combines normal young‑generation collection with reclamation of selected old‑generation regions identified by concurrent marking.

The Mixed GC process consists of two steps:

Global concurrent marking

Evacuation of live objects

Global concurrent marking has five sub‑steps:

Initial Mark (STW)

Root Region Scan (concurrent)

Concurrent Marking (concurrent)

Final Mark (Remark, STW)

Cleanup (STW, also purges empty regions)

Three‑color marking algorithm

During concurrent marking, objects are colored black (already scanned), gray (scanned but children not yet scanned), or white (unreachable). The algorithm proceeds by turning root objects black, traversing gray objects, and finally collecting white objects as garbage.

When the application mutates references during marking, objects can be incorrectly classified as garbage. G1 avoids this by using a snapshot‑at‑the‑beginning (STAB) approach combined with write barriers that record changed references.

Example of a mutating scenario:

A.c=C
B.c=null

CMS uses incremental update (recording inserts), while G1 uses STAB (recording deletions) to keep the marking accurate.

4. Tuning Practice

MaxGCPauseMillis tuning

The parameter limits the maximum GC pause time. G1 tries to keep each pause within this bound by selecting an appropriate collection set (CSet) of regions. For Young GC, only young regions are selected; for Mixed GC, G1 adds high‑benefit old regions while respecting the pause budget.

Setting MaxGCPauseMillis too low increases GC frequency and reduces throughput; setting it too high lengthens pauses. The default is 200 ms, which can be adjusted based on workload.

Other tuning parameters

-XX:G1HeapRegionSize=n

Region size (1 MB–32 MB, power of two) aims for ~2048 regions.

-XX:ParallelGCThreads=n

Number of STW worker threads, typically equal to the number of logical processors (capped at 8, or ~5/8 of processors for larger machines).

-XX:ConcGCThreads=n

Number of concurrent marking threads (≈ ¼ of ParallelGCThreads).

-XX:InitiatingHeapOccupancyPercent=45

Heap occupancy threshold that starts a marking cycle (default 45%).

Avoid using -Xmn or -XX:NewRatio to fix the young generation size, as this overrides pause‑time goals.

Triggering Full GC

Full GC may occur when concurrent marking fails, promotion or evacuation fails, or humongous allocation fails. Remedies include increasing heap size, adjusting -XX:G1ReservePercent, lowering InitiatingHeapOccupancyPercent, or increasing Parallel/Concurrent GC thread counts.

Due to space limits, many additional G1 tuning techniques exist and can be explored in practice.

Appendix

1) The original G1 paper: Detlefs, D., Flood, C., Heller, S., and Printezis, T. 2004. Garbage‑first garbage collection. In Proceedings of the 4th International Symposium on Memory Management (Vancouver, BC, Canada, October 24‑25, 2004).

javaJVMMemory ManagementGarbage CollectionPerformance TuningG1GC
58 Tech
Written by

58 Tech

Official tech channel of 58, a platform for tech innovation, sharing, and communication.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.