public sealed class GC
|
The garbage collector tracks and reclaims objects allocated in managed memory. Periodically, the garbage collector performs garbage collection to reclaim memory allocated to objects for which there are no valid references. Garbage collection happens automatically when a request for memory cannot be satisfied using available free memory. Alternatively, an application can force garbage collection using the GC.Collect method.
Garbage collection consists of the following steps:
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 GC.KeepAlive method provides a mechanism that prevents the garbage collector from collecting objects that are still in use in unmanaged code.
Other than 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. 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 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 IDisposable.Dispose method; however, implementations of the IDisposable.Dispose method can call methods in the GC class to customize the finalization behavior of the garbage collector.
It is recommended, but not required, that garbage collectors support object aging using generations. A generation is a unit of measure of the relative age of objects in memory. The generation number, or age, of an object tells which generation an object belongs to. Objects created more recently are part of newer generations, and have lower generation numbers than objects created earlier in the application life cycle. Objects in the most recent generation are in generation zero.
MaxGeneration | Read-only Gets the maximum number of generations the system currently supports. |
Collect | Overloaded:Collect() Forces garbage collection of all generations. |
Collect | Overloaded:Collect(int generation) Forces garbage collection from generation zero through a specified generation. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetGeneration | Overloaded:GetGeneration(object obj) Returns the current generation number of the specified object. |
GetGeneration | Overloaded:GetGeneration(WeakReference wo) Returns the current generation number of the target of a specified weak reference. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetTotalMemory | Retrieves the number of bytes currently thought to be allocated. A parameter indicates whether this method can wait a short interval before returning while the system collects garbage and finalizes objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
KeepAlive | References the specified object, making it ineligible for garbage collection from the start of the current routine to the point where this method is called. |
ReRegisterForFinalize | Requests that the system call the finalizer method for the specified object, for which GC.SuppressFinalize has previously been called. |
SuppressFinalize | Requests that the system not call the finalizer method for the specified object. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
WaitForPendingFinalizers | Suspends the current thread until the thread processing the queue of finalizers has emptied that queue. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public static int MaxGeneration {get;}
|
The garbage collector assumes that newer memory is more likely to be eligible for garbage collection than older memory. Therefore, the garbage collector improves its performance by adjusting generation numbers each time it reclaims memory, and the GC.MaxGeneration property value can grow over time.
If object aging is implemented, the GC.MaxGeneration property returns the maximum generation number used by the system; otherwise, this property returns zero.
Use the GC.MaxGeneration property to determine the maximum value you can specify when calling the GC.Collect method that takes a generation parameter.
public static void Collect(); |
All objects, regardless of how long they have been in memory, are considered for collection; however, objects that are referenced in managed code are not collected. Use this method to force the system to attempt to reclaim the maximum amount of available memory.
public static void Collect( |
generation
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | generation is less than zero or greater than the number of generations that exist. |
If object aging is implemented, the garbage collector does not collect objects with a generation number higher than the specified generation. If object aging is not implemented, the garbage collector considers all objects during the garbage collection.
Use the GC.MaxGeneration property to determine the maximum valid value of generation.
To have the garbage collector consider all objects regardless of their generation, use the version of this method that takes no arguments.
~GC(); |
obj
public static int GetGeneration( |
wo
Exception Type | Condition |
---|---|
ArgumentException | wo has already been garbage collected. |
public virtual int GetHashCode(); |
forceFullCollection
public Type GetType(); |
public static void KeepAlive( |
obj
This method references obj, making that object ineligible for garbage collection from the start of the routine to the point, in execution order, where this method is called. Code this method at the end, not the beginning, of the range of instructions where obj must be available.
The implementation of the GC.KeepAlive method is intentionally hidden, which prevents mechanisms such as optimizing compilers from omitting this method from the compiled code, and garbage collection from determining whether obj is no longer in use. This method performs no operations and produces no side effects.
protected object MemberwiseClone(); |
public static void ReRegisterForFinalize( |
obj
Exception Type | Condition |
---|---|
ArgumentNullException | obj is null. |
InvalidOperationException | This method is not called within a property, method, or constructor. |
Calling the GC.ReRegisterForFinalize method does not guarantee that the garbage collector will call an object's finalizer.
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 GC.SuppressFinalize method.
A finalizer can use this method to resurrect itself or an object it references.
public static void SuppressFinalize( |
obj
Exception Type | Condition |
---|---|
ArgumentNullException | obj is null. |
ExecutionEngineException | This method is not called within a property, method, or constructor. |
Objects that implement the IDisposable interface can call this method from the IDisposable.Dispose method to prevent the garbage collector from calling Object.Finalize on an object that does not require it.
public virtual string ToString(); |
public static void WaitForPendingFinalizers(); |
Finalizers are run on a separate thread of execution, so there is no guarantee that this method will terminate. However, this thread can be interrupted by another thread while the GC.WaitForPendingFinalizers method is in progress. For example, you can start another thread that waits for a period of time then interrupts this thread if this thread is still suspended.