G1GC shrinks the young generation before starting mixed collections because it needs to free up heap regions for the old-generation regions it is about to collect. The young generation is the easiest place to reclaim memory quickly: by reducing the young generation size, G1 ensures that the next young collection frees enough regions to accommodate the mixed collection's old regions without triggering a full GC. This is a deliberate space-management strategy, not a bug or an optimization oversight.
Background: How G1GC Organizes the Heap
G1 (Garbage-First) divides the heap into equal-sized regions, typically 1–32 MB each, and dynamically assigns them to the young generation (eden and survivor) or the old generation. Unlike earlier collectors with fixed generational boundaries, G1 can resize generations on the fly by changing how many regions belong to each. The young generation is collected frequently with stop-the-world pauses; the old generation is collected less often, and only during mixed collections that also include young regions.
Mixed collections are triggered when the old generation occupancy crosses a threshold (InitiatingHeapOccupancyPercent, default 45%). At that point G1 schedules a series of mixed collections that gradually evacuate old regions with the most garbage. But mixed collections need somewhere to copy surviving objects, and that somewhere is free regions.
Why Shrinking the Young Generation Frees Regions
Before a mixed collection cycle begins, G1 performs a young-only collection (or a few) and adjusts the young generation size downward. The logic is simple: the young generation is the only generation G1 can shrink immediately without waiting for old regions to be collected. By reducing the number of eden and survivor regions, G1 converts those regions into free regions that can serve as to-space for the upcoming mixed collections.
If G1 did not shrink the young generation, it would have to either delay mixed collections until enough free regions appeared naturally or risk a promotion failure—a situation where survivors cannot be copied because no free regions exist, often leading to a full GC. Shrinking the young generation is a preventive measure that keeps the mixed collection cycle from stalling.
The Mechanics: How G1 Adjusts Young Generation Size
G1 uses a feedback loop based on pause-time goals and heap occupancy. The key parameters and behaviors include:
- Young generation sizing: G1 tracks the desired pause time (MaxGCPauseMillis, default 200 ms) and adjusts the young generation size to meet it. When mixed collections are imminent, G1 may temporarily reduce the young generation below what the pause-time model alone would suggest.
- Region accounting: Each region is either free, young (eden/survivor), or old. The collector maintains a free-region list; shrinking the young generation adds regions to that list.
- Mixed collection set selection: G1 chooses old regions with the highest garbage percentage first (hence "garbage-first"). The collection set for a mixed collection includes both young regions and selected old regions. The number of old regions per mixed collection is limited by the available free regions and the pause-time goal.
- Adaptive sizing: The young generation size is not fixed; it changes after each collection based on observed survival rates and pause times. The shrink before mixed collections is an explicit adjustment within this adaptive framework.
What Happens If the Young Generation Is Not Shrunk
Without the shrink, G1 would enter mixed collections with fewer free regions. The consequences can include:
- Fewer old regions can be collected per mixed collection, prolonging the mixed cycle and increasing the chance of concurrent mode failure.
- Higher risk of promotion failure during young collections that occur alongside mixed collections, because survivors from young regions compete for the same limited free regions.
- Increased likelihood of a full GC, which is stop-the-world and much more expensive than incremental mixed collections.
In extreme cases, if the heap is nearly full and the young generation cannot be shrunk enough, G1 may trigger a full GC as a last resort. The shrink is therefore a safety valve that buys time and space.
Trade-offs and Costs of Shrinking the Young Generation
Shrinking the young generation is not free. A smaller young generation means:
- More frequent young collections, since eden fills faster. Each young collection has overhead, so total GC time may increase.
- Higher promotion rate per young collection if survival rates are high, because objects have less time to die in the young generation before being promoted. This can increase pressure on the old generation.
- Potential for increased CPU usage due to more frequent GC cycles.
G1 balances these costs against the benefit of enabling mixed collections. The adaptive sizing algorithm attempts to keep pause times within the goal while maintaining enough free regions for mixed collections. In practice, the shrink is usually modest and temporary; once the mixed cycle completes and old regions are reclaimed, the young generation can expand again.
Common Misconceptions and Related Questions
A common misconception is that G1 shrinks the young generation to reduce pause times. While pause-time goals do influence young generation sizing, the specific shrink before mixed collections is driven by space management, not pause time. Another misconception is that the shrink is permanent; it is a dynamic adjustment that reverses as heap conditions change.
Readers often ask why G1 does not simply collect old regions without touching the young generation. The answer is that mixed collections always include young regions—they are "mixed" because they combine young and old. The young generation must be collected anyway, and shrinking it beforehand ensures that collection yields enough free regions to support the old-region evacuation.
How to Observe and Tune This Behavior
You can observe young generation shrinking before mixed collections using GC logging. Enable logging with flags such as -Xlog:gc* (JDK 9+) or -XX:+PrintGCDetails (older JDKs). Look for log lines showing young generation size changes and the transition to mixed collections. The log will show the young generation size before and after the adjustment, and the number of old regions in each mixed collection.
Tuning options that affect this behavior include:
- -XX:MaxGCPauseMillis: A tighter pause goal may cause G1 to shrink the young generation more aggressively, but it can also reduce throughput.
- -XX:InitiatingHeapOccupancyPercent: Lowering this threshold starts mixed collections earlier, giving G1 more time to shrink the young generation gradually.
- -XX:G1MixedGCLiveThresholdPercent: Controls which old regions are eligible for mixed collections; regions with live data above this threshold are skipped, reducing the need for free regions.
- -XX:G1HeapWastePercent: Sets the amount of reclaimable space G1 is willing to waste before starting mixed collections; a higher value delays mixed collections.
In most applications, the default settings work well. Manual tuning is only advisable after profiling shows a specific problem, such as frequent full GCs or excessive pause times.
Frequently Asked Questions
Does G1GC always shrink the young generation before mixed collections?
Not always. G1 shrinks the young generation when it needs to free regions to support the mixed collection cycle. If there are already enough free regions, the shrink may be minimal or unnecessary. The behavior is adaptive and depends on heap occupancy, free region count, and pause-time goals.
Can I disable young generation shrinking in G1GC?
There is no direct flag to disable the shrink. The behavior is part of G1's adaptive sizing algorithm. You can influence it indirectly by adjusting pause-time goals or heap occupancy thresholds, but you cannot turn it off entirely without switching to a different collector.
Why does G1 shrink the young generation instead of expanding the old generation?
G1 cannot expand the old generation without collecting old regions first, because the heap is a fixed size. Shrinking the young generation is the only way to increase the number of free regions immediately. Expanding the old generation would require reclaiming space from somewhere, and the young generation is the most readily available source.
Does shrinking the young generation cause more frequent young GCs?
Yes, typically. A smaller young generation fills up faster, leading to more frequent young collections. However, G1 balances this against the need to complete mixed collections and avoid full GCs. The net effect on overall GC time depends on the application's allocation rate and object lifetime profile.
How can I tell if young generation shrinking is causing performance problems?
Look for symptoms such as increased young GC frequency, higher promotion rates, or longer overall GC time in GC logs. If these occur alongside mixed collections, the shrink may be contributing. However, correlation is not causation; profiling with tools like GCViewer or JClarity's Censum can help identify the root cause.
Is young generation shrinking unique to G1GC?
Other collectors also adjust young generation size dynamically, but the specific strategy of shrinking before mixed collections is characteristic of G1's region-based design. Parallel and CMS collectors use different mechanisms for managing generation sizes and do not have an equivalent mixed collection phase.