golang garbage collection

Introduction to GC

Go GC (Garbage Collection) garbage collection is a way to automatically manage memory. Languages ​​that support GC do not need to manage memory manually, and the program background automatically determines whether the object is alive and reclaims its memory space, freeing developers from memory management.

Garbage collection classification

  • Reference counting C++ std library, cocos2d
  • clearly marked
  • three-color marker go,
  • Generational collection of java, .net

go uses three-color markers

Origin: In 1959, GC was invented by John McCarthy for manual memory management in Lisp. Now many languages ​​provide GC, but the principle and basic algorithm of GC have not changed much.

Development of go GC

There may be more GC problems in the early version of Golang, but fortunately, the GC has been improved in each version released

  • After version 1.5 (2015.8), Go GC has been able to meet the usage requirements of most production environments; three-color markup has been added, and concurrent mark clearing has been added.
  • 1.8 (2017.2) Through the hybrid write barrier, the STW (stop the world) is reduced to sub ms; the hybrid write barrier is added.

Go GC features

  • three-color marker
  • Concurrent mark and sweep
  • non-generation
  • non-crunch
  • mixed write barrier

 

three-color marker

Reference:  https://en.wikipedia.org/wiki/Tracing_garbage_collection

  1. There are three sets of black, white and gray at the beginning of the program, and all objects are initially white
  2. Start marking from the root object, marking all reachable objects in grey
  3. Take an object out of the grey object collection, mark its referenced object in grey, put it in the grey collection, and mark itself in black
  4. Repeat the third step until the gray set is empty, that is, all reachable objects are marked
  5. After the marking is over, the unreachable white objects are garbage, and the memory is iteratively cleaned to recycle the white objects
  6. reset GC state

 

 

 write barrier

The pointer will open the write barrier before assigning it; after the operation is completed, it will be closed and the previous code will be called back to ensure that the garbage collection mechanism is complete and that there are no missing objects;

 

three-color state

In GC, there are no real three sets to hold three-color objects;

Go wants to allocate it in the span. There is also a field in the span called gcmarkBits. In the mark phase, each bit represents a slot that has been marked.

The bit is 0 for white objects, and 1 for gray or black (runtime.markBits); white will be recycled

There are wbBuf and gcw gcWork in each p, as well as the global workbuf marking queue to implement the producer-consumer model. The pointer in the queue is a gray object, indicating that it has been marked and is waiting to be scanned

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/a6taotao/article/details/104019927