G1, or Garbage-First, is the JVM collector designed to balance application throughput, pause times, and memory use rather than maximize one at the expense of the others. Introduced experimentally in JDK 6, it became the default in server environments with JDK 9 and, from JDK 27, is the default across all environments. Improvements have made it suitable for smaller systems as well as large, multicore servers. G1 divides the heap into uniformly sized regions that serve as eden, survivor, old, or humongous space. New objects enter eden; survivors are copied between regions and eventually promoted to old space. Objects larger than half a region occupy contiguous humongous regions.
Collection begins with pauses that evacuate young-generation regions. When old-space occupancy reaches a threshold, G1 marks old objects concurrently, with special pauses for starting and completing the marking and deciding whether to reclaim space. If needed, it enters a space-reclamation phase, selecting old regions with the most recoverable garbage for mixed collections alongside young regions; it returns to young-only collections when further reclamation is not worthwhile. G1 is explicitly enabled with -XX:+UseG1GC on older JVM versions, though it is unnecessary from JDK 27 onward. Configuration should be cautious: a larger heap can reduce pauses but uses more memory, while a higher pause-time target may improve throughput at the cost of latency. -Xms and -Xmx set initial and maximum heap sizes, and MaxGCPauseMillis is a target, not a guarantee.
Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.