Video in progress
How does G1 divide the heap, and what does -XX:MaxGCPauseMillis actually control?
G1 splits the heap into equally sized regions (1-32 MB), and each region takes the role of eden, survivor, old or humongous. MaxGCPauseMillis is a target, not a guarantee: G1 chooses how many regions to collect in a single pause to try to fit within it.
📖 Answer
In classic GCs, young and old are two contiguous parts of the heap. In G1 they are sets of regions, and the number of regions changes at runtime. The region size is chosen automatically based on the heap, aiming for about 2048 regions.
How it works:
- Young pauses collect the eden and survivor regions.
- When Old fills up to a threshold (IHOP, adaptive by default), concurrent marking starts, and the application keeps running.
- After marking come mixed pauses: together with young, the old regions with the most garbage are collected too. That is where the name Garbage First comes from.
With -XX:MaxGCPauseMillis=200 (default 200 ms), G1 looks at the statistics of past pauses and adjusts the young generation size and the number of regions it collects in a mixed pause.
Practical observations:
- if you set the value too low (10-20 ms), young shrinks too much, GCs happen too often and throughput drops;
- if you set young by hand with
-XmnorNewRatio, G1 can no longer steer toward the pause target, so it is better not to set them with G1; - if the real pauses always exceed the target, the cause is often a heap that is too small or too many live objects.
Sign up to mark studied questions and save your progress.