【发布时间】:2012-02-14 23:23:44
【问题描述】:
我正在使用我正在研究的简单脚本语言 API 实现标记和清除垃圾收集,并且一直在阅读有关垃圾收集的各种实现的信息。诸如 Lua 之类的 API 对白名单、灰名单和黑名单使用标记和清除。
问题是,我似乎无法解释为什么会有这样的列表以及为什么将它们放入这些特定的颜色中。
在我当前的简单实现中,我只是使用“死”或“活”状态。在扫描中,死对象被删除。我正在使用本机堆,所以我没有在我的 GC 中做任何移动。
我是用 C 写的。
// Performs a full garbage collection
void GorCollect(GorContext *ctx)
{
Value *v, *end;
Collectable *c, *n;
// mark stack references
end = ctx->stack + ctx->stackTop + 1;
v = ctx->stack;
while(v != end)
{
if (gvisgc(v) && v->v.gc) // mark if a collectable obj
Mark(v->v.gc);
v = v++;
}
// mark global references
if (ctx->global)
Mark((Collectable *)ctx->global); // ctx->global is a collectable obj
// perform sweep
c = ctx->gchead; // full list of collectable objs
ctx->gchead = 0;
while(c) {
n = c->next;
// destroy unmarked collectable
if (!c->marked)
FreeCollectable(ctx, c);
// rebuild gc list (singly-linked)
else
{
c->marked = 0;
if (!ctx->gchead)
c->next = 0;
else
c->next = ctx->gchead;
ctx->gchead = c;
}
c = n;
}
}
【问题讨论】:
-
why they are put into these specific colours- 因为它们很漂亮! -
搜索“mark and sweep white gray black”导致我:memorymanagement.org/glossary/t.html#tri-color.marking。该页面特别提到该算法的一个重要属性是它是“正确的”,所以我的猜测是天真的方法有一些边缘情况会中断。
-
另外:en.wikipedia.org/wiki/Mark_and_sweep#Naive_mark-and-sweep 列出了幼稚方法的主要缺点,即如果不暂停进程就无法执行。
-
我想我需要知道的是,考虑到我简单的黑白实现,使它成为三色 GC 会发生什么变化(除了标记状态的明显 GC 标志)?而不是仅仅以抽象的方式谈论它的页面。在我的实现中,我有两个“根集”,对吗? (堆栈和全局对象 - 对象是关联数组样式对象)。
标签: c garbage-collection mark-and-sweep