These three characteristics make G1 replace CMS. The main characteristics of g1 are incorrect

8 minutes
two hundred and twenty-two
zero

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

This article is written by: Chief Editor Published on Software Development of Little Turkey , please indicate the source for reprinting: //hongchengtech.cn/blog/4675.html
Kuke_WP editor
author

Related recommendations

1 year ago (2024-02-20)

Industry Fit! Preferred element of WMS warehouse management system, wms warehouse software

Enterprise managers often think that warehouses are inefficient, high cost places, and belong to heavy asset operations. With the development of enterprise business, if the warehouse needs to be expanded in traditional ways, the cost is relatively high. At the same time, it also faces problems such as lack of operating experience. In the operation link, the process of warehouse, allocation, human resource matching and management is very complicated, and the team's professional ability is also highly required
seven hundred and eighty-three
zero
1 year ago (2024-02-19)

Supply chain billing system management (I): system overview, what are the supply chain management fees

In recent years, with the continuous development of e-commerce industry and increasing business, everyone has started to distribute goods online, and the supply chain billing system needs to manage more and more things. How to manage the billing system? The author summarizes some contents about settlement based on his own practical experience, hoping to enlighten you. After working on the warehouse management system for several years, I was transferred to work as a supplier
five hundred and fifty-four
zero
1 year ago (2024-02-19)

Multi merchant system management - store background design, what is the meaning of multi merchant classification

Simply understood, multi merchants are a large mall. The platform can manage merchants who settle in the mall. The merchants who settle in the mall have independent backstage. They can log in and add goods to the shelves by themselves, manage stores by themselves and other information functions. Then how to design the backstage of the store? Let's see the author's sharing. I hope it can help you. 1、 Introduction The backstage of the store is an important part of the e-commerce platform
six hundred and forty-six
zero
1 year ago (2024-02-19)

Jiangyang District of Luzhou City took the lead in the city's full coverage training on domestic waste classification management regulations, Luzhou waste treatment

Source: Original Draft On January 10, the People's Congress of Jiangyang District, Luzhou City and the District Government jointly carried out a training on the regulations of the Regulations on the Classified Management of Domestic Waste in Luzhou City (the Regulations for short), and invited Lei Zhengyun, the chairman of the Legislative Affairs Committee of the Municipal People's Congress, to give a live lecture, so as to guide the comprehensive and systematic grasp of the contents and legal functions and responsibilities of the Regulations, deeply understand the specific specifications of the Regulations, and quickly set off
three hundred and seventeen
zero
1 year ago (2024-02-19)

Simeng CMS (smcms) content management system, Simeng Central Primary School

SMCMS (Simon CMS) is a content management system developed based on the microbee http rapid development framework. Product development follows the concept of simplicity, security, high concurrency and efficiency. Enterprise level web content management software for high-end users is designed to help users solve the increasingly complex and important web content creation, maintenance, publishing and response
three hundred and sixty-one
zero
1 year ago (2024-02-19)

Does the website have to install a content management system?, What apps are needed to install software on the website

1: The role of the website is to let companies or enterprises display their own windows, but also to let more customers or potential customers know their work and products. Through the website, customers can understand their products and services more intuitively, and can also provide more services to meet customer needs. 2: The role of the content management system The content management system can help
four hundred and fifty-six
zero

comment

0 people have participated in the review

Scan code to add WeChat

contact us

WeChat: Kuzhuti
Online consultation: