Java中ZGC的Relocation Set与G1的Collection Set有何区别?
Great question—this is a common point of confusion when navigating G1 and ZGC, since both use region-based targeted collection but are optimized for very different pause-time goals. Let’s break down their core distinctions:
1. Core Purpose & Collection Focus
G1's Collection Set (CSet)
The CSet is a group of regions (both young and old) that G1 has selected to collect garbage from. Its primary goal is to maximize garbage reclamation efficiency while adhering to your configured pause-time target. When G1 processes the CSet, it identifies and discards dead objects, then copies surviving objects to new regions.ZGC's Relocation Set
The Relocation Set is a set of regions containing live objects that need to be moved. ZGC’s priority is ultra-low pause times, so its collection process revolves around moving live objects out of regions rather than directly scanning for garbage. Once live objects are relocated, the original regions are immediately freed for reuse—no need to scan for dead objects first.
2. Selection Strategy
G1 CSet Selection
G1 uses a "garbage-first" heuristic: it picks regions with the highest garbage density (i.e., most dead objects) because they offer the best reclamation payoff. It also caps the size of the CSet to ensure the collection pause stays within your specified limit. If the CSet were too large, the STW pause would exceed your target.ZGC Relocation Set Selection
ZGC prioritizes regions where moving live objects is fastest. It favors regions with low live object density (fewer objects to move) or regions that have been marked for relocation due to fragmentation. Crucially, ZGC doesn’t limit the size of the Relocation Set strictly for pause time—since most relocation work happens concurrently, larger sets don’t translate to longer STW pauses.
3. Collection Execution & Pause Behavior
G1 CSet Processing
Processing the CSet requires a significant STW phase. During this pause, G1 copies all live objects from the CSet regions to new regions (survivor spaces for young objects, old-gen regions for tenured objects), then frees the original CSet regions. The length of this pause directly correlates with the number of live objects in the CSet.ZGC Relocation Set Processing
Almost all work with the Relocation Set happens concurrently. The only STW phases are brief: one to initialize the relocation and another to finalize it (updating pointers in a few root objects). While threads are running, ZGC uses colored pointers to redirect access to old objects to their new relocated locations. This means even large Relocation Sets barely impact pause times.
4. Region Reuse & Memory Layout
G1 CSet Regions
After collection, CSet regions are cleaned and reclassified (either as young-gen survivor spaces, eden, or old-gen regions) based on the needs of the heap. G1 maintains strict generational boundaries between young and old regions.ZGC Relocation Set Regions
Once live objects are moved out of Relocation Set regions, those regions are immediately marked as free and can be reused for any purpose—ZGC doesn’t enforce strict generational boundaries. This flexible reuse helps ZGC avoid fragmentation more effectively than G1 in many cases.
内容的提问来源于stack exchange,提问作者coffee

