The go language does not require manual memory management; the go language has a built-in memory management function (GC mechanism), which is an automatic memory management mechanism. 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 automatic recycling process for memory-level resources is It is garbage collection; and the program component responsible for garbage collection is the garbage collector.
The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.
go language does not require manual memory management; go language has built-in memory management function (GC mechanism), and developers do not need to care about the application and release of memory, which provides users with Come to great convenience.
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.
In calculations, 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, and does not generate Memory fragmentation is managed by the compiler and does not need to be managed by developers; the heap area will generate memory fragmentation. In the Go language, objects in the heap area are allocated by the memory allocator and recycled by the garbage collector
Usually, garbage collection The execution process of the mutator is divided into two semi-independent components:
Mutator: This name essentially refers to user-mode code. 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 marking process of the garbage collector. The first objects to be checked 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 evaluator.
In the Go language, the algorithm implemented by the garbage collector is A concurrent three-color mark and scan collector
The garbage collector runs concurrently with the Go program, so a write barrier algorithm is required to detect potential changes in memory. The only condition for initiating a write barrier is to stop the program for a short period of time, i.e. "Stop the World"
The purpose of the write barrier is to allow the collector to remain active during collection Data integrity on the heap
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
Marking phase (STW)
Switch status to _GCmark
, enable write barrier, user program assistance (Mutator Assists) and enqueue the root object
Resume the execution program, 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 pointers and the new pointers as gray, and all new The created objects will be directly marked in black
Start scanning the root object, including all Goroutine stacks, global objects, and runtime data structures not in the heap. Scanning the Goroutine stack will be paused. The current processor
processes the objects in the gray queue in turn, marking the objects black and marking the objects they point to gray
Use The distributed termination algorithm checks the remaining work and finds that after the marking phase is completed, it enters the marking termination phase
Marking termination phase (STW)
_GCmarktermination
and close the auxiliary marked user program##Cleaning phase
_GCoff Start the cleanup phase, initialize the cleanup state and close the write barrier
Programming Video! !
The above is the detailed content of Does Go language need to manually manage memory?. For more information, please follow other related articles on the PHP Chinese website!