System.GC Class

public sealed class GC

Base Types

Object
  GC

Assembly

mscorlib

Library

BCL

Summary

Provides a mechanism for programmatic control of the garbage collector.

Description

[Note: The garbagecollector is responsible for tracking and reclaiming objects allocated in managed memory. Periodically, the garbage collector performs a garbage collection to reclaim memory allocated to objects for which there are no valid references. Garbage collections happen automatically when a request for memory cannot be satisfied using available free memory.

A garbage collection consists of the following steps:

  1. The garbage collector searches for managed objects that are referenced in managed code.
  2. The garbage collector attempts to finalize unreferenced objects.
  3. The garbage collector frees unreferenced objects and reclaims their memory.
During a collection, the garbage collector will not free an object if it finds one or more references to the object in managed code. However, the garbage collector does not recognize references to an object from unmanaged code, and can free objects that are being used exclusively in unmanaged code unless explicitly prevented from doing so. The System.GC.KeepAlive(System.Object) method provides a mechanism that prevents the garbage collector from collecting objects that are still in use in unmanaged code.

Implementations of the garbage collector should track the following information:

Other than managed memory allocations, implementations of the garbage collector should not maintain information about resources held by an object, such as file handles or database connections. When a type uses unmanaged resources that must be released before instances of the type are reclaimed, the type should implement a finalizer . In most cases, finalizers are implemented by overriding the System.Object.Finalize method; however, types written in C# or C++ implement destructors, which compilers turn into an override of System.Object.Finalize.

In most cases, if an object has a finalizer, the garbage collector calls it prior to freeing the object. However, the garbage collector is not required to call finalizers in all situations. Also, the garbage collector is not required to use a specific thread to finalize objects, or guarantee the order in which finalizers are called for objects that reference each other but are otherwise available for garbage collection.

In scenarios where resources must be released at a specific time, classes should implement the IDisposable interface, which contains a single method (System.IDisposable.Dispose) that is used to perform resource management and cleanup tasks. Classes that implement System.IDisposable.Dispose must specify, as part of their class contract, if and when class consumers call the method to clean up the object. The garbage collector does not, by default, call the System.IDisposable.Dispose method; however, implementations of the System.IDisposable.Dispose method can call methods in the GC class to customize the finalization behavior of the garbage collector.

]

See Also

System Namespace

Members

GC Methods

GC.Collect Method
GC.KeepAlive Method
GC.ReRegisterForFinalize Method
GC.SuppressFinalize Method
GC.WaitForPendingFinalizers Method


GC.Collect Method

public static void Collect()

Summary

Requests that the garbage collector reclaim memory allocated to objects for which there are no valid references.

Description

A call to this method is only a suggestion; such a call does not guarantee that any inaccessible memory is, in fact, reclaimed.

See Also

System.GC Class, System Namespace

GC.KeepAlive Method

public static void KeepAlive(object obj);

Summary

Creates a reference to the specified object.

Parameters

obj
A Object that is not to be reclaimed by the garbage collector.

Description

The purpose of the System.GC.KeepAlive(System.Object) method is to ensure the existence of a reference to an object that is at risk of being reclaimed by the garbage collector prematurely.

The System.GC.KeepAlive(System.Object) method performs no operations and does not produce any side effects.

This method is required to be implemented in such a way as to prevent optimizing tools from omitting the method call from the executable code.

During program execution, after the call to the System.GC.KeepAlive(System.Object) method is executed, if there are no additional references to obj in managed code or data, obj is eligible for garbage collection.

See Also

System.GC Class, System Namespace

GC.ReRegisterForFinalize Method

public static void ReRegisterForFinalize(object obj);

Summary

Requests that the specified object be added to the list of objects that require finalization.

Parameters

obj
The Object to add to the set of objects that require finalization.

Exceptions

Exception TypeCondition
ArgumentNullExceptionobj is a null reference.

Description

The System.GC.ReRegisterForFinalize(System.Object) method adds obj to the list of objects that request finalization before the garbage collector frees the object. obj is required to be the caller of this method.

Calling the System.GC.ReRegisterForFinalize(System.Object) method does not guarantee that the garbage collector will call an object's finalizer. [Note: For more information, see System.Object.Finalize .]

[Note: By default, all objects that implement finalizers are added to the list of objects that require finalization; however, an object might have already been finalized, or might have disabled finalization by calling the System.GC.SuppressFinalize(System.Object) method.]

See Also

System.GC Class, System Namespace

GC.SuppressFinalize Method

public static void SuppressFinalize(object obj);

Summary

Instructs the garbage collector not to call the System.Object.Finalize method on the specified object.

Parameters

obj
The Object whose System.Object.Finalize method will not be called.

Exceptions

Exception TypeCondition
ArgumentNullExceptionobj is a null reference.

Description

The method removes obj from the set of objects that require finalization. obj is required to be the caller of this method.

[Note: Objects that implement the IDisposable interface should call this method from the System.IDisposable.Dispose method to prevent the garbage collector from calling System.Object.Finalize on an object that does not require it.]

See Also

System.GC Class, System Namespace

GC.WaitForPendingFinalizers Method

public static void WaitForPendingFinalizers();

Summary

Suspends the current thread until the set of objects waiting for finalization is empty.

Description

System.GC.WaitForPendingFinalizers blocks an application until all objects that are awaiting finalization have been finalized.

When the garbage collector finds objects that can be reclaimed, it checks each object to determine the object's finalization requirements. If an object implements a finalizer and has not disabled finalization by calling System.GC.SuppressFinalize(System.Object), the object is passed to the thread that handles finalization. The System.GC.WaitForPendingFinalizers method blocks until all finalizers have run to completion.

See Also

System.GC Class, System Namespace