-1

根据我的阅读,GC 从“根”开始,并在 GC 运行扫描之前跟踪并标记所有活动对象。但我不确定是什么构成了根。例如,在下面的代码中,Main 方法从一个线程开始并存在。那么当 GC 启动时,它是如何知道选择哪个部分作为 root 的呢?

         static void Main()
         {

             var thread = new Thread(SomeLongRunningProcess);
             thread.Start();

         }

        static void SomeLongRunningProcess()
        {


        }
4

1 回答 1

5

Roots include

  • static fields (all static fields of all types in all assemblies loaded, for each AppDomain in the process)
  • local variables (including those in CPU registers, for each thread)
  • the f-reachable queue (a list of objects having a finalizer)
  • the finalization queue (a list of objects otherwise garbage, but waiting for finalization)
于 2013-10-02T15:59:52.037 回答