Home>Article>Backend Development> Does Go language have garbage collection?
The go language has garbage collection. The Go language comes with a garbage collection mechanism (GC); GC is executed through a separate process. It searches for variables that are no longer used and releases them. in calculation. The memory space contains two important areas: the stack area (Stack) and the heap area (Heap); the stack area generally stores the parameters, return values and local variables of function calls, does not produce memory fragmentation, is managed by the compiler, and does not require development The heap area will generate memory fragments. In the Go language, the objects in the heap area are allocated by the memory allocator and recycled by the garbage collector.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
Go language has its own garbage collection mechanism (GC). The GC is performed by a separate process, which searches for variables that are no longer used and frees them. It should be noted that GC will occupy machine resources when running.
In computer science, garbage collection (Garbage Collection (GC) for short) is an automatic The mechanism for managing memory, the garbage collector will try to recycle objects and memory occupied by the program that are no longer used
Programmers benefit from GC and do not need to worry about or manually apply for memory. and release operations, GC automatically releases the remaining memory when the program is running.
GC is almost invisible to programmers. Only when the program needs special optimization, the GC's running timing and running overhead can be adjusted by providing a controllable API. Only when you are in control can you show up
In computing, the memory space contains two important areas: the stack area (Stack) and the heap area (Heap); the stack area generally stores function calls Parameters, return values and local variables do not generate memory fragmentation and are managed by the compiler and do not need to be managed by developers; while the heap area generates memory fragmentation. In the Go language, objects in the heap area are allocated by the memory allocator and collected by the garbage collector. Recycle. [Related recommendations:Go video tutorial,Programming teaching]
Usually, the execution process of the garbage collector is divided into two semi-independent components:
1. Memory management and allocation
When the memory is no longer in use, Go memory management Executed automatically by its standard library, i.e. allocating from memory to Go collections. Memory management generally includes three different components, namely the user program (Mutator), the allocator (Allocator) and the collector (Collector). When the user program applies for memory, it will apply for new memory through the memory allocator, and the allocator Will be responsible for initializing the corresponding memory area from the heap
In programming languages, memory allocators generally have two allocation methods:
Linear Allocator (Sequential Allocator, Bump Allocator)
Idle Linked list allocator (Free-List Allocator)
Linear allocator
Linear allocation (Bump Allocator) is an efficient memory allocation method , but it has major limitations. When the user uses a linear allocator, he only needs to maintain a pointer to a specific memory location in the memory. If the user program applies for memory from the allocator, the allocator only needs to check the remaining free memory, return the allocated memory area and modify the pointer in Location in memory;
Although the linear allocator has faster execution speed and lower implementation complexity, the linear allocator cannot reuse the memory after the memory is released. As shown in the figure below, if the allocated memory is recycled, the linear allocator cannot reuse the red memory
Therefore, the linear allocator needs to be used in conjunction with a suitable garbage collection algorithm
Mark-Compact
Copying GC
Generational recycling (Generational GC)
The above algorithm can defragment the surviving objects through copying and merge the free memory regularly, so that the efficiency of the linear allocator can be used to improve the performance of the memory allocator.
Free-List Allocator
Free-List Allocator (Free-List Allocator) can reuse the memory that has been released. It will maintain a linked list internally. data structure. When a user program applies for memory, the free linked list allocator will traverse the free memory blocks in order to find a large enough memory, then apply for new resources and modify the linked list
There are four common strategies for free linked list allocators:
The fourth strategy is similar to the memory allocation strategy used in the Go language
This strategy will divide the memory into a linked list consisting of 4, 8, 16, and 32-byte memory blocks. When we apply for 8-byte memory from the memory allocator, it will Find the free memory block that meets the conditions in the above figure and return it. The allocation strategy of isolation adaptation reduces the number of memory blocks that need to be traversed and improves the efficiency of memory allocation
A picture shows the composition of memory allocation:
In the Go language, all objects on the heap will allocate memory by calling theruntime.newobjectfunction, which The function will callruntime.mallocgcto allocate the memory space of the specified size. This is also the necessary function for the user program to apply for memory space on the heap
func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer { mp := acquirem() mp.mallocing = 1 c := gomcache() var x unsafe.Pointer noscan := typ == nil || typ.ptrdata == 0 if sizeIt can be seen from the code
runtime .mallocgc
Execute different allocation logic according to the size of the object, divide them into micro objects, small objects and large objects according to the size of the objects
(0, 16B)
— Use the micro allocator first, and then try to allocate memory using the thread cache, the central cache, and the heap in sequence[16B, 32KB]
— Try to use the thread cache, the central cache, and the heap in sequence Allocate memory(32KB, ∞)
— Allocate memory directly on the heapSmall Allocation
For small allocations less than 32kb, Go will try to obtain memory from the local cache ofmcache
, which handles a span list (32kb memory blocks)mspan
Each thread M is assigned to a processor P, which can process at most onegoroutine
at a time. When allocating memory, the currentgoroutine
will use its current local cache P to find the first available free object in thespan
list
large Allocation
Go does not use local cache to manage large allocations. These allocations larger than 32kb are rounded to the page size, and the page is allocated directly to the heap
In the Go language, the algorithm implemented by the garbage collector is a concurrent three-color mark and scan collector The garbage collector runs at the same time as the Go program, so it needs to be passed AThe purpose of the write barrier is to allow the collector to remain active during collection Data integrity on the heap
Clear Termination Phase
Marking Phase (STW)
Switch the status to_GCmark
, enable write barriers, user program assistance (Mutator Assists) and enqueue the root object
When the execution resumes, the marking process and the assisting user program will begin to concurrently mark the objects in the memory. The write barrier will mark both the overwritten pointer and the new pointer as gray, and all newly created objects will be marked directly as black.
Start scanning the root object, including all Goroutine stacks, global objects, and runtime data structures not in the heap. The current processor will be paused during the scanning of the Goroutine stack
Process the objects in the gray queue in sequence, marking the objects black and marking the objects they point to gray
Use the distributed termination algorithm to check the remaining work , it is found that after the marking phase is completed, it enters the marking termination phase
Marking termination phase (STW)
_GCmarktermination
And close the user program of the auxiliary markCleaning phase
Switch the state to_GCoff
Start the cleanup phase, initialize the cleanup state and close the write barrier
Restore the user program, all newly created objects will Marked in white
Concurrently clean up all memory management units in the background. When Goroutine applies for a new memory management unit, cleanup will be triggered
The three-color marking algorithm divides the objects in the program into three categories: white, black and gray:
The working principle of the three-color mark garbage collector is very simple, you can It is summarized into the following steps:
Select a gray object from the collection of gray objects and mark it black
Convert black All objects pointed to by the object are marked gray to ensure that neither the object nor the objects referenced by the object will be recycled
Repeat the above two steps until there are no gray objects in the object graph
For more programming-related knowledge, please visit:Programming Video! !
The above is the detailed content of Does Go language have garbage collection?. For more information, please follow other related articles on the PHP Chinese website!