WaitForFullGCApproach错过了GC集合

时间:2012-12-12 11:38:03

标签: .net garbage-collection

我使用方法GC.RegisterForFullGCNotification,GC.WaitForFullGCApproach和GC.WaitForFullGCComplete来获取即将到来的垃圾收集的通知。 GC.WaitForFullGCApproach在一个不同的线程中循环,如下所示:

            while (!gcAbort)
            {
                GCNotificationStatus s = GC.WaitForFullGCApproach(10 * 60 * 1000);

                if (s == GCNotificationStatus.Succeeded)
                {
                    Console.WriteLine(GCState.Approaching, "GC is approaching ... ");
                    gcApproachNotificationCount++;
                }
                else
                {
                    if (s == GCNotificationStatus.NotApplicable)
                    {
                        Console.WriteLine("Concurrent garbage collection is enabled, or the time specified for the millisecondsTimeout parameter has expired and no garbage collection notification was obtained.");
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("Unexpected GCNotificationStatus " + s);
                        break;
                    }
                }

                if (forceGCWhenApproaching)
                {
                    int maxGenCollectionCountBefore = GC.CollectionCount(GC.MaxGeneration);
                    Thread.Sleep(12000);
                    int maxGenCollectionCountAfter = GC.CollectionCount(GC.MaxGeneration);
                    if (maxGenCollectionCountBefore == maxGenCollectionCountAfter)
                    {
                        Console.WriteLine("Inducing forced Gen {0} GC.. Collection count is {1}".FormatWith(GC.MaxGeneration, maxGenCollectionCountBefore));
                        GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, false);
                    }
                    else
                    {
                        Console.WriteLine("GC was already triggered by the system! Old collection count was {0}; new count is {1}".FormatWith(maxGenCollectionCountBefore, maxGenCollectionCountAfter));
                    }
                }

                // Check for a notification of a completed collection.
                s = GC.WaitForFullGCComplete(10 * 60 * 1000);
                if (s == GCNotificationStatus.Succeeded)
                {
                    Console.WriteLine(GCState.Okay, "GC completed.");
                }
                else
                {
                    Console.WriteLine(GCState.Unknown, "Unexpected GCNotificationStatus: " + s);
                    break;
                }
            }

问题是我没有收到有关所有垃圾收集的通知。所以我不时会看到这样的事情:

GC is approaching ... 

GC was already triggered by the system! Old collection count was 19; new count is 23

GC completed.

GC is approaching ...

GC was already triggered by the system! Old collection count was 33; new count is 37

GC completed.

这意味着在收集计数23和33之间我错过了所有这些。

在我的web.config文件中,我已禁用并发GC:

  <runtime>
    <gcConcurrent enabled="false" />
  </runtime>

对于GC.RegisterForFullGCNotification,我尝试了多个阈值但结果相同。

你能告诉我为什么我会观察到这种行为吗?

0 个答案:

没有答案