Categories
Uncategorized

G1 collector Introduction

Background: Since the CMS algorithm creation of space debris and other defects in a series of problems, HotSpot provides another garbage collection policy, G1 (ie, Garbage First) algorithm, which was officially launched in JDK7u4 version, the official website of this is described as follows:

The Garbage-First (G1) collector is a server-style garbage collector, targeted for multi-processor machines with large memories. It meets garbage collection (GC) pause time goals with a high probability, while achieving high throughput. The G1 garbage collector is fully supported in Oracle JDK 7 update 4 and later releases. The G1 collector is designed for applications that:
  • Can operate concurrently with applications threads like the CMS collector.
  • Compact free space without lengthy GC induced pause times.
  • Need more predictable GC pause durations.
  • Do not want to sacrifice a lot of throughput performance.
  • Do not require a much larger Java heap.

G1 is planned as the long term replacement for the Concurrent Mark-Sweep Collector (CMS). Comparing G1 with CMS, there are differences that make G1 a better solution. One difference is that G1 is a compacting collector. G1 compacts sufficiently to completely avoid the use of fine-grained free lists for allocation, and instead relies on regions. This considerably simplifies parts of the collector, and mostly eliminates potential fragmentation issues. Also, G1 offers more predictable garbage collection pauses than the CMS collector, and allows users to specify desired pause targets.

G1 Collector is a server-style garbage collector, for a large memory and multi-cpu machines. It is at the same time meet the high throughput, and achieve as much as possible low GC time, G1 Collector mainly for the following scenarios:

    Like CMS, like garbage collection and application threads concurrently executing threads

    Free memory compression, avoid prolonged GC result in suspension

    To have a better prediction of GC pause time

    I do not want to sacrifice too much throughput performance

    Hope can be controlled as much as possible the size of the heap of (G1 itself for piles, piles above are generally considered 4-6G, so I personally understand the magnitude here is on this basis)

For before the garbage collectors who (for example, the first serial, later parallel, CMS), they will heap is divided into three parts: the new generation, old time, permanent-generation (dimensional space)

Different G1, G1 will heap algorithm is divided into a number Region, it is still divided generations. Case and default heap memory is divided into 2048 parts, that is, the size of each region of the heap size / 2048, but the region can only be the size of 1M, 2M, 4M, 8M, 16M and 32M, in short, is a power of 2 , usually generate about 2000 region

1. G1 Heap Structure

 2. G1 Heap Allocation

As shown above, these are defined as the region eden, survivor, old generation logic contiguous memory space. Which eden, survivor, old generation with the past as GC collector, in addition to a conventional algorithm Humongous area is not, if an object space occupied by more than 50% of the area capacity, G1 collector will think this is a giant object. These giant objects, the default will be assigned directly in the old generation, but if it is a short-lived massive objects, it will have a negative impact on the garbage collector. To solve this problem, G1 divided a Humongous area, which is used specifically to store huge objects. If a H area fit a giant object, then the G1 will look for consecutive H partition to store. In order to be able to find a continuous H area, and sometimes have to start Full GC.

3. Young Generation in G1

Young GC mainly for Eden zone GC, it will be triggered when the Eden space is exhausted. In this case, the data is moved to Survivor space Eden space, Survivor if the space is insufficient, part of the data Eden space is promoted directly to the old generation space. Data movement Survivor Survivor area to the new area, there are also promoted to the old part of the data’s space. The final data Eden space is empty, GC stops working, the application thread to continue execution.

4. A Young GC in G1

Live objects are copied to one or more survivor region, if the age of some objects reach the threshold required for promotion years old, will be promoted to the old era. (Similar to the previous algorithm with), G1 young gc belong to stop the world (STW) nature, and in the meantime, the number and amount of survivor region of eden region will be through a new round of calculation (that is, the number is in the dynamic region changes), to accommodate the next round of the GC to better achieve the desired pause time.

5. End of a Young GC with G1

Survival object is evacuated to survivor region or old generation region, the object represented by the promotion in FIG dark portion, representing blue Survivor regions

Old Generation Collection with G1

As more and more objects promoted to the old years old region, in order to avoid heap memory is exhausted, it will trigger the Old Generation Collection (also known as mixed gc), old gc G1 of old gc algorithm is not the same as before , G1 old gc is at the same time recover the entire young region, but also recovered part of the old region, It should be noted, is part of the old region, but not all, G1 algorithm selection part old region for recycling, allowing for the consumption of garbage collection when the time control. Trigger conditions: -XX: InitiatingHeapOccupancyPercent = n years old when the size of the total percentage of the size of the heap reaches this threshold, it will trigger a old gc

old gc execution process:

1. initial mark: The whole process STW, marked up objects from the GC Root 2. concurrent marking: Concurrent marking process, the whole process gc collector threads execute in parallel with the application threads to find whole heap of live objects 3. remark: the STW whole process, the final flag, using the snapshot-at-the-beginning (SATB) algorithm (much faster than CMS use) concurrent those marked missing marking process, or an internal reference object is changed 4. clean up: the STW process part processing, parallel processing section

1. Statistical live objects and completely free region (STW)

2. Empty Remembered Sets (STW)

3. Place the empty region added to the free list (concurrent)

5.Copying: The whole process STW, copy live objects to a new region in

 

Full GC

In some cases, triggered G1 Full GC, then G1 will be degraded using Serial garbage collector to complete the clean-up work, it uses only a single thread to complete the work GC, GC pause time will reach the second level. The entire application in suspended animation, unable to process any request, we certainly do not want to see these programs. Full GC happens then what does?

    Concurrent mode failure

G1 start marking period, but before the Mix GC, it is filled years old, this time period marked G1 will give up. In this case, it is necessary to increase the size of the heap, or adjust the period (e.g., increasing the number of threads -XX: ConcGCThreads etc.).

    Promotion failure or failure Evacuation

G1 during the GC when there is not enough memory for the survival of the object or objects to use for promotion, thereby triggering a Full GC. You can be seen (to-space exhausted), or (to-space overflow) in the log. Way to solve this problem is:

    Increase -XX: G1ReservePercent value of the options (and a corresponding increase in the total heap size), as a “target space” to increase the amount of memory reserved.

    By reducing the -XX: InitiatingHeapOccupancyPercent early start marking period.

    You can also increase the -XX: to increase the number of parallel threads marked values ​​ConcGCThreads options.

    Giant object allocation failure

When the giant objects not find a suitable space allocation, it will start Full GC, to free up space. In this case, a large number of distribution should be avoided huge objects, additional memory or increase -XX: G1HeapRegionSize, that the object is no longer giant huge objects.

 

Leave a Reply