原文

https://en.wikipedia.org/wiki/Tracing_garbage_collection#TRI-COLOR

Because of these performance problems, most modern tracing garbage collectors implement some variant of the tri-color marking abstraction, but simple collectors (such as the mark-and-sweep collector) often do not make this abstraction explicit. Tri-color marking works as described below.

Three sets are created – white, black and gray:

  • The white set, or condemned set, is the set of objects that are candidates for having their memory recycled.
  • The black set is the set of objects that can be shown to have no outgoing references to objects in the white set, and to be reachable from the roots. Objects in the black set are not candidates for collection.
  • The gray set contains all objects reachable from the roots but yet to be scanned for references to “white” objects. Since they are known to be reachable from the roots, they cannot be garbage-collected and will end up in the black set after being scanned.

In many algorithms, initially the black set starts as empty, the gray set is the set of objects which are directly referenced from roots and the white set includes all other objects. Every object in memory is at all times in exactly one of the three sets. The algorithm proceeds as following:

  1. Pick an object from the gray set and move it to the black set.
  2. Move each white object it references to the gray set. This ensures that neither this object nor any object it references can be garbage-collected.
  3. Repeat the last two steps until the gray set is empty.

When the gray set is empty, the scan is complete; the black objects are reachable from the roots, while the white objects are not and can be garbage-collected.

Since all objects not immediately reachable from the roots are added to the white set, and objects can only move from white to gray and from gray to black, the algorithm preserves an important invariant – no black objects reference white objects. This ensures that the white objects can be freed once the gray set is empty. This is called the tri-color invariant. Some variations on the algorithm do not preserve this invariant but use a modified form for which all the important properties hold.

The tri-color method has an important advantage – it can be performed “on-the-fly”, without halting the system for significant periods of time. This is accomplished by marking objects as they are allocated and during mutation, maintaining the various sets. By monitoring the size of the sets, the system can perform garbage collection periodically, rather than as needed. Also, the need to touch the entire working set on each cycle is avoided.

三色标记(Tri-color marking)
An example of tri-color marking on a heap with 8 objects. White, grey, and black objects are represented by light-grey, yellow, and blue, respectively.

渣渣翻译

由于一些性能问题,大多数现代跟踪垃圾收集器实现了某些变种的三色标记,但是简单收集器(如标记-清除收集器)通常不会显式地实现这种抽象。三色标记工作如下所述。

三组被创建 ——白色,黑色和灰色:

  • 白色集合,或废弃集合,是候选对象的集合,它们的内存将被回收。
  • 黑色集合是没有引用到白色集合的对象集合,并且可以从根开始访问到。黑色集合中的对象不是集合的候选对象。
  • 灰色集合全部是根结点可到达的对象,但是存在尚未扫描到的“白色”对象的引用。由于已知从根可以到达它们,所以它们不能被垃圾收集,在被扫描后会在黑色集合中结束。

在许多算法中,最初黑色集合是空的,灰色集合是直接从根引用的对象的集合,而白色集合包括所有其他对象。内存中的每个对象在任何时候都恰好处于这三个集合中的一个。算法进行如下:

  1. 从灰色集合中选择一个对象,并将其移动到黑色集合中。
  2. 将它引用的每个白色对象移动到灰色集合中。这可以确保该对象和它引用的任何对象都不会被垃圾收集。
  3. 重复上面两个步骤,直到灰色集合为空。

当灰度集合为空时,扫描完成;黑色的对象是可以从根中到达的,而白色的对象则不能并且可以被垃圾收集。

由于所有不能立即从根到达的对象都被添加到了白色集合中,并且对象只能从白色集合移动到灰色集合,从灰色集合移动到黑色集合,这样算法保留了一个重要的不变量——没有黑色对象引用白色对象。这确保了在灰色集合为空时可以释放白色对象。这叫做三色不变量。算法的一些变化不保持这个不变,而是使用一种修改的形式,确保所有重要的性质保持不变。

三色法有一个重要的优点——它可以“即时”执行,而不需要在很长一段时间内停止系统。这是通过在分配对象时以及在变化期间标记对象、维护各种集合来实现的。通过监视集合的大小,系统可以定期执行垃圾收集,而不是根据需要执行。此外,也避免了在每个周期中接触整个工作集的需要。

三色标记(Tri-color marking)

这有8个对象的堆上进行三色标记的示例。白色、灰色和黑色物体分别用浅灰色、黄色和蓝色表示。

相关文章:

  • 2021-04-14
  • 2022-12-23
  • 2021-05-14
  • 2021-07-11
  • 2022-12-23
  • 2021-10-27
  • 2022-12-23
  • 2021-07-06
猜你喜欢
  • 2023-04-04
  • 2021-04-20
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
相关资源
相似解决方案