Understanding the G1 Garbage Collector in Java: Detailed Process and Log Analysis
This article explains the G1 (Garbage‑First) garbage collector introduced in JDK7, its region‑based architecture, key differences from CMS, and the complete GC cycle—including Young GC, concurrent phases, mixed GC, and Full GC—while providing real‑world log examples and performance insights for Java developers.
G1 (Garbage First) is a modern garbage collector introduced in JDK7 and set as the default in JDK9; it partitions the heap into regions, weakening the traditional generational model and improving space reclamation.
Compared with CMS, G1 offers region‑based memory layout, better space compression, flexible pause‑time targets, and concurrent phases that avoid stop‑the‑world pauses for most work.
Complete G1 GC Process
G1 GC consists of four main operations: Young GC (YGC), concurrent phases, mixed GC, and Full GC.
YGC
YGC clears the Eden region when it fills, moves live objects to Survivor or Old regions, and can be observed via PrintGCDetails logs. Example log snippet:
23.430:[GC pause (young),0.23094400 secs] ...Interpretation: Young GC took 230 ms, with GC threads using 850 ms CPU time; Eden reduced from 1286 MB to 0 B, Survivors grew from 78 MB to 152 MB, and heap usage dropped from 1454 MB to 242 MB.
Concurrent Phase
During the concurrent phase G1 scans root regions, marks regions with the most reclaimable garbage (X regions), and may pause briefly for initial marking. Sample logs show the start and end of concurrent root scanning and marking.
50.541:[GC concurrent-root-region-scan-start] ...Mixed GC
Mixed GC combines a YGC with reclamation of previously marked X regions, reducing heap fragmentation. Example mixed GC log:
79.826:[GC pause (mixed),0.26161600 secs] ...After mixed GC, Eden is emptied, some Survivor objects are promoted, and a portion of Old space is reclaimed.
Overall, G1 repeatedly cycles through these phases, providing predictable pause times and better memory compaction compared with CMS, making it suitable for multi‑core servers with large heap sizes.
Follow the "SouYunKu" technical team for more articles and updates.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.