Home  >  Article  >  Backend Development  >  Does Go language have garbage collection?

Does Go language have garbage collection?

青灯夜游
青灯夜游Original
2022-12-09 19:42:386930browse

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.

Does Go language have garbage collection?

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

Does Go language have garbage collection?

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

Does Go language have garbage collection?

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

Does Go language have garbage collection?

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

Does Go language have garbage collection?

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:

Does Go language have garbage collection?

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

Does Go language have garbage collection?

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

Does Go language have garbage collection?

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

Does Go language have garbage collection?

##2. Garbage collection

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 A

write barrier algorithm 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"

Does Go language have garbage collection?

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)

Does Go language have garbage collection?

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 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)

  • 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 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

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

1Does Go language have garbage collection?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn