Поделиться через


GC Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Controls the system garbage collector, a service that automatically reclaims unused memory.

Inheritance Hierarchy

System.Object
  System.GC

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public NotInheritable Class GC
public static class GC

The GC type exposes the following members.

Properties

  Name Description
Public propertyStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 MaxGeneration Gets the maximum number of generations that the system currently supports.

Top

Methods

  Name Description
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Collect() Induces an immediate garbage collection of all generations.
Public methodStatic memberSupported by Silverlight for Windows Phone Collect(Int32) Security Critical. Induces an immediate garbage collection of a specified generation.
Public methodStatic memberSupported by Silverlight for Windows Phone GetGeneration Security Critical. Retrieves the generation of a specified object.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetTotalMemory Retrieves the number of bytes currently thought to be allocated. A parameter indicates whether this method can wait a short interval before returning, to allow the system to collect garbage and finalize objects.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 KeepAlive References the specified object, which makes it ineligible for garbage collection from the start of the current routine to the point where this method is called.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 ReRegisterForFinalize Requests that the system call the finalizer for the specified object for which SuppressFinalize has previously been called.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 SuppressFinalize Requests that the system not call the finalizer for the specified object.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 WaitForPendingFinalizers Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue.

Top

Remarks

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.

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 Collect method.

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 tries to finalize objects that are not referenced.

  3. 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 KeepAlive 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 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 indicates the generation to which an object belongs. 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.

Notes to Implementers

This implementation of the garbage collector supports three generations of objects.

MaxGeneration is used to determine the maximum generation number supported by the system. Object aging allows applications to target garbage collection at a specific set of generations rather than requiring the garbage collector to evaluate all generations.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference