Java / JVM Architecture (Java21) Interview questions
How does G1 GC organize heap memory into regions?
Unlike Serial or Parallel GC, which use one large contiguous Young space and one large contiguous Old space, G1 ("Garbage First") GC divides the whole heap into many equally-sized regions, typically between 1MB and 32MB depending on heap size.
Each region is dynamically labeled as Eden, Survivor, Old, or, for objects larger than half a region, Humongous, and these labels can be reassigned between collections as the heap's needs change, rather than being fixed at JVM startup.
During a collection, G1 prioritizes the regions containing the most garbage first, which is where the name comes from, since reclaiming those gives the best return in freed memory per unit of collection time.
This region-based approach lets G1 target a pause-time goal set with -XX:MaxGCPauseMillis, by choosing how many regions to collect in a given pause rather than being forced to collect an entire fixed-size generation at once.
More Related questions...