SoFunction
Updated on 2025-03-01

Explanation of similarities and differences in Java's CMS and G1 garbage collection process

Similarities and similarities between CMS and G1 garbage collection processes

Garbage recycling

CMS garbage collection

The CMS garbage collector is a garbage collector based on the Mark-Sweep algorithm to obtain the shortest pause time. It is the first truly concurrent collector in the JVM. CMS garbage collection is divided into 4 processes

Initial mark(CMS initial mark)

  • STW, suspend all business threads
  • Tags the direct association object of GC Roots
  • The first object you can find is very fast

Concurrent tags (CMS concurrent mark)

  • Concurrent tags are the process of traversing the entire object graph based on the GC Roots' direct correlation object known by the initial tag. This process takes a long time, but does not require STW and GC threads to be performed simultaneously, which will cause floating garbage and missing tag problems.
  • During the process, the GC thread records the records of the black object referring to the white object and is used for the secondary check of the remark stage.

Remark (CMS remark)

  • Based on the missed labeling problem caused by concurrent marking, turn on STW and use the incremental update algorithm to secondary marking of the missed labeling phenomenon that occurs during concurrent marking. That is, black objects are set to gray, and the GC thread rescans the reference paths of these objects to avoid missing tags.
  • This stage will pause longer than the initial mark

Concurrent cleaning (CMS concurrent sweep)

  • GC thread starts to apply white objects
  • That is, no references are collected

Problems caused by CMS

  • Multi-label problem, resulting in floating garbage
  • Lacking problem, resulting in a bug

G1 garbage collection

The G1 garbage collector uses a mark-tidy algorithm as a whole, and in detail it is a copy algorithm. As a full-function and full-generation garbage collector, it is the default garbage collector after JDK9, which is used to replace CMS. The main process of garbage collection is the same as CMS, and it mainly has 4 stages.

Initial mark (Inital Marking)

  • STW, only tags objects that GC Roots can directly associate with, it takes a very short time, just like CMS

Concurrent tags (Concurrent Marking)

  • Starting accessibility analysis from GC Roots directly associates objects, scanning the object graph, it takes a long time to execute concurrently.
  • Similarly, it will cause floating garbage and missed labeling problems; in order to avoid missed labeling problems, SATB original snapshots are used to record objects with reference changes during concurrency. That is, the white object is broken from the reference of the gray object

Final mark (Final Marking)

  • STW, used to process the small number of SATB records left by the concurrent marking phase.
  • Grey the broken white objects and let the GC thread rescan the objects

Filter and recycling (Live Data Counting And Evacuation)

  • Responsible for updating Region statistics and sorting the recycling value and costs of each Region. Then, according to the user's expected pause time, multiple regions can be selected freely to form a collection (CSet)
  • STW, GC threads in parallel copy the surviving objects in the CSet that need to be recycled into the region in the idle state

Pay attention to explanation

  • Here, screening and recycling is actually a general and simple explanation solution, but it is not actually the case; recycling in the elderly is divided into two stage markings, one is the concurrent marking stage. 2. The garbage recycling stage. The concurrent marking phase is divided into 4 stages (initial marking, concurrent marking, remarking, and cleaning phases).This article combines the cleaning sub-stage of the concurrent marking stage and the garbage collection stage as filtering and recycling.
  • In fact, the cleaning sub-stage does not deal with garbage collection. It only counts the region sorting and obtains Cset, but still requires STW; the garbage collection stage only conducts copying and garbage collection of surviving objects.But every garbage collection will only happen at the beginning of a new round of YGC.. That is, whether it is YGC or Mixed GC, you will only get the CSet to be cleaned, and you will only clean it when a new round of CG comes, that is, delay cleaning. Although we don't know what the benefits are

CMS, G1 strategy for missing tags for three-color marks

Whether it is CMS or G1, the three-color marking algorithm is used to deal with the problem of concurrent garbage collection, but different behaviors are adopted for the possible missed marking behavior.

Incremental update strategy

  • During the concurrency marking phase, the changes of the white object referenced by the black object are recorded
  • During the remarking phase, STW, graying the black object, rescan the gray object and its reference path

In my understanding, the incremental update strategy is similar to recording such information in the insertion barrier of a black object referencing a white object, and handing it over to the remark stage for processing.

SATB (snapshot-at-beginning) strategy

  • In the concurrency marking stage, recording changes in white objects that are broken from gray objects. To put it bluntly, recording the original reference information and subsequent processing
  • During the final marking phase, STW processes these changes, sets the broken white object to gray, and finally waits for the GC thread to scan

My understanding is that SATB also uses a write barrier, but uses a deletion barrier, that is, when the white object is disconnected, record the original pointing information because it believes that all objects recorded at the beginning must be scanned. Similarly, it is handed over to the final marking stage to process

Golang Strategy

  • Insert/Delete the barrier
  • Mixed writing barrier

What are the advantages of G1? Why can I replace CMS?

Advantages of G1

  • Has a predictable pause time model that supports users to set the expected pause time; that is, G1 can allow pause time to float as much as possible within the range the user wants
  • Brand new design, recycled throughout the age, simple and efficient
  • Compared with CMS, no space debris is generated

Disadvantages of G1

  • Compared with CMS, it requires more system memory footprint, such as occupancy of 10 to 20% of the total space

Replace scene

  • On CMS, assuming that in the flash sale scenario, a large number of requests come in an instant. If the young generation is set too small, it will cause the Survice area to survive, causing the new object to be unable to enter the young generation and be guaranteed to enter the old generation. Gradually accumulating will lead to Full GC. Especially in high concurrency scenarios, this can easily lead to frequent Full GCs, which will also cause serious lags.
  • On CMS, the solution is also very simple, because when instant killing, most of the objects die in a short time, and they are basically not old people. So we only need to reduce the size of the old generation and increase the size of the young generation to solve this problem
  • On G1, it is easier to solve because the size of the younger generation will be automatically adjusted according to each YGC. We don’t need to actively set it up. G1 can solve this problem by itself and choose the appropriate ratio of young generation and old generation.

question

Why does CMS initial tag require STW?

  • If the initial tag is not STW, there will be constant local variables appearing during the program running, so we don’t know when the tag will be considered as the end
  • At the same time, the initial marking takes a very short time, so the instant STW will not affect anything

Why does G1 filtering and recycling require STW?

  • First, we need to filter the recycling stage. We need to divide it into two parts. One part is to conduct regional spam statistics and select the specified CSet according to the sorting. This part requires STW to avoid the trouble caused by concurrency.
  • Another part is garbage cleaning, because it involves copying of surviving objects, copying objects from one area to another, without STW, may cause object coverage issues

In the concurrent marking stage, if there is no STW, how should we deal with the newly added object?

There are several possible situations for newly added objects

  • The newly added object is a GC Roots direct association object
  • The newly added object is referenced by the black object
  • Newly added objects are referenced by white or gray objects

Assuming that the newly added object is assigned as a white object, there are several problems

  • If it is GC Roots, the object is directly associated with, because the initial tag has been missed, the new object will be considered garbage and recycled.
  • If it is referenced by a black object and there is no other reference path, this will cause the problem of missing tags and will still be considered garbage collected.
  • If it is a white or gray reference, then there is no big problem

In any case, there is still a problem that newly added objects are collected as garbage, so it is problematic to directly allocate new objects as white objects. How should we deal with them reasonably?

In different languages ​​and different garbage collectors, I guess it should be handled by different ways. Here we can list them

  • In Go 1.8, under the three-color mark + mixed write barrier strategy, all newly added objects are marked gray. So the new object will definitely be scanned by the GC thread. The advantage is to avoid missing marks, and the disadvantage is that it may be multi-marked, causing floating garbage
  • Similarly, I guess a similar solution is used for CMS or G1.

What garbage collection algorithm does G1 use?

It is usually said on the Internet that G1 uses a mark-organization algorithm, and it can be understood in this way as a whole.

- Overall marking - Organizing, Detailed copying algorithm

- G1 will copy the active objects in the region that need to be recycled to an free partition, and then clean the recycling region and then classify it as an free partition. This process is like marking or replication algorithm

  • G1 only provides YGC and Mixed GC options. If Mixed GC really cannot keep up with the speed of the program allocating memory, causing the old age to fill up and cannot continue with Mixed GC, it will use Serial Old GC (Full GC) to collect the entire GC heap. So we can know that G1 does not provide Full GC, and we must avoid Full GC
  • There is only serial Full GC between JDK10, and after 11, parallel Full GC will be

How to understand G1 GC and garbage collection process?

The concurrent marking stage of G1 is only aimed at the analysis of old-age objects, that is, the garbage recycling stage we described above is for G1 old-age recycling, and the new generation recycling is simpler than the above. The entire process of STW and GC threads recycle new generation objects in parallel

In fact, Mixed GC includes the entire process of YGC, that is, if Mixed GC occurs, then YGC must have happened before. We can simply understand that the process of YGC has only two stages (initial marking, screening recycling). After the initial marking, when the heap memory occupies 45% of the total memory available (can be set), the concurrent marking level will be triggered. This means that the progress from YGC to Mixed GC, and then the normal process will be followed.

The beginning of Mixed GC, based on the completion of G1 YGC, the concurrent marking process is the root of the active object recorded by the concurrent marking thread from the Survivor area or the RSet of the old age and starts marking.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.