GC Class

GC Class

Developers use the GC class as an interface to the GC in .NET. You can manipulate and monitor garbage collection with the GC class. Some GC commands affect the normal operation of the garbage collection. Garbage collection usefully performs best without developer interference. Table 14-2 lists the static members of the GC class.

Table 14-2: GC Class

Member Name

Description

MaxGeneration

This property is an integer property and gets the maximum number of generations.

AddMemoryPressure

This method forces the GC to recognize memory allocations for unmanaged resources.

Collect

This method forces garbage collection on a specific generation. There are two overloads of the method. The no-argument Collect performs garbage collection for all the generations. The one-argument Collect has a parameter that specifies the oldest generation to be collected.

CollectionCount

This method returns the number of garbage collection cycles for the specified generation.

GetGeneration

This method returns the generation of the specified object.

GetTotalMemory

This method returns the total number of bytes thought to be allocated for the managed heap. You can wait on garbage collection and finalization by setting the Boolean parameter to true.

KeepAlive

This method keeps alive the specified object from the beginning of the current routine to where the KeepAlive method is called.

RemoveMemoryPressure

This method removes some of the memory pressure set aside for unmanaged resources.

ReRegisterForFinalize

This method reattaches a finalizer to a resurrected object.

SuppressFinalize

This method suppresses future finalization of the specified object.

WaitForPendingFinalizers

This method suspends the current thread until the Finalization queue is empty.