[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
Controls the system garbage collector, a service that automatically reclaims unused memory.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
|ReRegisterForFinalize||Requests that the system call the finalizer for the specified object for which SuppressFinalize has previously been called.|
|SuppressFinalize||Requests that the system not call the finalizer for the specified object.|
|WaitForPendingFinalizers||Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue.|
The methods in this class influence when garbage collection is performed on an object and when resources allocated by an object are released. Properties in this class provide information about the total amount of memory available in the system and the age category, or generation, of memory allocated to an object.
Garbage collection consists of the following steps:
The garbage collector searches for managed objects that are referenced in managed code.
The garbage collector tries to finalize objects that are not referenced.
The garbage collector frees objects that are not referenced 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 might free objects that are being used exclusively in unmanaged code unless explicitly prevented from doing so. The method provides a mechanism that prevents the garbage collector from collecting objects that are still in use in unmanaged code.
Aside from managed memory allocations, implementations of the garbage collector do 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 can implement a finalizer.
In most cases, finalizers are implemented by overriding the Object..::..Finalize method; however, types written in C# or C++ implement destructors, which compilers turn into an override of 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; for example, the SuppressFinalize method explicitly prevents a finalizer from being called. 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 can implement the IDisposable interface, which contains the IDisposable..::..Dispose method that performs resource management and cleanup tasks. Classes that implement 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 Dispose method; however, implementations of the Dispose method can call methods in the class to customize the finalization behavior of the garbage collector.