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

How to understand the tricolor marking algorithm in Go garbage collection

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

How to understand the tricolor marking algorithm in Go garbage collection, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Introduction to tricolor labeling:

Tricolor tagging (tricolor mark-and-sweep algorithm) is an improvement of traditional Mark-Sweep, it is a concurrent GC algorithm, which is used as a garbage collection algorithm in Golang, but it also has a defect that the speed of garbage generation in the program may be faster than the speed of garbage collection, which will lead to more and more garbage in the program can not be collected. The principle is as follows:

Step 1: create: White, gray, and black collections.

Step 2: put all objects in a white collection.

Step 3: traverses all objects from the root node, putting the traversed objects from the white collection into the gray collection (Note: the objects that are put in the gray collection here are the objects of the root node).

Step 4: iterate through the gray set, putting the objects referenced by the gray object (Note: all objects referenced by the gray object, including those indirectly referenced by the gray node) from the white set to the gray set, and then put the analyzed grey objects into the black collection.

Step 5: until there are no objects in gray.

Step 6: use the write barrier (write-barrier) to detect changes in objects and repeat the above operations (Note: because mark and the user program are parallel, there may be new object assignments when the previous step is executed, and the write barrier was introduced to solve this problem).

Step 7: collect all white objects (garbage).

Examples are as follows:

1. In the initial phase, assume that the current object invocation is as follows: root- > A-> Bhand A-> C hand A-> F; E; G-> H

According to the algorithm, all objects are placed in a white set, corresponding to step 1 and step 2.

2. GC starts scanning, where it starts from the root node. Traversing finds that only An and F are root nodes, so move An and F from the white set to the gray set. After the white combination, there are B, C, D, E, G, H nodes, corresponding to step 3.

3. GC continues to scan the gray set, and the nodes referenced in the nodes in the grey set will be moved to the grey set. In this example, the nodes B, C, D referenced by node A will be moved to the grey set, and then A finds that all the child nodes he references are already in the grey set, so they will be moved to the black set. At the same time, the F node will be moved to the black set without self-nodes. Corresponds to step 4.

4. GC loops through the gray set until there are no nodes in the gray set. In this case, when it is found that B, C, and D have no children in the white set, B, C, and D are all moved to the black set, corresponding to step 5.

5. At this point, only E, G, H are left in the white set, and the remaining objects are in the black set. GC clears the objects in the white set, that is, to recycle these objects, corresponding to step 7.

6. After the above garbage collection, GC will do one step, that is, change the black collection to a white collection for the next garbage collection.

After reading the above, have you mastered how to understand the tricolor marking algorithm in Go garbage collection? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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: 204

*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

Internet Technology

Wechat

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

12
Report