G1 (Garbage First) Garbage Collector: Modern Default for Java

Illustration for G1 (Garbage First) Garbage Collector: Modern Default for Java
By Last updated:

The G1 (Garbage First) Garbage Collector is the modern default in the JVM, replacing CMS as the go-to option for balancing low latency and high throughput. Its region-based design provides predictable pause times while managing large heaps efficiently.

In this tutorial, we’ll explain how G1 works, why it became the default, tuning strategies, and real-world use cases.


Why G1 GC Matters

  • Default GC in Java since Java 9.
  • Designed for multi-core systems and large heaps.
  • Provides predictable pause times with tunable options.
  • Replaces CMS with region-based compaction to avoid fragmentation.

Analogy: G1 is like cleaning your house room by room instead of all at once. This keeps things tidy without overwhelming pauses.


How G1 Works: Region-Based Heap

Unlike CMS or Parallel GC, G1 divides the heap into equal-sized regions (1–32MB). These regions can dynamically act as Eden, Survivor, or Old Generation.

Heap: [Region1][Region2][Region3]...[RegionN]
  • Young Gen objects → Allocated in Eden regions.
  • Survivors → Move to Survivor regions.
  • Long-lived objects → Promoted to Old Gen regions.
  • Large objects → Allocated directly in Humongous regions.

G1 GC Phases

  1. Young GC (Minor GC)

    • Cleans Eden + Survivor regions.
    • Frequent, short pauses.
  2. Concurrent Marking

    • Identifies live objects across the heap.
  3. Mixed GC

    • Collects both young and old regions.
    • Prioritizes regions with the most garbage (“garbage first”).
  4. Compaction

    • Moves objects to reduce fragmentation.
    • Done incrementally to avoid long pauses.

Example: Enabling G1 GC

java -XX:+UseG1GC -Xms2g -Xmx2g MyApplication

Tuning G1 GC

  • -XX:MaxGCPauseMillis=200 → Target pause time.
  • -XX:InitiatingHeapOccupancyPercent=45 → Start concurrent cycle at 45% heap usage.
  • -XX:G1HeapRegionSize=16m → Region size.
  • -XX:+ParallelRefProcEnabled → Improves reference processing.

Advantages of G1 GC

  • Predictable pause times with pause-time goals.
  • Region-based compaction eliminates fragmentation.
  • Scales well for multi-GB heaps.
  • Balanced for both throughput and latency.

Limitations of G1 GC

  • More complex than CMS.
  • Slightly higher overhead for small heaps (<1GB).
  • Requires tuning for ultra-low-latency apps (ZGC/Shenandoah are better).

Real-World Case Study

A large e-commerce platform migrated from CMS to G1 GC. By setting -XX:MaxGCPauseMillis=200, it achieved consistent response times under 250ms, reducing customer-facing latency during sales peaks.


JVM Version Tracker

  • Java 8 → Parallel GC default; G1 optional.
  • Java 9 → G1 became default.
  • Java 11 → ZGC introduced as experimental.
  • Java 17 → ZGC and Shenandoah became stable.
  • Java 21+ → NUMA-aware GC, Project Lilliput improvements.

Best Practices

  • Start with default G1 GC settings.
  • Use -XX:MaxGCPauseMillis for tuning pause goals.
  • Monitor GC logs (-Xlog:gc* in Java 9+).
  • Prefer ZGC or Shenandoah for ultra-low-latency workloads.

Conclusion & Key Takeaways

  • G1 GC is the default garbage collector in modern JVMs.
  • Region-based design avoids fragmentation and provides predictable performance.
  • Best suited for large heaps and balanced workloads.
  • For ultra-low latency, consider ZGC or Shenandoah.

FAQs

1. What is the JVM memory model and why does it matter?
It ensures safe and consistent access to memory across threads.

2. How does G1 differ from CMS?
G1 avoids fragmentation with region compaction, while CMS left scattered free memory.

3. When should I use G1 GC?
For apps needing balanced throughput and latency, especially with large heaps.

4. How do G1 GC logs look?
They show Young GC, Mixed GC, and Pause times.

5. What is a humongous object in G1 GC?
An object larger than half a region size, allocated directly in special regions.

6. How do I tune G1 GC for performance?
Start with -XX:MaxGCPauseMillis and adjust InitiatingHeapOccupancyPercent.

7. Can G1 handle multi-GB heaps?
Yes, G1 is designed for heaps >4GB.

8. Why is G1 better than Parallel GC?
It provides predictable pauses, while Parallel GC focuses only on throughput.

9. What’s new in Java 21 GC improvements?
NUMA-aware GC and Project Lilliput reduce object header size and improve efficiency.

10. How does GC differ in microservices vs monoliths?
Microservices prioritize low-latency GC; monoliths often prefer throughput-oriented GC.