[ Team LiB ] Previous Section Next Section

GCCF 1.0, ECMA 1.0

System (mscorlib.dll)sealed class

This class allows you to control garbage collection programmatically. Garbage collection is the .NET service that periodically scans for unreferenced objects and reclaims the memory they occupy.

The garbage-collection service distinguishes between older and more recently allocated memory using generations. The most recently allocated memory is considered generation zero, and the oldest memory is in generation MaxGeneration. Because new allocations are likely to be freed before long-standing memory allocations, the garbage collector improves its performance by concentrating on lower generations of memory. You can find out the generation of an object using the GetGeneration( ) method. You can also get the number of memory bytes that are currently allocated using the GetTotalMemory( ) method. A forceFullCollection parameter indicates whether this method should wait a short interval before returning to collect and finalize some objects.

To force a full sweep garbage collection, use the Collect( ) method. You can improve performance by specifying the maximum generation that will be examined. Generally, it is best to let .NET perform garbage collection automatically when the system is idle.

Some developers have lamented a noticeable lack of deterministic finalization within a garbage-collected system; that is, because the object's lifetime is under the control of the garbage collector, there is no guarantee that an object is destroyed as soon as it becomes unreferenced. One approach used to try to compensate for this phenomenon is to call GC repeatedly in an effort to force the object's cleanup. This is both time-consuming and wasteful of the garbage collector's efforts, since a collection may involve not only recollection, but readjustment of object locations in memory. If a programmer requires more explicit control over when an object is cleaned up, the class can be declared as implementing the IDisposable interface (which consists of a single method, Dispose( )). This interface allows the object to participate in a using declaration, which guarantees that the object's Dispose( ) method is invoked when control leaves the declared scope block (see Section 2.9.5.7). Use of IDisposable is recommended over the use of Finalize( ) methods. This is due to a variety of reasons too numerous to explore here.

The KeepAlive( ) method is used to preserve the life of an object that is not strongly referenced. This is sometimes required when interacting with methods in unmanaged code (such as Win32 APIs or COM). The KeepAlive( ) method works in an unusual manner: it makes an object ineligible for garbage collection from the start of the current routine to the point where the KeepAlive( ) method is called. This unusual system prevents problems that could otherwise be created by compiler optimizations.

public sealed class GC {
// Public Static Properties
   public static int MaxGeneration{get; }
// Public Static Methods
   public static void Collect( );
   public static void Collect(int generation);
   public static int GetGeneration(object obj);
   public static int GetGeneration(WeakReference wo);
   public static long GetTotalMemory(bool forceFullCollection);
   public static void KeepAlive(object obj);
   public static void ReRegisterForFinalize(object obj);
   public static void SuppressFinalize(object obj);
   public static void WaitForPendingFinalizers( );
}
    [ Team LiB ] Previous Section Next Section