当前位置:网站首页>Detailed explanation of Golang garbage collection mechanism
Detailed explanation of Golang garbage collection mechanism
2022-08-02 15:26:00 【The stars have a meal】
一、垃圾回收概念
有些编程语言(C、C++)Need to manually release those that are no longer needed、Data allocated on the heap,这叫做“手动垃圾回收”,But if the data is released too early,Subsequent accesses to the data will fail,Because the freed memory may have been emptied or reallocated;如果忘记释放,数据会一直占用内存,出现“内存泄漏”,So more and more programming languages are supported“自动垃圾回收”.
It is up to the runtime to identify data that is no longer useful and free the memory they occupy,When the memory is freed,We don't need to think about when to deal with the freed memory
二、常用的垃圾回收算法
引用计数
Reference count refers to the number of times a data object is referenced,程序执行的过程中,会更新对象的引用计数,当引用计数更新为0时,就表示这个对象不再有用,The memory it occupies can be reclaimed,Therefore, the task of garbage identification in the reference counting method has been allocated to each operation of the data object
缺点:
1.Frequently updating reference counts can also cause a lot of overhead
2.若是A引用了B,B也引用了A,形成循环引用,当A和B's reference counts are updated to only have mutual references to each other,The reference count cannot be updated to0,The corresponding memory cannot be reclaimed
标记——清除算法
The data used in the program must be from the stack、Data segment These root nodes track the data obtained,Although it can be traced, it does not mean that it will be used in the future,However, the data that cannot be tracked by the root node must not be used,It must be garbage.
要识别存活对象,可以把栈、Data objects on the data segment asroot,基于他们进一步追踪,Mark all traceable data,The rest that cannot be traced is garbage
算法分两个部分:标记(mark)和清扫(sweep).标记阶段表明所有的存活单元,清扫阶段将垃圾单元回收.
(1)标记阶段
在此阶段,The garbage collector will start traversing from the root object of the application.每一个可以从根对象访问到的对象都会被添加一个标识,于是这个对象就被标识为可到达对象.
(2)清理阶段
在此阶段中,垃圾回收器,会对堆内存从头到尾进行线性遍历,如果发现有对象没有被标识为可到达对象,那么就将此对象占用的内存回收,并且将原来标记为可到达对象的标识清除,以便进行下一次垃圾回收操作
优点:No circular reference issues;No maintenance counting overhead
缺点:需要STW,Execution of the application is suspended;Non-incremental requires traversing the entire heap space,开销大
三色标记算法
- 白色:还没有搜索过的对象(标记结束后,白色对象会被当成垃圾对象)
- 灰色:正在搜索的对象
- 黑色:搜索完成的对象(不会当成垃圾对象,不会被GC)
假设现在有白、灰、黑三个集合(表示当前对象的颜色),其遍历访问过程为:
- 初始时,所有对象都在【白色集合】中;
- 将 GC Roots 直接引用到的对象挪到 【灰色集合】中;
- 从灰色集合中获取对象
3.1. 将本对象引用到的其他对象全部挪到 【灰色集合】中;
3.2. 将本对象挪到【黑色集合】里面. - 重复步骤3,直至【灰色集合】为空时结束.
- 结束后,仍在【白色集合】的对象即为 GC Roots 不可达,可以进行回收.
注:如果标记结束后对象仍为白色,意味着已经“找不到”该对象在哪了,不可能会再被重新引用.
标记——整理算法
Mark all reachable objects,Then move the reachable object to another segment of the space,Finally clean up the memory outside the bounds.
优点:
避免了内存碎片化的问题
适合老年代算法:老年代对象存活率高的情况下,标记整理算法由于不需要复制对象,效率更高
缺点:The sorting process is complicated;How long to traverse the memory,导致STW时间比标记清除算法高
分代收集算法
弱分代假说:大部分对象都在年轻时死亡
For young generation regions with short life cycles,Each collection only needs to consider how to keep a small number of live objects,因此可以采用标记——The replication method is completeGC
For old age regions with long lifetimes,可以通过减少gcfrequency to increase
效率,At the same time there is no extra space for replication due to high object survival rate,Therefore, the mark removal method or the mark finishing method can generally be used
这样划分,The heap is dividedYoung和Old两个分区,因此GCAlso divided into new generationGC和老年代GC
对象的分配策略:
Objects take precedence in the young generationEden区域分配
Most objects go directly to the old age
Objects with longer periods in the new generation are presents0或s1Every time the area passes through a new generationGc,就增加一岁,increase to a certain threshold,into the old age area
三、屏障技术
The three-color marking clearly depicts the misjudgment of live objects as garbage during garbage collection
1.References of black objects to white objects do not appear(强三色不变式),The white pointer can be colored gray,It is also possible to return written black objects to gray,This is called an insert write barrier
2.References of black objects to white objects are allowed,But it is guaranteed that white objects can be reached through gray objects(弱三色不变式),Reminds us of vandalism on the path to white,Prevent this vandalism from occurring,We can color white objects gray,This is called removing the write barrier
实现强/A common practice for weak tricolor invariants is to create read-write barriers
To solve the concurrency problem of the three-color notation,有两种思路:
read barrier technology:in the non-mobile garbage collector,Naturally, read barriers are not required;The replicating collector moves data to avoid fragmentation,Reading data at this time is also not so safe,Requires read barrier technology,Make sure that user programs do not access stale objects that already have copies
写屏障技术:Insert instruction in write operation,The purpose is to notify the modification of the data object to the garbage collection period,Therefore, write barriers usually require a recordset,Recordsets are stored sequentially,使用哈希表,The record is accurate to the extent that the object being modified only records the page on which it is located
四、运行模式
1.增量式垃圾回收
可以分摊GC时间,Avoid prolonged program pauses
存在的问题:
1.分摊GC时间,Avoid prolonged program pauses
2.内存屏障技术,需要额外时间开销,And due to the conservative nature of memory barrier technology,Some garbage objects are not collected,An additional round will be addedgc的总时长
2.并行垃圾回收
A task that would have required the sole responsibility of a thread,Now the fractions need to be handed over to multiple threads
achieve better load balancing,Will increase the synchronization overhead between threads
3.并发垃圾回收
The user program executes concurrently with the garbage collector,在多核场景下,The user program and the garbage collector will execute in parallel
To a certain extent, the advantages of multi-core computers are used to reduce the interference to user programs,The overhead and conservatism issues of write barriers remain,这是不可避免的
关于gc触发时间:
- Heap memory reaches a certain threshold
- 距离上次gc超过一定阈值
- If it is not currently activatedgc,A new round startsgc
关于gc调优:
- Try to combine small objects into large ones
- Try to use small data types
- 大量stringUsed when splicingstring.join,而不是+号(go中string只读,each targetstringoperations will create a new onestring)
参考
边栏推荐
- 13.56MHZ刷卡芯片CI521兼容cv520/ci520支持A卡B卡MIFARE协议
- STM32LL库使用——SPI通信
- 5. Use RecyclerView to elegantly achieve waterfall effect
- 发布模块到npm应该怎么操作?及错误问题解决方案
- 使用libcurl将Opencv Mat的图像上传到文件服务器,基于post请求和ftp协议两种方法
- DP1101兼容CC1101是SUB1GHz无线收发芯片应用于智能家居
- 使用npx -p @storybook/cli sb init安装失败,手把手搭建专属的storybook
- Win10电脑不能读取U盘怎么办?不识别U盘怎么解决?
- FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
- TCP三次握手、四次挥手
猜你喜欢
随机推荐
General code for pytorch model to libtorch and onnx format
TCP三次握手、四次挥手
2020-02-06-快速搭建个人博客
Actual combat Meituan Nuxt +Vue family bucket, server-side rendering, mailbox verification, passport authentication service, map API reference, mongodb, redis and other technical points
DP4344兼容CS4344-DA转换器
IPV4和IPV6是什么?
刷卡芯片CI520可直接PIN对PIN替换CV520支持SPI通讯接口
The SSE instructions into ARM NEON
2022TI杯D题混沌信号产生实验装置
What is Win10 God Mode for?How to enable God Mode in Windows 10?
C语言函数参数传递模式入门详解
Win10系统设置application identity自动提示拒绝访问怎么办
mysql的索引结构为什么选用B+树?
Mysql connection error solution
FP5207电池升压 5V9V12V24V36V42V大功率方案
Win7怎么干净启动?如何只加载基本服务启动Win7系统
LORA芯片ASR6505无线远距离传输8位MCU
Golang 垃圾回收机制详解
轻量化AlphaPose
What should I do if Windows 10 cannot connect to the printer?Solutions for not using the printer









