GC.WaitForFullGCApproach Method

Definition

Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

Overloads

WaitForFullGCApproach()

Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

WaitForFullGCApproach(Int32)

Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

WaitForFullGCApproach(TimeSpan)

Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

WaitForFullGCApproach()

Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

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

Returns

The status of the registered garbage collection notification.

Attributes

Examples

The following example shows how to use this method to determine whether a full, blocking garbage collection is approaching. Whenever the status of the notification is Succeeded, the user method OnFullGCApproachNotify is called to perform actions in response to the approaching collection. This code example is part of a larger example provided for Garbage Collection Notifications topic.

// 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

Remarks

Use the GCNotificationStatus enumeration returned by this method to determine the status of the current garbage collection notification that was registered by using the RegisterForFullGCNotification method. You can also use the WaitForFullGCComplete method to determine whether the full garbage collection has completed.

When the enumeration returns Succeeded, you can do tasks such as preventing additional objects from being allocated and inducing a collection yourself with the Collect method. Note that the notification does not guarantee that a full garbage collection will occur, only that conditions have reached the threshold that are favorable for a full garbage collection to occur.

This method waits indefinitely for a garbage collection notification to be obtained. If you want to specify a time-out period for the method to return if the notification cannot be obtained, use the GC.WaitForFullGCApproach(Int32) method overload. If you call this method without specifying a time-out, you can call the CancelFullGCNotification method if you are waiting longer than preferred.

You should follow this method with a call to the WaitForFullGCComplete method to make sure that you have had a full garbage collection. Calling this method alone causes indeterminate results.

See also

Applies to

WaitForFullGCApproach(Int32)

Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

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

Parameters

millisecondsTimeout
Int32

The length of time to wait before a notification status can be obtained. Specify -1 to wait indefinitely.

Returns

The status of the registered garbage collection notification.

Attributes

Exceptions

millisecondsTimeout must be either non-negative or less than or equal to Int32.MaxValue or -1.

Remarks

Use the GCNotificationStatus enumeration returned by this method to determine the status of the current garbage collection notification that was registered by using the RegisterForFullGCNotification method. You can also use the WaitForFullGCComplete method to determine whether the full garbage collection has completed.

Note that this method returns immediately whenever a garbage collection notification status is obtained, regardless of the value specified by millisecondsTimeout. If a garbage collection notification status is not obtained before millisecondsTimeout times out, this method returns NotApplicable.

When the enumeration returns Succeeded, you can do tasks such as preventing additional objects from being allocated and inducing a collection yourself with the Collect method. Note that the notification does not guarantee that a full garbage collection will occur, only that conditions have reached the threshold that are favorable for a full garbage collection to occur.

You can call the CancelFullGCNotification method when you cannot wait for the time-out period to elapse.

You should follow this method with a call to the WaitForFullGCComplete method to make sure that you have had a full garbage collection. Calling this method alone causes indeterminate results.

See also

Applies to

WaitForFullGCApproach(TimeSpan)

Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

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

Parameters

timeout
TimeSpan

The timeout on waiting for a full GC approach

Returns

The status of a registered full GC notification

Applies to