1

我正在使用 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,我尝试了多个阈值,但结果相同。

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

4

0 回答 0