閱讀英文

共用方式為


GCNotificationStatus 列舉

定義

提供有關下次完整記憶體回收的目前通知註冊資訊。

C#
public enum GCNotificationStatus
C#
[System.Serializable]
public enum GCNotificationStatus
繼承
GCNotificationStatus
屬性

欄位

名稱 Description
Canceled 2

使用者已取消目前的註冊。

Failed 1

通知因為任何原因而失敗。

NotApplicable 4

當垃圾收集通知目前沒有註冊,或發生完整 GC 時,這個值可能會因為背景 GC (而完成,也就是大部分同時與使用者執行緒同時執行的 GC,) 而不是完整封鎖 GC。 預設會啟用背景 GC;停用它可改善預測精確度,但會產生較大的 GC 暫停。

Succeeded 0

通知成功且註冊未取消。

Timeout 3

已超過 WaitForFullGCApproach(Int32)WaitForFullGCComplete(Int32) 兩者之一的 millisecondsTimeout 參數所指定的時間。

範例

下列範例會 GCNotificationStatusWaitForFullGCApproach 方法取得列舉。 如果列舉傳回 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

另請參閱