当前位置:网站首页>Three color mark removal method
Three color mark removal method
2022-06-21 17:18:00 【What's so strange!】
List of articles
1、 Overview of tricolor marking algorithm
When we do garbage scanning , I have learned two algorithms : Reference counting and reachability analysis , stay Java We mainly use reachability analysis .
The three basic algorithms of reachability algorithm are : Mark - eliminate , Mark - Copy , Mark - eliminate . If it's in STW Under the circumstances , Then neither algorithm will go wrong , It's just efficiency . But with the development of calculator hardware , The corresponding computer software technology and ideas are also progressing . For example, on the basis of multiple concurrency , Reduce STW Time for , Complete the garbage removal , Edge clear edge marker , This leads to Tri-color Marking Algorithm .
Tricolor refers to : black 、 gray 、 white
black: Represents the object and has been accessed by the garbage collector , And the references of this object have been scanned . Black objects represent and have been scanned , He survived safely , If there are other object references pointing to black objects , No need to scan again . Black objects can't be direct ( Without going through the gray object ) Point to a white object .gray: Indicates that the object has been accessed by the garbage collector , But at least one reference on this object has not been scannedwhite: Indicates that the object has not been accessed by the garbage collector . Obviously at the beginning of accessibility analysis , All objects are white , For example, at the end of the analysis , Still a white object , That is to say, it's impossible .
2、 The process of tricolor marking

Suppose there's white now 、 black 、 Grey three sets ( Represents the color of the current object )
- At first , All objects are in
In the white collection - take GC Roots Move directly referenced objects to
In the grey set - from
Grey setsGet object :- Put the object to which this object refers into
In the grey set - Put this object into
In the black collection
- Put the object to which this object refers into
- Repeat step 3, until
Grey setsEmpty end - After the end , Still in
White setThe object of is GC Roots Unreachable , Can be recycled .
notes : If the object is still white after the tag ends , It means that you have “ Can't find ” Where is the object , It's impossible to be quoted again . For example, there is no reference to H 了 , It's white , There can be no more object references to it .
3、 Existing problems
Both user threads and garbage collection threads are threads , Naturally there is a pause time , If the time slice of the garbage collection thread is up , The user thread does other operations , This leads to the problems of wrong and missing marks .
3.1 Wrong mark
Marked as not garbage , It's rubbish ( Also known as floating garbage ), Pictured :

The garbage collection thread has just D Finish sweeping , take D Marked black , The garbage collection thread time is up , The garbage collection thread is paused . The user thread will D Point to E Change the relationship of to ,D.E=null, From our point of view ,E It's garbage , however E Still grey , After scanning E after ,E Will turn black , But this does not affect the execution of the program , In the next garbage collection , The garbage collection thread will E Recycle .
3.2 Missing mark
This problem is quite serious , As shown in the figure below

Garbage collection thread take D Marked in black , Express D The scan is complete . Go now Scan his references E, But at this time The garbage collection thread is paused , The user thread performs such an operation :E.G = null, D.G = G. Then the garbage collection thread comes back Continue scanning E,** At this time in E It seems ,G Is an unreachable object . however D And then I quote G, But D It's black , I won't go back and do it again , Garbage collection is over ,G Its color is white , To be removed ,D To call G object , A null pointer exception occurred .** It's really a terrible thing .
4、 Solve the problem of killing by mistake
As described above , Wrong killing only happens when The following two conditions occur simultaneously , Will happen :
- Black objects point to white objects
- The gray object that originally pointed to the white object was disconnected from him

4.1 CMS: Write barriers + Incremental updating (Incremental Update)
stay CMS in , If this happens : Black objects point to white objects (D.G = G), Before performing the operation , Use Hook function ( Write barriers , Be similar to AOP), Put the object D From black to gray , This makes it possible to scan D 了 .
So for the grey set , It is an incremental update .
4.2 G1: Write barriers + Original snapshot (STAB)
stay G1 in , This is the case : Gray objects break references to white objects (E.G = null), Before performing the operation , Use Hook function ( Write barriers , Be similar to AOP), Record the reference to be deleted , At the end of the concurrent scan , Scan again .
And the idea is that : Taste Try to keep the object graph at the beginning , The original snapshot (Snapshot At The Beginning,SATB), At some point Of GC Roots after , At that time, the object graph was determined .
such as at that time D It's quoting G Of , Then the subsequent tags should follow the object diagram of this moment (D Quote G). If the period changes , You can record it , Make sure that the tags are still as they were .
边栏推荐
- Google Earth engine (GEE) - sentinel-1 comprehensively check the difference between automatic landslide monitoring before and after two months (Guatemala as an example)
- 模板:P6114 【模板】Lyndon 分解&Runs(字符串)
- Calculation of carbon emissions
- Notice on the third national operation research / data, model and decision-making course teaching seminar in 2022
- [从零开始学习FPGA编程-38]:进阶篇 -语法-函数与任务
- How to write test cases
- 海外new things | 软件开发商「Dynaboard」种子轮融资660万美元,开发低代码平台连接设计、产品和开发人员
- 垃圾回收器
- Oracle JDBC 驱动
- Unittest framework
猜你喜欢

Overseas new things | zoovu, an American AI startup, raised a new round of financing of US $169million to optimize the online "product discovery" experience for consumers

Come and watch – tpt18 new report

Pytest框架实现前后置的处理

Exness: the impact of inflation in the United States is too great, and the leaders of the Federal Reserve have expressed their position one after another

中国游戏的“外卷”大时代,中小厂商如何破解出海难题?

MATLAB实现的基于对称TSP问题研究

Cisco(59)——Hub&Spoke MPLS

既不是研发顶尖高手,也不是销售大牛,为何偏偏获得 2 万 RMB 的首个涛思文化奖?
![[ros2 basics] Introduction to navigation2 navigation system](/img/7f/ad6af972460d7a78fb28d9b4c24477.png)
[ros2 basics] Introduction to navigation2 navigation system

使用unittest框架生成测试报告
随机推荐
南京大学 静态软件分析(static program analyzes)-- introduction 学习笔记
qtcreator报错解决
[observation] Microsoft's "cloud + end" comprehensive innovation makes hybrid cloud simpler, more flexible and more secure
进击的程序员,如何提升研发效能?|直播预告
3M互助智能合约系统开发搭建技术
[mathematical modeling] Matlab Application Practice Series (95) - application cases of time series prediction (with matlab code)
rtmp webrtc 协议 openssl 等安装
三色标记清除法
Unittest框架
2022年第三届全国运筹学/数据、模型与决策课程教学研讨会通知
About product operation strategy
面向流动人口管理的人脸验证系统设计及实现 论文+答辩PPT+项目工程文件
Google Earth engine (GEE) - use sentinel-2 data acquisition to obtain the NDVI difference of one month ago (Guatemala as an example)
Beaucoup de sociétés de logiciels sont en fait des "blagues"
快速排序简单思路及程序
之前的装机记录
ESP8266/ESP32 通過TimeLib庫獲取NTP時間方法
第十六周总结
我给航母做3D还原:这三处细节,太震撼了…
Unittest framework