Phase 14 of 20 · Topic 14.2

Tracing Garbage Collectors: G1GC, ZGC & Shenandoah

1Concept

Modern GC algorithms: 1. Serial GC (single-threaded for small micro-VMs); 2. Parallel GC (high-throughput multi-core batch processing); 3. G1GC (default since Java 9, partitions heap into 2048 dynamic regions to meet target pause times `-XX:MaxGCPauseMillis`); 4. ZGC & Shenandoah (ultra-low latency collectors achieving sub-millisecond pauses on multi-terabyte heaps using colored pointers and load barriers).

2Architecture Diagram

G1GC Region Matrix (Heap split into ~2048 equal regions):
[ Eden ] [ Tenured ] [ Survivor ] [ Free ] [ Humongous ] [ Eden ]

3Code Example

Core Java
public class GarbageCollectorInspection {
    public static void main(String[] args) {
        java.lang.management.ManagementFactory.getGarbageCollectorMXBeans().forEach(gcBean -> {
            System.out.println("Collector: " + gcBean.getName());
            System.out.println("Collections Count: " + gcBean.getCollectionCount());
            System.out.println("Total Collection Time: " + gcBean.getCollectionTime() + " ms");
            System.out.println("------------------------------------");
        });
    }
}

4Expected Output

Collector: G1 Young Generation
Collections Count: 2
Total Collection Time: 12 ms
------------------------------------
Collector: G1 Old Generation
Collections Count: 0
Total Collection Time: 0 ms
------------------------------------

5Key Takeaways

  • G1GC is the default garbage collector in Java 9 through Java 21+.
  • ZGC (`-XX:+UseZGC`) provides pause times under 1ms regardless of heap size (even on 16TB heaps).
  • Tune pause time target in G1GC with `-XX:MaxGCPauseMillis=200`.