GC in Go language

  1. Go's Garbage Collection Mechanism

Garbage collection (GC) is a daemon thread running in the background. Its role is to monitor the status of each object, identify and discard objects that are no longer used to release and reuse resources.

Simply put, the core of garbage collection is to mark which memory is still in use (that is, is referenced), which memory is no longer used (that is, not referenced), and reclaim the unreferenced memory for subsequent memory used when assigning.

At the beginning of garbage collection, scan from the root object, and mark the memory referenced by the root object as "referenced". Considering that the memory block may be a pointer, it needs to be marked recursively. Marked memory, all unmarked memory is marked as unallocated and completed recycling.

  1. Garbage collection trigger timing

There are three timings for triggering GC in the Go language:

Timing triggering of the system: If the GC is not triggered within two minutes, the GC will be triggered every two minutes.
User display call: The user calls the runtime.GC method to perform mandatory triggering.
Triggering when applying for memory: When applying for heap space for an object, GC may be triggered and the mallocgc method may be called.

  1. garbage collection algorithm

(1) Reference counting: Maintain a reference count for each object. When the object referencing the object is destroyed, the reference count is decremented by 1. When the reference count is 0, the object is recycled.

(2) Mark-Clear: Traversing all referenced objects from the root variable, the referenced objects are marked as "referenced", and the unmarked ones are recycled.

(3) Generational collection: Divide different generation spaces according to the length of object life cycle. Objects with long life cycles are placed in the old generation, while objects with short life cycles are placed in the new generation. Different generations have different recycling algorithms and recycling frequencies.

Garbage collection in Golang mainly uses the three-color marking method. The GC process and other user goroutines can run concurrently, but it takes a certain period of time for STW (stop the world). During the STW process, the CPU does not execute user code and is all used for garbage collection. , this process has a great impact, and Golang has carried out multiple iterative optimizations to solve this problem. Let's discuss this problem below.

  1. GC Analysis Tools

GC analysis tools are:

go tool pprof
go tool trace
go bulid -gcflags = “-m”
GODEBUG=“gctrace=1”

The most commonly used is GODEBUG="gctrace=1". The figure below shows the analysis of GC using GODEBUG="gctrace=1.

  1. Summarize

The Go language can automatically reclaim the memory allocated on the heap, and can implement concurrent GC, which has better performance. The
marking method used by the Go language is the three-color marking method, and in order to perform concurrent GC, a mixed barrier is used to effectively deal with the GC process. insertion and deletion operations.
There are three timings for GC to trigger, namely, timing trigger, user display call trigger, and trigger when applying for memory.
The optimization principle of GC is to minimize the allocation of garbage on the heap, memory pooling, reduce escape, and use empty structures.
The most commonly used command in GC analysis is GODEBUG="gctrace=1

Author: Kong Xiangyu

Guess you like

Origin blog.csdn.net/ekcchina/article/details/130710298