纯属阅读总结。
《垃圾回收算法手册:自动内存管理的艺术》英文版还是靠谱一点。
1. 标准引用计数
2. 延迟引用计数
先不释放 childrean 在重新分配使用的时候(复用)发现有 children 才顺便释放。
3. 标记清扫
First, live memory is marked by traversing the object graph starting at the roots.
Next, all unmarked memory is reclaimed in the sweep phase.
The algorithm starts by marking the roots. Marking an object includes finding its children and marking them.
By marking the roots, all reachable objects will be marked.
The sweeping phase traverses the heap and all unmarked objects found are reclaimed.
简单翻译一下: 从根开始标记活着的对象,标记完成之后遍历堆回收没有被标记的。
整个算法过程是不断构建一个标记对象的树。从根开始,扫描其子对象,再扫描子对象的子对象。
所以某一时刻,自己和所有子对象已标记完的对象(黑色),自己被扫描 ,但自己的子对象没有被标记完(灰色),自己没有被标记(白色)。清扫阶段就是清扫掉还是白色的对象。
想要正确的构建这课标记树,需要在构建过程中维持三色不变式 :
保证已扫描完的对象(黑色)永远不指向未被扫描的对象(白色)。为什么(要保证树构建的正确性,维持这个不变式)
如果不 stop-the-world 并发执行 GC, 那么中途有正常程序的赋值,黑色对象会指向一个白色对象(即产生一个白色的子对象,可以理解指向就是引用,见下图)
如何保证,摘选几种 barrier (对象赋值和读取的拦截代码)
意思是: 如果之前是白色的对象,赋值之后,左值变成灰色。
意思是: 赋值之后,白色右值变成灰色。
所以这两种都是保证,黑色不指向白色。必然是把打破的一方改变颜色。
4. 标记整理
Two pointers are used to point out the first free region, and the last reachable region. Objects are moved from the end of the heap to the first free slot. When the two pointers meet, all memory is compacted. Forward references, which are stored in the first cell of moved objects, are then used to update references from other objects. The algorithm does not require any extra space (except for the mark bit), but the objects are re-ordered. This is not a problem in most systems, but some systems depend on the ordering of objects.
简单翻译一下: 活着对象从后往前填充空闲 slot 。两个游标相等时算法结束。
(需要注意的是,被移动的对象,可能被指,那么指向关系需要更改到正确的地址)
5. 复制算法
上图是一种形象的描述。 构建标记图还是需要的(虚拟的构建)。只不过这个构建过程是把灰色的对象移动到 to-space 而且需要构建对象指向的正确关系。扫描完成之后,from-space 可以整体清除。
暂时放弃理解 stop-the-world 。
具体构建算法:
A GC cycle starts by copying the roots into to-space. When an object is copied, the new address is stored in the from-space copy. Since the from-space copy will never be referred to again, the forward address can over- write data in the object. Thus, no extra field is needed.
Objects in to-space are grey or black. To separate grey objects from black ones, a pointer named scan is used. Objects to the left of scan have been scanned and are thus black. The object that scan points to is being scanned. While an object is being scanned, its children are examined. If a child reference refers into from-space and the object has not been copied, it is copied into to-space and the child reference is updated to refer to the to- space copy. If the child has been copied, the child reference is updated to refer to the to-space copy using the forward address stored in from-space. When all roots have been copied and all copied objects have been scanned, the collector is finished.(这是一个迭代的过程。根对象一个接着一个的处理。)
The algorithm is a stop-and-copy algorithm, because it stops the mutator to perform garbage collection. Thus, it may interrupt the mutator for long periods of time which makes it unsuitable for use in most real-time systems.
简单翻译一下: scan 指向正在扫描的对象,其子对象在 from 的就移动到 to 。要注意移动的时候是往空闲区放。而且只有被 scan 对象所有子对象移动完,才能标记为黑。(其实就是虚拟的构建一个标记树)
想要不 stop-the-world 。 意味着复制时也可以正常执行程序,要维持三色不变式。同样需要 barrier。
意思是: 赋值时,如果右对象还在 from-space. 需要把它移动到 to-space 。forward 一个字段表示我指向的地方可以代替我。读的拦截就是,并发过程中 forward 永远指向对的地方。(参考写的代码)