Garbage collection mechanism of GO language

Generation of memory garbage

The program is divided into five parts in memory: heap area, stack area, global data area, code segment, and data area . For early programming languages ​​such as C++, the compiler is responsible for managing and reclaiming the memory on the stack, while the memory space on the heap requires programmers to apply and release. In Go, the memory on the stack is still managed and recovered by the compiler, while the memory on the heap is managed and recovered by the compiler and garbage collector, which brings great convenience to programmers.

 Garbage refers to the memory space that the program applies to the stack. With the running of the program, these memory spaces are no longer used. If they are not released at this time, garbage will be caused, that is, memory leaks.

 package main

 //假设每个人都拥有自己都一部手机
 type Person struct {
    phone *Phone
 }

 type Phone struct {
    money int
 }

 func main() {
    //定义一个Person为超超
    chao := new(Person)

    //超超一开始用的是iphone12
    iphone := &Phone{money: 6599}
    chao.phone = iphone
    //华为推出了鸿蒙,于是超超果断入了一部mate40
    huawei := &Phone{money: 5899}
    chao.phone = huawei

 }

As Chaochao changed its mobile phone from an iphone to a Huawei, the memory space pointed to by the phone becomes garbage. At this time, it is necessary to reclaim the memory space pointed to by the phone , otherwise it will become a memory leak.


Golang Garbage Collection Mechanism

Go1.3 uses the mark-and-clear method , Go1.5 uses the three-color mark method , and Go1.8 uses the three-color mark method + hybrid write barrier .


mark-and-sweep

Divided into two phases: mark and clear

Marking phase: Find and mark all surviving objects starting from the root object.

Cleanup phase: traverse the objects in the heap, recycle unmarked objects, and join the free list

Disadvantage: need to suspend program STW

step:

  1. Perform STW (suspend program business logic), and then start from the main function to find the unreachable memory footprint and reachable memory footprint
  2. Start marking, the program finds out the reachable memory usage and marks
  3. End of marking, clear unmarked memory usage
  4. End STW to stop pausing, let the program continue to execute, and loop the process until the end of the main life cycle.

 At the beginning, the method was to stop STW when the garbage was cleaned up. Later, the solution was optimized and the garbage was cleaned up after the STW, and it was performed at the same time as the program was running. This reduced the duration of STW. However, STW will suspend user logic and have a great impact on the performance of the program. This kind of granularity of STW is still unacceptable for programs with high performance. Therefore, Go1.5 uses the three-color marking method to optimize STW.


three-color marking method

Mark objects as white, gray and black

White: uncertain object (default color); black: surviving object; gray: surviving object, child object to be processed

step:

  1. mark all objects as white
  2. Starting from the root node collection, mark the nodes traversed for the first time as gray and put them into the collection list
  3. Traverse the gray collection, mark the white nodes traversed by the gray nodes as gray, and mark the gray nodes as black,
  4. cycle through the process
  5. Until the collection of gray nodes is empty, recycle all white nodes

This method has a flaw: if the reference to the object is modified by the user, then the previous mark is invalid.

Analyze the root cause of the bug, mainly because the following two situations occurred during the running of the program

  1. A white object is referenced by a black object
  2. The white object with the reachability relationship between the gray object and it is destroyed

Therefore, two methods have been developed on this basis, the strong three-color invariant and the weak three-color invariant

  1. Strong tricolor invariant : black objects are not allowed to refer to white objects
  2. Weak three-color invariant : black objects can refer to white, white objects have references to him from other gray objects, or gray objects exist on his links

In order to realize the design ideas of these two invariants, a barrier mechanism is introduced, that is, a judgment mechanism is added during the execution of the program, and the callback function is executed if the judgment mechanism is satisfied.

 The barrier mechanism is divided into insertion barrier and deletion barrier . The insertion barrier realizes the strong three-color invariant, and the deletion barrier realizes the weak three-color invariant. It is worth noting that in order to ensure the operating efficiency of the stack, the barrier is only enabled for the memory objects on the heap , and the memory on the stack will be rescanned by STW after the GC ends.

Insertion barrier : A mechanism triggered when an object is referenced. When a white object is referenced by a black object, the white object is marked as gray (the object on the stack has no insertion barrier)

The disadvantage is: if object 1 creates a new object 6 on the stack, since the stack has no barrier mechanism, object 6 is still a white node and will be recycled

Therefore, at the end of the stack GC iteration (without gray nodes), STW will be performed on the stack, and the white nodes will be re-scanned and cleared. (STW time is 10-100ms) 

Delete Barrier : A mechanism triggered when an object is deleted. If a white object referenced by a gray object is deleted, the white object will be marked gray .

Disadvantages : This approach has low recovery accuracy . Even if an object is deleted, it can survive this round and be recycled in the next round . (If object 4 does not refer to object 3, object 3 should be recycled as garbage at this time, but object 3 will not be recycled until the next round of GC)

There is also a second scan of the stack that affects the efficiency of the program.


Three-color mark + hybrid write barrier technology

Based on the performance bottleneck caused by inserting a write barrier and deleting a write barrier that requires STW to rescan the stack at the end, Go introduced a hybrid write barrier in 1.8 to implement a weak three-color invariant design method, a hybrid write barrier in four steps

  1. At the beginning of GC, all reachable objects on the stack are marked as black (no secondary scanning, no STW)
  2. During GC, any new objects created on the stack are black
  3. Objects that are referenced by deletion are marked in gray
  4. Objects that are referenced are marked gray

Guess you like

Origin blog.csdn.net/qq_48626761/article/details/132069925