Java / Java 17 Garbage Collection Interview Questions
How can you optimize G1 GC for large heaps (32GB+)?
Large heaps stress the defaults G1 tunes itself for, so a few adjustments tend to pay off:
-XX:G1HeapRegionSize=32m -XX:InitiatingHeapOccupancyPercent=30 -XX:ConcGCThreads=8 -XX:G1ReservePercent=15 -XX:+UseNUMA
Explicitly setting a larger region size (up to the 32MB cap) reduces the total region count G1 has to track and cuts down on humongous-object classification for large objects. Lowering IHOP (or letting adaptive IHOP handle it) starts the concurrent marking cycle earlier relative to old-gen occupancy, giving G1 more runway to finish marking before the heap fills further. Increasing ConcGCThreads speeds up that concurrent marking pass on multi-core hosts with large old generations to scan.
Raising G1ReservePercent keeps more headroom free specifically to avoid evacuation failures, which get costlier the larger the heap is. On multi-socket hardware, enabling NUMA awareness helps G1 allocate memory local to the core doing the work, reducing cross-socket memory latency. If, after this tuning, pause times are still too high for the workload's needs, that's usually the signal to evaluate ZGC or Shenandoah instead, since their pause times don't scale with heap size the way G1's can.
More Related questions...