GC.WaitForFullGCApproach Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static GCNotificationStatus WaitForFullGCApproach()

Return Value

Type: System.GCNotificationStatus

The status of the registered garbage collection notification.

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.

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 Notification raised.");
else if (s == GCNotificationStatus.Canceled)
    Console.WriteLine("GC Notification cancelled.");
    // 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.");


for full trust for the immediate caller. This member cannot be used by partially trusted code.


requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

.NET Framework
Available since 2.0
Return to top