語言

GC.WaitForFullGCApproach 方法

定義

回傳註冊通知狀態,用以判斷公共語言執行時是否即將發生完整的阻塞性垃圾回收。

多載

名稱 Description
WaitForFullGCApproach()

回傳註冊通知狀態,用以判斷公共語言執行時是否即將發生完整的阻塞性垃圾回收。

WaitForFullGCApproach(Int32)

在指定的逾時期內,回傳已註冊通知的狀態,用以判斷公共語言執行時是否即將出現完整的阻塞性垃圾回收。

WaitForFullGCApproach(TimeSpan)

在指定的逾時期內,回傳已註冊通知的狀態,用以判斷公共語言執行時是否即將出現完整的阻塞性垃圾回收。

WaitForFullGCApproach()

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

回傳註冊通知狀態,用以判斷公共語言執行時是否即將發生完整的阻塞性垃圾回收。

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 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
來源:
GC.CoreCLR.cs
來源:
GC.CoreCLR.cs

在指定的逾時期內,回傳已註冊通知的狀態,用以判斷公共語言執行時是否即將出現完整的阻塞性垃圾回收。

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
來源:
GC.cs
來源:
GC.cs

在指定的逾時期內,回傳已註冊通知的狀態,用以判斷公共語言執行時是否即將出現完整的阻塞性垃圾回收。

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進場的暫停時間

傳回

已註冊的完整綠書通知狀態

適用於