SSerguey Asael Shinder
Java coding notes: the JVM, and writing software that lasts

Serguey Asael Shinder: A G1 tutorial on Inside Java: the old default rule and the three flags it says to touch

· by Serguey Asael Shinder / Serguey Shinder

Inside Java published a tutorial on 10 November, The G1 Garbage Collector, a lesson in the Learn Java series on the JVM. Its news value is small: we covered the change that makes G1 the default in every environment, JEP 523 in JDK 27, on 5 October. Its practical value is in the details it writes down in one place.

The most useful paragraph explains what "default" meant until now. From JDK 9 to JDK 26, the JVM chose G1 only if it detected two or more CPU cores and more than 1792 MB of available memory; otherwise it used SerialGC. On JDK 8, ParallelGC was the default and G1 needed -XX:+UseG1GC. From JDK 27 the flag is never necessary. For anyone running small containers on JDK 21 or 25, that rule is the reason a service can end up on Serial without anyone having chosen it.

Serguey Asael Shinder: A G1 tutorial on Inside Java: the old default rule and the three flags it says to touch
A G1 tutorial on Inside Java: the old default rule and the three flags it says to touch — Serguey Asael Shinder

The lesson then explains the design. G1 splits the heap into regions of equal size, assigned as eden, survivor, old or humongous; an object larger than half a region gets its own contiguous humongous space and is treated as old. A cycle has a Young-Only phase and a Space-Reclamation phase. Concurrent marking of old regions starts when old-generation occupancy reaches a threshold, passes through the Concurrent Start, Remark and Cleanup pauses, and may be cancelled with a Concurrent Mark Undo pause. Mixed collections then evacuate the old regions with the most garbage first, which is where the name Garbage-First comes from.

On configuration the lesson is deliberately restrained: "less is more", and every setting is a trade-off rather than a way to get "better" performance. It names three parameters. -Xmx sets the maximum heap, and the general guidance it gives is about 25% above the expected live set. -Xms sets the initial heap; matching it to -Xmx avoids resizing and, with -XX:+AlwaysPreTouch, can reduce latency early in a process's life. -XX:MaxGCPauseMillis is a soft pause target: higher favours throughput, lower favours latency. The lesson also mentions an upcoming change, Automatic Heap Sizing, which is meant to take over heap sizing itself.

For services that set a dozen GC flags years ago, the lesson is a reasonable checklist: confirm which collector is actually running on each JDK version you deploy, keep the heap settings, and question the rest.