Go tri-color flag GC

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Tri-Color Marking

The three-color labeling principle is as follows:
The entire process space for each object occupied by the memory can be treated as a graph, the initial state of each memory object is a white flag, stop the world, the scan task as multiple concurrent goroutine immediately queue to the scheduler, and then CPU processing, The first round first scan all the accessible memory objects, marked gray into the queue, the second round can restore start the world, the first step in the Queue object reference object to gray queue, the object refers to all objects are gray and queued, the object can be set to black and removed from the queue. After the last queue is empty, the remaining white memory space of the entire graph is the unreachable object, that is, the object that is not referenced; Stop the world again in the third round, mark the memory for the new object in the second round (gray), This uses the Writebarrier (write barrier) to record the identity of these memory;

The entire GC process is as follows:

Note that:
Mark has two of processes.

The first step is to start with Root, which includes the global pointer and the pointer on the Goroutine stack, marked as gray. Traverse the gray queue.

Re-scan global pointers and stacks. Because Mark and the user program are parallel, there may be a new object assignment at the time of Procedure 1, which needs to be recorded through the write barrier. Re-scan finish the check again.

Stop the world has two of processes.

The first is when the GC is about to start, and this time it's mostly about prep work, like enable write barrier.

The second process is the re-scan process mentioned above. If there is no STW at this time, then mark will be endless.

When Mark is finished, start the world is in parallel, and for parallel cleanup, the GC initializes the Bgsweep () and blocks it in the background, wakes the process up at the start of the cleanup, and makes the concurrent sweep for the main m.

Memory management is based on span, MHEAP_ is a global variable, and all assigned objects are recorded in Mheap_. At the time of tagging, we just have to find the corresponding span of the object to mark, sweep the time to scan span, no marked span can be recycled.

In addition: 1.8 After the Golang will be the first step of the Stop the world also canceled, this is an optimization.

Write barrier

About the usefulness of the write barrier as in the following example, this example modifies a question and answer on a self-knowledge basis, in which we express our gratitude:

Before GC:
Stack->a->b; A is the object requested in the stack, B is the object requested in the heap, and a reference to B exists in a object;

Stack->c; C is also the object to be applied in the stack.

Stop the world, mark. Here the a,c will be marked gray; B is white
Start the world and repeat mark.
Because it is concurrent mark, we assume that C is processed first, C does not refer to other objects, so it is directly black, removed from the queue, C is black, A is gray, B is white

Suppose at this point the user does the following:

A=nil
New (d);
c->b; That is, the reference to B in A is null (you can also understand that the reference to any other memory object in a is emptied), the D object is requested, and a reference to B is added to C.

Because C is already black, so will not scan him, then this memory scan will not be able to find B, and D object because it has just been applied, has not been quoted, so here only a mark:a: black, B: white; c: black; D: White

Then the user did:
b->d; because B cannot be scanned, it is clear that D will not be scanned. This will continue until the end of this round of Mark (that is, the gray queue is empty).

Stop the world, mark termination. Sweep At the end of the GC, B,D's memory space is white, so it will be cleared when sweep. How to avoid this kind of mistake clean-up?

The function of the write barrier is to make a mark on B when the c->b occurs, and a mark on D when the a->d occurs, so that when we enter the second stop of the world, mark termination, we make sure there is no more reference to the new memory. During a repeat mark, although it is not possible to scan B and D through c->b->d, we can scan the two remaining white objects of b,d, marked as gray, and eventually marked black, due to the marker of the write barrier.

In short, the function of the write barrier is roughly the ability to capture a new memory reference to a white object during Mark's time without STW.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.