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.
Detailed explanation of the garbage collection mechanism GC in the Go language
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:
- User Program (Mutator): User-mode code. For GC, user-mode code only modifies the reference relationship between objects.
- Collector (Colletor): Responsible for performing garbage collection. Code
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
1.1 Allocation method of the memory allocator
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:
- First-Fit - Traverse from the head of the linked list and select the first size Memory blocks larger than the requested memory
- Loop first adaptation (Next-Fit) - Traverse from the end of the last traversal, and select the first memory block whose size is larger than the requested memory
- Optimal Adaptation (Best-Fit) — Traverse the entire linked list from the head of the linked list, select the most appropriate memory block
- Segregated Adaptation (Segregated-Fit) — Split the memory into multiple linked lists, the size of the memory block in each linked list Similarly, when applying for memory, first find a linked list that meets the conditions, and then select an appropriate memory block from the linked list
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
1.2 Memory allocation in Go
A picture shows the composition of memory allocation:
In the Go language, all objects on the heap will allocate memory by calling the runtime.newobject function, which The function will call runtime.mallocgc to 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 size <p>It can be seen from the code<code>runtime .mallocgc</code> 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</p>
- Micro 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 - Small objects
[16B, 32KB]
— Try to use the thread cache, the central cache, and the heap in sequence Allocate memory - Large object
(32KB, ∞)
— Allocate memory directly on the heap
Small Allocation
For small allocations less than 32kb, Go will try to obtain memory from the local cache of mcache
, which handles a span list (32kb memory blocks) mspan
Each thread M is assigned to a processor P, which can process at most one goroutine
at a time. When allocating memory, the current goroutine
will use its current local cache P to find the first available free object in the span
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
The purpose of the write barrier is to allow the collector to remain active during collection Data integrity on the heap
2.1 Implementation principle
Go language garbage collection can be divided into clear termination, mark, and mark termination and clear four different phases, two of which produce Stop The World (STW)Clear Termination Phase
- Pause the program, all processors will enter the safe point at this time
- If the current garbage collection cycle is forcibly triggered, we also need to deal with the memory management unit that has not been cleaned up
Marking Phase (STW)
Switch the status to
_GCmark
, enable write barriers, user program assistance (Mutator Assists) and enqueue the root objectWhen 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)
- Pause the program and switch the state to
_GCmarktermination
And close the user program of the auxiliary mark - Clean the thread cache on the processor
Cleaning phase
Switch the state to
_GCoff
Start the cleanup phase, initialize the cleanup state and close the write barrierRestore 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
2.2 Three-color marking method
The three-color marking algorithm divides the objects in the program into three categories: white, black and gray:
- White Objects — Potentially garbage, whose memory may be reclaimed by the garbage collector
- Black objects—Active objects, including objects that do not have any reference to external pointers and objects reachable from the root object
- Gray objects - active objects, because there are external pointers pointing to white objects, the garbage collector will scan the sub-objects of these objects
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Efficiently handle concurrency security issues in multi-process log writing. Multiple processes write the same log file at the same time. How to ensure concurrency is safe and efficient? This is a...
