Java / Java 17 Garbage Collection Interview Questions
How does adaptive sizing (ergonomics) work in the JVM's garbage collectors?
Adaptive sizing lets the JVM automatically tune heap-region sizing at runtime instead of relying on fixed, hand-picked values - it's controlled by -XX:+UseAdaptiveSizePolicy, which is enabled by default for Parallel GC.
Under Parallel GC, the policy runs a feedback loop: after each collection it compares observed pause times and throughput against the configured goals (-XX:MaxGCPauseMillis and -XX:GCTimeRatio) and adjusts the young generation size and survivor-space ratio for the next cycle accordingly - shrinking generations if pauses run too long, growing them if throughput is falling short of the ratio goal.
G1 does something conceptually similar but continuously: it recomputes how many regions to label as young (and how many old regions to fold into the next mixed collection) at essentially every pause, specifically to keep hitting -XX:MaxGCPauseMillis as allocation behavior shifts. This removes most of the manual generation-sizing tuning developers used to do, though it can cause visible "sawtooth" adjustments in generation sizes as the policy reacts to workload changes - and it can be turned off with -XX:-UseAdaptiveSizePolicy for teams that want fully deterministic, manually fixed sizing instead.
More Related questions...