Home > Article > Backend Development > Does go language have gc?
The go language has gc. GC refers to garbage collection, an automatic memory management mechanism; the go language supports GC, and the recycling of object memory space in the Go language is completed through the GC mechanism. For the Go language, the GC of the Go language uses three colors: no generation (objects are not divided into generations), no sorting (objects are not moved and sorted during the recycling process), and concurrent (executed concurrently with user code). Mark and sweep algorithm.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
The GC mechanism became popular after the Java language became widely used. For example, the later scripting language Python supports GC, and GO also supports GC.
A notable feature of the Go language and the C/C language is that the recycling of object memory space in Go is completed through the GC mechanism. It does not require manual application and release by programmers like C, so Memory leaks are relatively unlikely to occur in Go. Today we will talk about the GC mechanism in Go.
What is GC and what is its use?
GC, full name Garbage Collection, is a mechanism for automatic memory management.
When the memory requested by the program from the operating system is no longer needed, garbage collection actively recycles it and reuses it for other codes to apply for memory, or returns it to the operating system. This is for memory-level resources. The automatic recycling process is garbage collection. The program component responsible for garbage collection is the garbage collector.
Garbage collection is actually a perfect example of "Simplicity is Complicated". On the one hand, programmers benefit from GC and do not need to worry about or manually apply for and release memory. GC automatically releases remaining memory when the program is running. On the other hand, GC is almost invisible to programmers. It only appears when the program needs special optimization by providing a controllable API to control the GC's running timing and running overhead.
Usually, the execution process of the garbage collector is divided into two semi-independent components:
Mutator: This name essentially refers to Code that represents user mode. Because for the garbage collector, user-mode code is only modifying the reference relationship between objects, that is, operating on the object graph (a directed graph of reference relationships between objects).
Collector: The code responsible for performing garbage collection.
Root object in GC
The root object is also called the root collection in the terminology of garbage collection. It is The objects that the garbage collector first checks when marking the process include:
Global variables: variables that exist throughout the entire life cycle of the program that can be determined at compile time.
Execution stack: Each goroutine contains its own execution stack, which contains variables on the stack and pointers to allocated heap memory blocks.
Register: The value of the register may represent a pointer, and these pointers involved in the calculation may point to the heap memory block allocated by some evaluators.
How GC is implemented
The existence of all GC algorithms can be summarized as tracking and referencing Reference Counting is a mixture of these two forms.
Tracking GC
Starting from the root object, based on the reference information between objects, proceed step by step until the entire heap is scanned and determined Objects that need to be retained, thereby recycling all recyclable objects. Go, Java, and V8's implementation of JavaScript are all tracking GC.
Reference counting GC
Each object itself contains a reference counter, which is automatically recycled when the counter reaches zero. Because this method has many flaws, it is usually not used when pursuing high performance. Python, Objective-C, etc. are all reference counting GC.
Currently the more common GC implementation methods include:
Tracking type, which is divided into many different types, such as:
Marking and Sweeping: Starting from the root object, the objects that are determined to be alive are marked, and the objects that can be recycled are cleaned.
Marking and defragmentation: proposed in order to solve the problem of memory fragmentation. During the marking process, objects are organized into a continuous piece of memory as much as possible.
Incremental: Execute the marking and cleaning process in batches, executing a small part each time, thereby incrementally advancing garbage collection, achieving near real-time, almost no pauses Purpose.
Incremental sorting: Based on the incremental method, the object sorting process is added.
Generational: Classify objects according to the length of their survival time. Those whose survival time is less than a certain value are the young generation, and those whose survival time is greater than a certain value are the old generation, which will never participate in recycling. The object is the permanent generation. And objects are recycled based on generational assumptions (if an object does not live long, it tends to be recycled, if an object has lived for a long time, it tends to live longer).
#Reference counting: Recycle according to the object's own reference count, and recycle immediately when the reference count reaches zero.
The implementation of GC in Go
For Go, Go’s GC uses generationless (Objects are not divided into generations), no sorting (objects are not moved and sorted during the recycling process), and concurrent (executed concurrently with user code) three-color mark cleaning algorithm. [Related recommendations: Go Video Tutorial]
The reasons are as follows:
The advantage of object defragmentation is to solve the memory fragmentation problem and "allow" the order of use Memory allocator. However, the Go runtime allocation algorithm is based on tcmalloc, and there is basically no fragmentation problem. And the sequential memory allocator is not suitable in multi-threaded scenarios. Go uses a modern memory allocation algorithm based on tcmalloc, and sorting objects will not bring substantial performance improvements.
Generational GC relies on the generational assumption, that is, GC places the main recovery target on newly created objects (which have short survival times and are more likely to be recycled) instead of frequent checks All objects. However, Go's compiler will store most new objects on the stack through escape analysis (the stack is directly recycled), and only those objects that need to exist for a long time will be allocated to the heap that requires garbage collection. In other words, the short-lived objects recycled by the generational GC are directly allocated to the stack in Go. When the goroutine dies, the stack will be recycled directly without the participation of the GC. Therefore, the generational assumption does not bring Come to direct advantage. Moreover, Go's garbage collector executes concurrently with user code, so the STW time has nothing to do with the generation of the object and the size of the object. The Go team is more focused on how to better enable GC to execute concurrently with user code (using appropriate CPUs to perform garbage collection) rather than on the single goal of reducing pause times.
For more programming-related knowledge, please visit: Programming Video! !
The above is the detailed content of Does go language have gc?. For more information, please follow other related articles on the PHP Chinese website!