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!

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

本篇文章带大家了解一下golang 的几种常用的基本数据类型,如整型,浮点型,字符,字符串,布尔型等,并介绍了一些常用的类型转换操作。

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

在写 Go 的过程中经常对比这两种语言的特性,踩了不少坑,也发现了不少有意思的地方,下面本篇就来聊聊 Go 自带的 HttpClient 的超时机制,希望对大家有所帮助。

go语言需要编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言,也就说Go语言程序在运行之前需要通过编译器生成二进制机器码(二进制的可执行文件),随后二进制文件才能在目标机器上运行。

删除map元素的两种方法:1、使用delete()函数从map中删除指定键值对,语法“delete(map, 键名)”;2、重新创建一个新的map对象,可以清空map中的所有元素,语法“var mapname map[keytype]valuetype”。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
