kal*_*lix 2 c# reference object
If an object has 5 references can that object find out what is referenced to him?
Because I know that Java (and hopefully C#) have a list of that for the GC.
不,没有"参考列表".GC不需要知道引用对象的所有内容 - 它只需要知道是否有任何引用对象.
作为GC的一个非常粗略的模型,它将堆中的每个对象标记为垃圾,然后查看它知道为非垃圾的对象("根"对象).例如,它会寻找在每个线程的堆栈以及用于每个实例方法中的线程,它通常将1标记目标实例非垃圾.
Then it will go through each of those roots, and see what objects those ones refer to... and mark them as non-garbage. It will recurse down, finding everything it can. Whatever hasn't been marked as non-garbage can then be collected (or finalized).
As you can see from this algorithm, the GC doesn't need to keep a full list of references for each object - just a bit to say "garbage" or "non-garbage".
Obviously in reality the GC is much more complicated than this, in both Java and .NET, with generational garbage collectors and various strategies to minimise the GC "pause" and use multiple threads for GC. Hopefully this simplified view is enough to explain why even the GC doesn't have a list of references though.
1并非总是如此,在.NET的情况下.如果该方法从当前点开始不引用对象中的任何字段,则实例方法仍然在其中运行时,可以对对象进行垃圾回收.