Can someone explain how the G1 Garbage Collector works please? I haven't been able to find any comprehensive, easy-to-understand descriptions anywhere yet.
Thanks
G1 copies objects from one or more regions of the heap to a single region on the heap, and in the process both compacts and frees up memory. This evacuation is performed in parallel on multi-processors, to decrease pause times and increase throughput.
In the common language runtime (CLR), the garbage collector (GC) serves as an automatic memory manager. The garbage collector manages the allocation and release of memory for an application. For developers working with managed code, this means that you don't have to write code to perform memory management tasks.
After the marking phase completes, G1 knows which regions are mostly empty. It collects these regions first, which often yields a large amount of free space. This is why this method of garbage collection is called Garbage-First.
Garbage collection (GC) is a dynamic approach to automatic memory management and heap allocation that processes and identifies dead memory blocks and reallocates storage for reuse. The primary purpose of garbage collection is to reduce memory leaks.
The collector splits the heap up into fixed-size regions and tracks the live data in those regions. It keeps a set of pointers — the "remembered set" — into and out of the region. When a GC is deemed necessary, it collects the regions with less live data first (hence, "garbage first"). Often, this can mean collecting an entire region in one step: if the number of pointers into a region is zero, then it doesn't need to do a mark or sweep of that region.
For each region, it tracks various metrics that describe how long it will take to collect them. You can give it a soft real-time constraint about pause times, and it then tries to collect as much garbage as it can in that constrained time.
There is JavaOne talk about G1 and few articles on the topic:
The G1 is nicely explained also in this new JavaOne 2012 session : G1 Garbage Collector Performance Tuning [youtube], [PDF].
They start with introduction of CMS and G1, their comparison, and then the G1 analysis and tuning is explained.
G1 characteristics
A typical G1 heap may look like:
Here is a summary of each G1 phase:
1.1 Young Phase - Minor GC
1.2 Young / Initial Mark
2.1 Initial Mark - see 1.2.
2.2 GC remark
2.3. GC pause (mixed)
Note that G1 is intended to avoid Full GC as much as possible. As of Java 7u40, FullGC pauses in G1 are not optimized and are implemented as a single threaded operation. When using G1, try to avoid Full GC - if you see any FullGC pauses, your GC setup probably requires some tuning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With