使用英语阅读

通过


GCNotificationStatus 枚举

定义

提供相关信息,介绍下一次完整垃圾回收通知的当前注册。

C#
public enum GCNotificationStatus
C#
[System.Serializable]
public enum GCNotificationStatus
继承
GCNotificationStatus
属性

字段

名称 说明
Canceled 2

用户已取消当前注册。

Failed 1

由于某种原因,通知失败。

NotApplicable 4

如果没有垃圾回收通知的当前注册,或者发生了完整的 GC,但已作为后台 GC (完成,也就是说,与用户线程) (而不是完全阻止 GC)同时运行的 GC 时,可能会产生此值。 默认情况下,后台 GC 处于启用状态;禁用它可以提高预测准确性,但会导致更大的 GC 暂停。

Succeeded 0

已成功通知且未取消注册。

Timeout 3

millisecondsTimeout 参数为 WaitForFullGCApproach(Int32)WaitForFullGCComplete(Int32) 指定的时间已过。

示例

以下示例从 WaitForFullGCApproach 方法获取GCNotificationStatus枚举。 如果枚举返回 Succeeded,它将调用自定义方法 OnFullGCApproachNotify 以执行操作,以响应即将进行的完整垃圾回收。 此代码示例是为 垃圾回收通知 主题提供的更大示例的一部分。

C#
public static void WaitForFullGCProc()
{
    while (true)
    {
        // CheckForNotify is set to true and false in Main.
        while (checkForNotify)
        {
            // Check for a notification of an approaching collection.
            GCNotificationStatus s = GC.WaitForFullGCApproach();
            if (s == GCNotificationStatus.Succeeded)
            {
                Console.WriteLine("GC Notification raised.");
                OnFullGCApproachNotify();
            }
            else if (s == GCNotificationStatus.Canceled)
            {
                Console.WriteLine("GC Notification cancelled.");
                break;
            }
            else
            {
                // This can occur if a timeout period
                // is specified for WaitForFullGCApproach(Timeout)
                // or WaitForFullGCComplete(Timeout)
                // and the time out period has elapsed.
                Console.WriteLine("GC Notification not applicable.");
                break;
            }

            // Check for a notification of a completed collection.
            GCNotificationStatus status = GC.WaitForFullGCComplete();
            if (status == GCNotificationStatus.Succeeded)
            {
                Console.WriteLine("GC Notification raised.");
                OnFullGCCompleteEndNotify();
            }
            else if (status == GCNotificationStatus.Canceled)
            {
                Console.WriteLine("GC Notification cancelled.");
                break;
            }
            else
            {
                // Could be a time out.
                Console.WriteLine("GC Notification not applicable.");
                break;
            }
        }

        Thread.Sleep(500);
        // FinalExit is set to true right before
        // the main thread cancelled notification.
        if (finalExit)
        {
            break;
        }
    }
}

注解

RegisterForFullGCNotification使用 方法注册完整的垃圾回收通知。 WaitForFullGCApproach然后使用 方法或 WaitForFullGCComplete 方法返回GCNotificationStatus包含通知状态的枚举。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1

另请参阅