Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process of garbage collection CMS?

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly talks about "what is the process of garbage collection CMS". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what the process of garbage collection CMS is like"!

1. Introduction of CMS

The full name is Concurrent Mark Sweep, which is a concurrent garbage collector that uses the tag-cleanup algorithm.

1. Parallel, STW time is short.

2. No compression and defragmentation, resulting in memory fragmentation.

During the marking process, objects are divided into three categories according to the situation of the tag:

1. A white object that indicates that it is not marked.

2. A gray object that indicates that it is marked, but the internal reference is not processed

3. Black object, indicating that itself is marked and internal references are processed.

Trigger time: if the following parameters are added

When the utilization rate in the old days reached 80%, a cms gc was triggered.

Second, the collection process of CMS is divided into six steps.

Suppose the heap structure before CMS GC looks like this:

1. Initial tag (InitialMarking)

This is a STW process, which is divided into two steps

1. Mark the old objects that can be reached by GC Roots

2. Traversing the old objects that can be reached by the new generation of objects under GC Roots

3. This process does not carry out further reachable scanning of the above accessible old objects.

Results:

2. Concurrent tagging (Marking)

The GC thread and the application thread execute concurrently in this stage, traversing the living objects marked by the InitialMarking phase, and then continue to recursively mark the reachable objects of these objects.

This process is running by the application thread, and Young GC may also happen, and the following will happen:

1. The new generation is promoted to the old age.

2. Distribute the object in the old days

3. The citation of objects in the new and old years has changed.

Results:

2.1. So how do you deal with changes in objects during concurrent markup?

CMS uses the Card Table described in the previous section to solve this problem

The Card where the changed objects are referenced during concurrent tagging is recorded as a "dirty card" in Card Table, so that these objects will also be traversed as GC Root when they are remarked later.

But if Young GC happens, for example:

1. The concurrent tag has not yet scanned the dirty card 1.

2. Young GC scans dirty cards and changes dirty to clean.

3, concurrent mark scan, found that card 1 is no longer a dirty card, it will not be processed, which results in a missing mark.

2.2. What if the above problems are solved?

In CMS, there is another data structure (Mod Union Table)

Mod Union Table is a bit vector, the size of each cell is only 1 bit, and each cell corresponds to a Card (the size of Card is 512 bytes, the size of each cell of Card Table is 1 byte)

Before the new generation of GC deals with dirty card, set the corresponding item of the card in Mod Union Table.

In this way, when CMS performs the relabeling phase, it scans for marked items in Mod Union Table and card table.

3. Pre-cleaning (Precleaning&AbortablePreclean) 3.1 Precleaning

Select to disable this phase through the parameter CMSPrecleaningEnabled, which is enabled by default, and mainly does two things:

1. Deal with the references that have been discovered by the new generation, for example, in the concurrency phase, an An object is assigned in the Eden area, and the An object references an old object B (which has not been marked before), and object B is marked as the active object at this stage.

2. In the concurrent marking phase, if there is a change in the internal references of objects in the old years, the Card will be marked as Dirty (including ModUnionTalble). By scanning these Table, the objects whose references are updated in the concurrent marking phase will be re-marked.

3.2 、 AbortablePreclean

The premise that this stage occurs is that the memory usage of the new generation Eden area is greater than the parameter CMSScheduleRemarkEdenSizeThreshold is 2m by default. If there are too few objects in the new generation, it is not necessary to perform this stage and perform the relabeling phase directly.

Why do we need this stage? what is the value of existence?

Because the ultimate goal of CMS GC is to reduce the pause time during garbage collection, we should try our best to deal with the older objects that are updated by the applied thread during the concurrency phase, so that less processing can be done during the relabeling phase of the pause, and the pause time will be reduced accordingly.

At this stage, the main cycle does two things:

1. Handle objects in From and To areas and mark up old objects that can be reached.

2. As in the previous stage, scan and process objects in Dirty Card and ModUnionTalble.

Of course, this logic doesn't go on forever, and there are three conditions that interrupt the cycle:

1. You can set the maximum number of cycles (CMSMaxAbortablePrecleanLoops). The default is 0, which means there is no limit on the number of cycles.

2. If the time to execute this logic reaches the threshold CMSMaxAbortablePrecleanTime, the default is 5s, and the loop will exit.

3. If the memory utilization of the new generation Eden reaches the threshold CMSScheduleRemarkEdenPenetration, the default is 50%, and the loop will be exited.

4. Relabeling (the process of STW)

In the previous parallelism phase, new reference relationships might be generated as follows:

1. New objects of the old era are referenced by GC Roots.

2. The untagged objects of the old years are referenced by the new generation objects.

3. Objects marked in the old years are added with new references pointing to other objects in the old years.

4. The reference to the old age of the new generation object has been deleted.

Some of the above objects may have been processed in the Precleaning and AbortablePreclean phases, but there are always some that have not been handled in time, so you need to do the following:

1. Traverse the new generation of objects and re-tag them

2. Re-mark according to GC Roots

3. Traverse the old Dirty Card and Mod Union Table, and re-mark them

In step 1, you need to traverse all the objects of the new generation. If the utilization rate of the new generation is very high, there are many objects that need to be traversed, which is a disaster for the total time spent at this stage. (because a large number of objects may be temporarily alive, and these objects may also refer to a large number of old objects, resulting in a lot of old objects that should be recycled without being recycled. The number of iterations has also increased a lot.) if a YGC occurs before that, you can avoid scanning invalid objects.

A parameter is provided in the CMS algorithm: CMSScavengeBeforeRemark, which is not enabled by default. If this parameter is enabled, a YGC will be forced to be triggered before the execution of this stage, which can reduce the traversal time of the new generation of objects and recover it more thoroughly.

5. Concurrent cleanup

Cleanup collects objects identified as unreachable during the marking phase

6. Reset

Clear the data structure and prepare for the next concurrent collection.

At this point, I believe you have a deeper understanding of "the process of garbage collection CMS". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report