GO's garbage collector
Go language garbage collection generally uses the classic mark and sweep algorithm. (Recommended learning: Go )
1.3 Before version, Golang's garbage recovery algorithm was very simple, and then its performance was widely criticized: Go Runtime under certain conditions (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory (memory Exceeding the threshold or periodically (such as 2 minutes), the execution of all tasks is suspended, the mark&sweep operation is performed, and the execution of all tasks is started after the operation is completed.
In scenarios where a lot of memory is used, the go program will have a very obvious stuck phenomenon (Stop The World) when performing garbage collection. In background service processes that require high response speed, this kind of delay is simply intolerable! During this period, many teams at home and abroad that were practicing Go language in production environments had more or less stepped on the pitfalls of gc.
The common method to solve this problem at that time was to control the amount of automatically allocated memory as soon as possible to reduce the gc load, and at the same time, use manual memory management to deal with scenarios that require large amounts of memory and high frequency allocation.
Since version 1.3, the go team has begun to continuously improve and optimize gc performance. When each new version of go is released, gc improvements have become a focus of everyone's attention.
In version 1.3, go runtime separates mark and sweep operations. As before, all task execution is paused and mark is started. After mark is completed, the suspended tasks are restarted immediately, and sweep is performed. Tasks are executed in parallel with other tasks like ordinary coroutine tasks.
If running on a multi-core processor, go will try to run the gc task on a separate core without affecting the execution of the business code. Go team's own statement is that the pause time has been reduced by 50%-70%.
Version 1.4 (the latest stable version) has not made many performance changes to gc. In version 1.4, a lot of runtime code has replaced the native C language implementation with the Go language implementation. A major change brought to gc is that it can achieve accurate gc.
The C language implementation cannot obtain the object information of the memory during gc, so it cannot accurately distinguish between ordinary variables and pointers. It can only treat ordinary variables as pointers. If by chance there are other objects in the space pointed by this ordinary variable, then This object will not be recycled.
The Go language implementation fully knows the type information of the object, and will only traverse the object pointed to by the pointer when marking, thus avoiding the waste of heap memory in C implementation (solve about 10-30%).
In version 1.5, the go team has made major improvements to gc (foreshadowing has been laid in 1.4, such as the introduction of write barrier). The official main goal is to reduce delays. The garbage collector being implemented in go 1.5 is a "non-generational, non-moving, concurrent, three-color mark-and-sweep garbage collector".
The generational algorithm has been mentioned above and is a better garbage collection management strategy. However, its implementation is not considered in version 1.5. The reason I guess is that the steps cannot be too big. Gradually improving, go officials also stated that they will be considered in the gc optimization of version 1.6.
At the same time, the three-color marking method introduced above is introduced. The mark operation of this method can be executed gradually without scanning the entire memory space every time, which can reduce the time of stopping the world.
It can be seen that the garbage collection performance of go has been improving all the way up to version 1.5, but for relatively mature garbage collection systems (such as java jvm and javascript v8), go needs to optimize the path It’s still a long time coming (but I believe the future will be bright~).
The above is the detailed content of Does golang have gc?. For more information, please follow other related articles on the PHP Chinese website!