GC.WaitForFullGCApproach 方法

定義

傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

多載

WaitForFullGCApproach()

傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

WaitForFullGCApproach(Int32)

在指定的逾時期間傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

WaitForFullGCApproach(TimeSpan)

在指定的逾時期間傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

WaitForFullGCApproach()

來源:
GC.CoreCLR.cs
來源:
GC.CoreCLR.cs
來源:
GC.CoreCLR.cs

傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

public:
 static GCNotificationStatus WaitForFullGCApproach();
public static GCNotificationStatus WaitForFullGCApproach ();
[System.Security.SecurityCritical]
public static GCNotificationStatus WaitForFullGCApproach ();
static member WaitForFullGCApproach : unit -> GCNotificationStatus
[<System.Security.SecurityCritical>]
static member WaitForFullGCApproach : unit -> GCNotificationStatus
Public Shared Function WaitForFullGCApproach () As GCNotificationStatus

傳回

已註冊記憶體回收通知的狀態。

屬性

範例

下列範例示範如何使用這個方法來判斷是否已接近完整、封鎖垃圾收集。 每當通知的狀態為 Succeeded 時,會呼叫使用者方法 OnFullGCApproachNotify 以執行動作以回應接近的集合。 此程式碼範例是針對 垃圾收集通知 主題所提供的較大範例的一部分。

// Check for a notification of an approaching collection.
GCNotificationStatus s = GC::WaitForFullGCApproach();
if (s == GCNotificationStatus::Succeeded)
{
    Console::WriteLine("GC Notifiction 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 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 an approaching collection.
match GC.WaitForFullGCApproach() with
| GCNotificationStatus.Succeeded ->
    printfn "GC Notification raised."
    onFullGCApproachNotify ()
    // Check for a notification of a completed collection.
    match GC.WaitForFullGCComplete() with
    | GCNotificationStatus.Succeeded ->
        printfn "GC Notification raised."
        onFullGCCompleteEndNotify ()
    | GCNotificationStatus.Canceled ->
        printfn "GC Notification cancelled."
        broken <- true
    | _ ->
        // Could be a time out.
        printfn "GC Notification not applicable."
        broken <- true
| GCNotificationStatus.Canceled ->
    printfn "GC Notification cancelled."
    broken <- true
| _ ->
    // This can occur if a timeout period
    // is specified for WaitForFullGCApproach(Timeout)
    // or WaitForFullGCComplete(Timeout)
    // and the time out period has elapsed.
    printfn "GC Notification not applicable."
    broken <- true
' Check for a notification of an approaching collection.
Dim s As GCNotificationStatus = GC.WaitForFullGCApproach
If (s = GCNotificationStatus.Succeeded) Then
    Console.WriteLine("GC Notification raised.")
    OnFullGCApproachNotify()
ElseIf (s = GCNotificationStatus.Canceled) Then
    Console.WriteLine("GC Notification cancelled.")
    Exit While
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.")
    Exit While
End If

備註

GCNotificationStatus使用這個方法所傳回的列舉,判斷使用 RegisterForFullGCNotification 方法註冊的目前垃圾收集通知狀態。 您也可以使用 WaitForFullGCComplete 方法來判斷完整垃圾收集是否已完成。

列舉傳 Succeeded 回 時,您可以執行工作,例如防止其他物件被配置,並使用 方法自行 Collect 產生集合。 請注意,通知不保證會發生完整垃圾收集,只有條件已達到可進行完整垃圾收集的臨界值。

此方法會無限期等候,以取得垃圾收集通知。 如果您想要指定方法在無法取得通知時傳回的 GC.WaitForFullGCApproach(Int32) 逾時期間,請使用 方法多載。 如果您在未指定逾時的情況下呼叫這個方法,則可以在等候時間超過慣用的時間時呼叫 CancelFullGCNotification 方法。

您應該使用方法的呼叫 WaitForFullGCComplete 來遵循這個方法,以確定您有完整的垃圾收集。 單獨呼叫這個方法會導致不確定的結果。

另請參閱

適用於

WaitForFullGCApproach(Int32)

來源:
GC.CoreCLR.cs
來源:
GC.CoreCLR.cs
來源:
GC.CoreCLR.cs

在指定的逾時期間傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

public:
 static GCNotificationStatus WaitForFullGCApproach(int millisecondsTimeout);
public static GCNotificationStatus WaitForFullGCApproach (int millisecondsTimeout);
[System.Security.SecurityCritical]
public static GCNotificationStatus WaitForFullGCApproach (int millisecondsTimeout);
static member WaitForFullGCApproach : int -> GCNotificationStatus
[<System.Security.SecurityCritical>]
static member WaitForFullGCApproach : int -> GCNotificationStatus
Public Shared Function WaitForFullGCApproach (millisecondsTimeout As Integer) As GCNotificationStatus

參數

millisecondsTimeout
Int32

在可以取得通知之前所要等待的時間長度。 指定 -1 會無限期等待。

傳回

已註冊記憶體回收通知的狀態。

屬性

例外狀況

millisecondsTimeout 必須是非負數或小於或等於 Int32.MaxValue 或 -1。

備註

GCNotificationStatus使用這個方法所傳回的列舉,判斷使用 RegisterForFullGCNotification 方法註冊的目前垃圾收集通知狀態。 您也可以使用 WaitForFullGCComplete 方法來判斷完整垃圾收集是否已完成。

請注意,不論 指定的 millisecondsTimeout 值為何,每當取得垃圾收集通知狀態時,這個方法就會立即傳回。 如果在逾時之前 millisecondsTimeout 未取得垃圾收集通知狀態,這個方法會傳 NotApplicable 回 。

列舉傳 Succeeded 回 時,您可以執行工作,例如防止其他物件被配置,並使用 方法自行 Collect 產生集合。 請注意,通知不保證會發生完整垃圾收集,只有條件已達到可進行完整垃圾收集的臨界值。

當您無法等候逾時期間經過時,可以呼叫 CancelFullGCNotification 方法。

您應該使用方法的呼叫 WaitForFullGCComplete 來遵循這個方法,以確定您有完整的垃圾收集。 單獨呼叫這個方法會導致不確定的結果。

另請參閱

適用於

WaitForFullGCApproach(TimeSpan)

來源:
GC.cs
來源:
GC.cs
來源:
GC.cs

在指定的逾時期間傳回註冊通知的狀態,以判斷 Common Language Runtime 是否即將執行完整的封鎖記憶體回收。

public:
 static GCNotificationStatus WaitForFullGCApproach(TimeSpan timeout);
public static GCNotificationStatus WaitForFullGCApproach (TimeSpan timeout);
static member WaitForFullGCApproach : TimeSpan -> GCNotificationStatus
Public Shared Function WaitForFullGCApproach (timeout As TimeSpan) As GCNotificationStatus

參數

timeout
TimeSpan

等候完整 GC 方法的逾時

傳回

已註冊的完整 GC 通知狀態

適用於