.NET中的垃圾收集器(Garbage Collector in .NET)


How does the garbage collector know the objects and variables are out of scope so they can be collected by garbage collector?


简而言之:每个应用程序都有一组根。 Roots标识存储位置,这些位置引用托管堆上的对象或设置为null的对象。




这取自http://msdn.microsoft.com/en-us/magazine/bb985010.aspx - 关于垃圾收集的好文章。 “有趣”的部分是“垃圾收集算法”。 这不是一个很长的部分

In short: Every application has a set of roots. Roots identify storage locations, which refer to objects on the managed heap or to objects that are set to null.

When the garbage collector starts running, it makes the assumption that all objects in the heap are garbage.

The garbage collector starts walking the roots and building a graph of all objects reachable from the roots.

All objects not reachable are removed (memory is freed)

This is taken from http://msdn.microsoft.com/en-us/magazine/bb985010.aspx - good article about the garbage collection. The "interesting" part for you is "The Garbage Collection Algorithm". It is not a very long section