简体   繁体   English

垃圾收集器:对象图的数据结构

[英]Garbage collector : data structure of the object graph

I read many resources on garbage collecting where they explained different algorithm. 我阅读了很多有关垃圾收集的资源,他们在其中解释了不同的算法。 However, I didn't find any explaining the representation of the graph object. 但是,我没有找到任何解释图形对象表示的解释。

My idea is quite simple : an oriented graph where vertex represent allocated memory block (on heap) and the edges the owner relationship. 我的想法很简单:一个面向图,其中顶点表示分配的内存块(在堆上),并且表示所有者关系的边缘。 Example : Consider 2 memory blocks m1 and m2, if m1 contains reference to a block inside m2 then add an edge (m1, m2). 示例:考虑2个内存块m1和m2,如果m1包含对m2内部一个块的引用,则添加一条边(m1,m2)。 These edges are weighted with the number of references to m2 that m1 contains (here just 1). 这些边通过m1包含的对m2的引用数(此处仅为1)加权。 Finally I've got a "virtual" memory vertex representing the stack, call it M0. 最后,我有一个表示堆栈的“虚拟”内存顶点,称为M0。 Every Mi reachable from M0 mustn't be garbage collected. 不能从M0到达的每个Mi都被垃圾收集。

Okay, now consider you want to add a memory block to the graph. 好的,现在考虑您要向图形添加一个内存块。 If we keep the vertices inside a set, then the complexity of adding a memory block should be of O(log(n)). 如果我们将顶点保留在集合内,则添加内存块的复杂度应为O(log(n))。 First question : Can we do better ? 第一个问题:我们能做得更好吗?

Idem for deleting. 同上删除。

Now, I'm asked to use this algorithm with a reference counting mechanism in C++ (shared_ptr). 现在,我被要求将此算法与C ++(shared_ptr)中的引用计数机制一起使用。 Firstly, is the reference counter isn't redundant with the in-degree of a vertex ? 首先,参考计数器在顶点的度数上是否多余?

Secondly, the key idea is to use the best of the reference counter (O(1) deleting/adding) with the best of the garbage collector algorithm (cleaning reference cycles), but is the overhead of adding/deleting each node in the object graph isn't a bit non-efficient ? 其次,关键思想是最好的引用计数器(O(1)删除/添加)与最好的垃圾收集器算法一起使用(清理参考周期),但是添加/删除对象中的每个节点的开销却很大。图不是有点效率不高吗?

What are the complexities of adding/removing in known garbage collector (java / C# / …) ? 在已知的垃圾收集器(java / C#/…)中添加/删除的复杂性是什么?

Thanks ! 谢谢 !

Well... your premise is off. 好吧...你的前提不对了。 Known garbage collectors don't maintain much state actually, at most a couple bits per object and some structure but that's it. 已知的垃圾收集器实际上并不能维护很多状态,每个对象最多只能有几个 ,并且只有某种结构,仅此而已。 Instead, they build up some state at each collection pass and let it die at the end of the pass. 取而代之的是,它们在每次收集通过时建立某种状态,并在传递结束时使其死亡。 This way, they need little (to no) instrumentation of the ownership relationships. 这样,他们几乎不需要(甚至不需要)所有权关系的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM