Garbage First - G1 Garbage Collector

Garbage First, commonly known as G1, was proposed in 2004, experienced JDK6 in 2006, officially supported by JDK7.0 in 2012, and then the default garbage collector of JDK9 in 2017. The CMS garbage collector was abandoned in JDK9, thus indicating that G1 replaced the CMS garbage collector.

G1 focuses on throughput and low latency at the same time, and is also a concurrent garbage collector. G1 is also suitable for super-large heap memory. It divides the heap memory into multiple regions of equal size. Each region is 1, 2, 4, and 8M in size. Each region can be independently used as the Garden of Eden, the survivor area, and the old age. The size of this region can be passed through -XX:G1HeapRegionSize=size (Size must be 1, 2, 4, 8, 16). If the heap memory is too large, the corresponding recycling rate will become slower and slower, because it involves the assignment of objects , and the whole is divided into small areas for management, which can be optimized and the rate of marking and copying can be accelerated. Overall, G1 uses the mark+sort algorithm, but the copy algorithm is used between the two regions. In JDK1.8, G1 is turned off by default, so you need to use the command -XX:+UseG1 to turn on the G1 garbage collector.

garbage collection phase

Oracle JVM engineers divide the garbage collection phase of G1 into three phases: Young Collection (garbage collection of the new generation), Young Collection+Concurrent Mark (garbage collection of the new generation, and perform some concurrent marks), MixedCollection (mixed collection), These three stages are a cyclical process. The garbage collection of the new generation has just started. When the memory of the old generation exceeds a threshold, concurrent marking will be performed during the garbage collection of the new generation. After this stage is completed, a mixed collection will be performed. , Mixed collection is a large-scale collection of the new generation, the survivor area, and the old generation. After the mixed collection is over, the memory of Eden will be released, and then it will enter the garbage collection of the new generation again.

What Oracle engineers say about the JVM

As mentioned above, the G1 collector will divide the heap memory into an area of ​​equal size. Each area can be independently used as the Garden of Eden, the Survival Area, and the Old Age. As shown in the figure below, the white area at the beginning is a free area. area, when the white free area is full, it will trigger a garbage collection of the new generation, which will trigger a STW.

New generation memory layout

 

The second stage of G1 garbage collection: the new generation of garbage collection and concurrent marking stage (Young Collection+CM). In the process of garbage collection, it is necessary to perform initial marking and concurrent marking on the GC Root object. Initial marking is to find those root objects, and concurrent marking is to find other objects along the root object and mark them. The initial marking is performed during the new generation GC. The initial marking does not take up the time of our concurrent marking, but only occurs during the garbage collection of the new generation. Concurrent marking will only occur when the proportion of heap space occupied by the old generation reaches a certain threshold (it will not cause other user threads to suspend, that is, it will not STW), and the threshold here can be controlled by a JVM parameter——XX:InitiatingHeapOccupancyPercent=percent (Default is 45%).

The third stage of G1 garbage collection: Mixed Collection. At this stage, the three areas of E, S, and O will be fully garbage collected. As shown in the figure below, the surviving objects in the Garden of Eden will be copied to the surviving area through the copy algorithm, and some of the surviving areas that are not old enough will be re-copied to the new surviving area, and some that meet the promotion conditions will be directly copied to the old generation Go, these belong to the garbage collection of the new generation. There is also a part of the old generation area that has also gone through the marking concurrency stage, and some objects are found to be useless. However, at this time, the G1 garbage collector will selectively recycle according to the maximum pause time. There are only two red old generation areas below. Recycling, others do not, because sometimes the heap memory space is too large, at this time, the garbage collection time of the old generation will be longer, and the maximum pause time will not be reached. In order to achieve this time, G1 will start from these old generation In the era, select those areas with higher recycling value, that is, when this area is recycled, more space will be released. So this also explains why it is called Garbage First? It is because it will give priority to recycling those areas with more garbage in this link. At each stage of the mixed collection, there is a final mark, and the copy survival will be STW,

 

Guess you like

Origin blog.csdn.net/qq_35363507/article/details/105079951