We talked about the CMS recycler before, but at that time we said that the CMS recycler was outdated, and now it was time to use the G1 recycler. So what's the magic of G1 recycler? What is it better than CMS recycler? Today, let Brother Tree take everyone to make a plate!
Article mind map
History of G1 reclaimer
The G1 (Garbage First) collector was determined to be done as early as JDK1.7, but it was not officially launched until JDK7u4. After JDK9, it became the default garbage collector and abandoned the CMS collector.
G1 Reclaimer characteristics
G1 collector is a garbage collector for server applications. Its long-term mission is to replace CMS collector. The G1 recycler is similar to the CMS recycler, for example: All collectors focus on GC pause time and adopt the idea of generational recovery 。
However, in terms of overall implementation, G1 recycler has made many improvements, which can be said to be a comprehensive improvement of CMS recycler. Compared with CMS recycler, G1 recycler has the following differences:
Adopt partition idea of breaking up into parts Garbage collection algorithm based on mark and sort Predictable GC pause time
Zoning idea
For CMS and previous collectors, the JVM memory space is divided into a large area that is physically continuous according to the idea of generations, as shown in the following figure.
However, although the G1 collector also adopts the idea of generation, it does not allocate a continuous memory for it, but breaks the whole memory into parts and regions, as shown in the following figure.
As shown in the figure above, the G1 recycler no longer divides large blocks of memory for the young generation and the old generation, but divides them into regions. Each region is marked as the young generation or the old generation. In G1, there is also a Humongous region, which was born to optimize the allocation of large objects.
The region design idea that G1 recyclers break up into parts is the core of G1 recyclers being more powerful than CMS recyclers. By breaking the large memory into pieces, the G1 collector can more flexibly control the GC pause time, and also solve the memory fragmentation problem of the CMS collector and the long GC pause time problem under large memory.
Marking sorting algorithm
Another big difference between the G1 collector and the CMS collector is that the G1 collector uses the Mark Clean algorithm, while the CMS collector uses the Mark Clear algorithm. Therefore, the CMS recycler will generate a lot of memory fragments, while the G1 recycler does not have this problem.
Some children will ask: Why does the CMS collector not use the "mark and sort" algorithm?
It is very simple. Because the CMS collector is very old, using the "mark and sort" algorithm requires a long GC pause time, which will lead to a longer interface response time. In fact, CMS recycler provides -XX:+UseCMSCompactAtFullCollection The parameter is used to implement memory compression, but the GC pause time will be very long during memory compression, which will lead to a longer interface response time.
The curious baby asked again: The G1 collector also uses the "mark and sort" algorithm. Why won't it lead to a long GC pause?
It is very simple, because the G1 collector uses the idea of dividing regions, which breaks up large blocks of memory into regions. In addition, it also maintains a list of regions to be recycled. You can select the region with the highest cost performance ratio for recycling to achieve flexible control of GC pause time.
See, the region design idea of G1 recycler breaking into parts is really the killer of G1 recycler!
Predictable pause time
The G1 collector also has a great advantage for CMS, that is, it can establish a predictable pause time model, which allows users to clearly specify that the time consumed in garbage collection should not exceed N milliseconds within a time segment of M milliseconds. This feature is still rarely used, so you can understand it.
Waste recycling process
Compared with the CMS collector, the garbage collection process of G1 collector is more special. It adopts two garbage collection methods, namely "young generation collection" and "mixed collection".
Collection of young generation
When the application just started, the traffic slowly came in, and the JVM began to generate objects. G1 will select a partition and specify the eden partition. When this partition is full, G1 will select a new partition as the eden partition. This operation will continue until the maximum limit of eden partition is reached, and then a young generation collection will be triggered.
In the s, the "replication algorithm" was used for collection, which first used single eden and dual survivors to migrate surviving objects. During migration, objects will be promoted to the old generation partition according to their age and other characteristics, and the original young generation partition will be recycled. The rules involved in this process are similar to those of the CMS collector, except that the G1 collector breaks the memory into parts.
Mixed collection
Over time, more and more people are promoted to the elderly. When the proportion of the old age (the proportion of the Java heap memory) reaches the InitializingHeapOccupancy Percent parameter, the JVM will trigger "hybrid collection" for garbage collection. It should be noted that hybrid collection will collect the memory of the younger generation and some older generations, which is not the same as Full GC. Full GC will recycle the memory of the whole old generation.
For the hybrid collection mode, the collection process can be divided into four stages:
Initial mark Concurrency mark Final marking Filter recycling
Initial tag. Like the CMS collector, this stage simply marks the objects that GC Roots can be directly associated with, so that subsequent GC collector threads can execute concurrently with user threads. Stop the World is required in the initial marking stage.
Concurrency flag. This phase is the same as the CMS collector. It starts from the GC Root to analyze the accessibility of objects in the heap and find out the surviving objects. This phase takes a long time, but it can be executed concurrently with the user program without "Stop the World".
Final marking. This phase is the same as the CMS collector. It is used to correct the reference change caused by the user program continuing to operate during the concurrent marking period. But the G1 collector is implemented in different ways. At this stage, "Stop the World" is required.
Filter recycling. This phase is the same as the concurrent cleaning of the CMS collector, which is to clean up the objects marked as garbage. Only for the G1 collector, it will maintain the recovery value and cost of each region, and then specify the recovery plan according to the expected GC pause time.
From Understanding Java Virtual Machine in Depth
On the whole, we can find that the mixed collection process of the G1 collector is very similar to that of the CMS collector, which goes through several stages: initial marking, concurrent marking, final marking, filtering recovery (concurrent clearing).
summary
From the official launch of JDK7 to JDK9 becoming the default garbage collector, the G1 collector has defeated the CMS collector in two generations.
From the realization of G1 collector, its pioneering region design idea of breaking up the whole into parts is undoubtedly the secret of defeating CMS collector. Through this design idea, the G1 collector can control the GC pause time more flexibly, and also realize more efficient and complex functions, such as selecting the best recovery region according to the recovery space and time consumption, and predicting the GC pause time.
Original link:
//mp.weixin.qq.com/s/YxmjzzPFsREWHqtbGDK4aQ