语言

GC.WaitForFullGCApproach 方法

定义

返回已注册通知的状态,用于确定公共语言运行时是否即将进行完全阻止垃圾回收。

重载

名称 说明
WaitForFullGCApproach()

返回已注册通知的状态,用于确定公共语言运行时是否即将进行完全阻止垃圾回收。

WaitForFullGCApproach(Int32)

返回在指定的超时时间段内,用于确定公共语言运行时是否即将进行完全阻止垃圾回收的已注册通知的状态。

WaitForFullGCApproach(TimeSpan)

返回在指定的超时时间段内,用于确定公共语言运行时是否即将进行完全阻止垃圾回收的已注册通知的状态。

WaitForFullGCApproach()

Source:
GC.CoreCLR.cs
Source:
GC.CoreCLR.cs
Source:
GC.CoreCLR.cs
Source:
GC.CoreCLR.cs
Source:
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)

Source:
GC.CoreCLR.cs
Source:
GC.CoreCLR.cs
Source:
GC.CoreCLR.cs
Source:
GC.CoreCLR.cs
Source:
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)

Source:
GC.cs
Source:
GC.cs
Source:
GC.cs
Source:
GC.cs
Source:
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 方法的超时

返回

已注册的完整 GC 通知的状态

适用于