Go get to know garbage collection

This paper introduces the concept of garbage collection, garbage collection algorithms and works Golang GC, and after reading this article allows you to have a comprehensive understanding of the Golang garbage collection. Because I do not understand other languages ​​GC, garbage collection algorithm does not compare to other languages, may require their own Google.

What is garbage collection

Garbage collection (English: Garbage Collection, abbreviated as GC), is an automatic memory management mechanism in computer science. When the dynamic memory on a computer is no longer needed, they should be released to make room for memory, this memory resource manager, known as garbage collection. The garbage collector can alleviate a lot of burden on the programmer, the programmer also reduce the chance of mistakes. From Wikipedia

Simply put, garbage collection (GC) is a daemon thread running in the background, its role is to monitor the status of individual objects, identify and discard unused objects to the release and reuse resources.

go garbage collection

The current garbage collection Golang using three color marker made with the write barrier and auxiliary GC , three-color labeling is labeling - clearance method an enhanced version.

Mark - sweep method (mark and sweep)

The original mark clearly divided into two steps:

  1. mark. First STP (Stop The World), to suspend the whole process all the running thread, the object will be marked with a reference mark
  2. Clear object is not marking machine, that reclaim memory resources, then resume running thread.

This has a big question mark is to the state of the object changes during the GC can not guarantee by STW, the whole process should be suspended off the outside it seems the program will Caton.

Three color labeling

Three color labeling method is an improvement over the mark phase, the principle is as follows:

  1. The initial state of all objects are white.
  2. Starting from the root scan all root objects root (below a, b), their object references marked in gray (FIG. A, B)

    So what is the root of it?
    I read many articles did not explain this concept, here under the description: root region is dominated by running to the current stack and global data area moments.

  1. Gray whether the object references other objects. If no other objects are referenced object is marked as the gray black (A figure above); if there is then it is referenced at the same time becomes black object it references also becomes gray (cited above figure B D)
  2. Repeat step 3 until the gray target queue is empty. In this case the object is the white trash, recycling.

Reference may also be movable FIG aid in understanding the following:

How to Go GC work

Described above is a three-color marker GO GC algorithm used, but does not seem to reduce the impact of STW reflected in how the program is it? In fact, because most of the processing is Golang GC and user code in parallel .

User code may change the status of certain objects during GC, GC and how to implement user code in parallel it? Look at the full GC process works:

  1. Mark: consists of two parts:
  • Mark Prepare: GC initialization tasks, including turning write barrier (write barrier) and secondary GC (mutator assist), statistical root object number and other tasks. This process requires STW
  • GC Drains: Scan all root objects, including a pointer on a global pointer and goroutines (G) stack (when the corresponding G stack scans required to stop the G), which was added to the tag queue (gray queue), and the gray target queue loop process, gray until the queue is empty. This process is performed in parallel background
  1. Mark Termination: work completion flag, rescan (re-scan) global pointer and a stack. Because Mark and the user program in parallel, so in the course of Mark may have a new object allocation and assignment pointer, this time need to be recorded by the write barrier (write barrier), re-scan check again. This process also will STW's.
  2. Sweep: recovery of all the white marker object according to a result, the background process is performed in parallel
  3. Sweep Termination: 对未清扫的span进行清扫, 只有上一轮的GC的清扫工作完成才可以开始新一轮的GC。
    如果标记期间用户逻辑改变了刚打完标记的对象的引用状态,怎么办呢?

    写屏障(Write Barrier)

    写屏障:该屏障之前的写操作和之后的写操作相比,先被系统其它组件感知。
    好难懂哦,结合上面GC工作的完整流程就好理解了,就是在每一轮GC开始时会初始化一个叫做“屏障”的东西,然后由它记录第一次scan时各个对象的状态,以便和第二次re-scan进行比对,引用状态变化的对象被标记为灰色以防止丢失,将屏障前后状态未变化对象继续处理。

辅助GC

从上面的GC工作的完整流程可以看出Golang GC实际上把单次暂停时间分散掉了,本来程序执⾏可能是“⽤户代码-->⼤段GC-->⽤户代码”,那么分散以后实际上变成了“⽤户代码-->⼩段 GC-->⽤户代码-->⼩段GC-->⽤户代码”这样。如果GC回收的速度跟不上用户代码分配对象的速度呢?
Go 语⾔如果发现扫描后回收的速度跟不上分配的速度它依然会把⽤户逻辑暂停,⽤户逻辑暂停了以后也就意味着不会有新的对象出现,同时会把⽤户线程抢过来加⼊到垃圾回收⾥⾯加快垃圾回收的速度。这样⼀来原来的并发还是变成了STW,还是得把⽤户线程暂停掉,要不然扫描和回收没完没了了停不下来,因为新分配对象⽐回收快,所以这种东⻄叫做辅助回收。
## 如何进行GC调优
衡量GC对程序的影响可以参考这篇文章,Go 程序的性能调试问题

减少对象的分配,合理重复利用;
避免string与[]byte转化;

两者发生转换的时候,底层数据结结构会进行复制,因此导致 gc 效率会变低。

少量使用+连接 string;

Go里面string是最基础的类型,是一个只读类型,针对他的每一个操作都会创建一个新的string。
如果是少量小文本拼接,用 “+” 就好;如果是大量小文本拼接,用 strings.Join;如果是大量大文本拼接,用 bytes.Buffer。

GC触发条件

自动垃圾回收的触发条件有两个:

  1. 超过内存大小阈值
  2. 达到定时时间
    阈值是由一个gcpercent的变量控制的,当新分配的内存占已在使用中的内存的比例超过gcprecent时就会触发。比如一次回收完毕后,内存的使用量为5M,那么下次回收的时机则是内存分配达到10M的时候。也就是说,并不是内存分配越多,垃圾回收频率越高。
    如果一直达不到内存大小的阈值呢?这个时候GC就会被定时时间触发,比如一直达不到10M,那就定时(默认2min触发一次)触发一次GC保证资源的回收。

写在最后

虽然Golang有自动垃圾回收机制,但是GC不是万能的,最好还是养成手动回收内存的习惯:比如手动把不再使用的内存释放,把对象置成nil,也可以考虑在合适的时候调用runtime.GC()触发GC。

近期在维护的go学习示例代码,新入坑的朋友们可以关注下
go-programming

参考:

string讨论

Go语言——垃圾回收GC

Golang 垃圾回收剖析

Golang垃圾回收机制详解

go垃圾回收概要

常见GC算法及Golang GC

Guess you like

Origin www.cnblogs.com/CodeWithTxT/p/11366643.html