mscorlib(4.0.0.0) API with additions
System.GC Class Reference

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

Public Member Functions

static int GetGeneration (object obj)
 Returns the current generation number of the specified object. More...
 

Static Public Member Functions

static void AddMemoryPressure (long bytesAllocated)
 Informs the runtime of a large allocation of unmanaged memory that should be taken into account when scheduling garbage collection. More...
 
static void RemoveMemoryPressure (long bytesAllocated)
 Informs the runtime that unmanaged memory has been released and no longer needs to be taken into account when scheduling garbage collection. More...
 
static void Collect (int generation)
 Forces an immediate garbage collection from generation 0 through a specified generation. More...
 
static void Collect ()
 Forces an immediate garbage collection of all generations. More...
 
static void Collect (int generation, GCCollectionMode mode)
 Forces a garbage collection from generation 0 through a specified generation, at a time specified by a T:System.GCCollectionMode value. More...
 
static void Collect (int generation, GCCollectionMode mode, bool blocking)
 Forces a garbage collection from generation 0 through a specified generation, at a time specified by a T:System.GCCollectionMode value, with a value specifying whether the collection should be blocking. More...
 
static void Collect (int generation, GCCollectionMode mode, bool blocking, bool compacting)
 Forces a garbage collection from generation 0 through a specified generation, at a time specified by a T:System.GCCollectionMode value, with values that specify whether the collection should be blocking and compacting. More...
 
static int CollectionCount (int generation)
 Returns the number of times garbage collection has occurred for the specified generation of objects. More...
 
static void KeepAlive (object obj)
 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. More...
 
static int GetGeneration (WeakReference wo)
 Returns the current generation number of the target of a specified weak reference. More...
 
static void WaitForPendingFinalizers ()
 Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue. More...
 
static void SuppressFinalize (object obj)
 Requests that the common language runtime not call the finalizer for the specified object. More...
 
static void ReRegisterForFinalize (object obj)
 Requests that the system call the finalizer for the specified object for which M:System.GC.SuppressFinalize(System.Object) has previously been called. More...
 
static long GetTotalMemory (bool forceFullCollection)
 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. More...
 
static void RegisterForFullGCNotification (int maxGenerationThreshold, int largeObjectHeapThreshold)
 Specifies that a garbage collection notification should be raised when conditions favor full garbage collection and when the collection has been completed. More...
 
static void CancelFullGCNotification ()
 Cancels the registration of a garbage collection notification. More...
 
static GCNotificationStatus WaitForFullGCApproach ()
 Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent. More...
 
static GCNotificationStatus WaitForFullGCApproach (int millisecondsTimeout)
 Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent. More...
 
static GCNotificationStatus WaitForFullGCComplete ()
 Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime has completed. More...
 
static GCNotificationStatus WaitForFullGCComplete (int millisecondsTimeout)
 Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by common language the runtime has completed. More...
 
static bool TryStartNoGCRegion (long totalSize)
 Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available. More...
 
static bool TryStartNoGCRegion (long totalSize, long lohSize)
 Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available for the large object heap and the small object heap. More...
 
static bool TryStartNoGCRegion (long totalSize, bool disallowFullBlockingGC)
 Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available, and controls whether the garbage collector does a full blocking garbage collection if not enough memory is initially available. More...
 
static bool TryStartNoGCRegion (long totalSize, long lohSize, bool disallowFullBlockingGC)
 Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available for the large object heap and the small object heap, and controls whether the garbage collector does a full blocking garbage collection if not enough memory is initially available. More...
 
static void EndNoGCRegion ()
 Ends the no GC region latency mode. More...
 

Properties

static int MaxGeneration [get]
 Gets the maximum number of generations that the system currently supports. More...
 

Detailed Description

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

Definition at line 11 of file GC.cs.

Member Function Documentation

◆ AddMemoryPressure()

static void System.GC.AddMemoryPressure ( long  bytesAllocated)
static

Informs the runtime of a large allocation of unmanaged memory that should be taken into account when scheduling garbage collection.

Parameters
bytesAllocatedThe incremental amount of unmanaged memory that has been allocated.
Exceptions
T:System.ArgumentOutOfRangeExceptionbytesAllocated is less than or equal to 0.-or-On a 32-bit computer, bytesAllocated is larger than F:System.Int32.MaxValue.

Definition at line 111 of file GC.cs.

◆ CancelFullGCNotification()

static void System.GC.CancelFullGCNotification ( )
static

Cancels the registration of a garbage collection notification.

Exceptions
T:System.InvalidOperationExceptionThis member is not available when concurrent garbage collection is enabled. See the <gcConcurrent> runtime setting for information about how to disable concurrent garbage collection.

Definition at line 402 of file GC.cs.

◆ Collect() [1/5]

static void System.GC.Collect ( int  generation)
static

Forces an immediate garbage collection from generation 0 through a specified generation.

Parameters
generationThe number of the oldest generation to be garbage collected.
Exceptions
T:System.ArgumentOutOfRangeExceptiongeneration is not valid.

Definition at line 155 of file GC.cs.

◆ Collect() [2/5]

static void System.GC.Collect ( )
static

Forces an immediate garbage collection of all generations.

Definition at line 163 of file GC.cs.

◆ Collect() [3/5]

static void System.GC.Collect ( int  generation,
GCCollectionMode  mode 
)
static

Forces a garbage collection from generation 0 through a specified generation, at a time specified by a T:System.GCCollectionMode value.

Parameters
generationThe number of the oldest generation to be garbage collected.
modeAn enumeration value that specifies whether the garbage collection is forced (F:System.GCCollectionMode.Default or F:System.GCCollectionMode.Forced) or optimized (F:System.GCCollectionMode.Optimized).
Exceptions
T:System.ArgumentOutOfRangeExceptiongeneration is not valid.-or- mode is not one of the T:System.GCCollectionMode values.

Definition at line 176 of file GC.cs.

◆ Collect() [4/5]

static void System.GC.Collect ( int  generation,
GCCollectionMode  mode,
bool  blocking 
)
static

Forces a garbage collection from generation 0 through a specified generation, at a time specified by a T:System.GCCollectionMode value, with a value specifying whether the collection should be blocking.

Parameters
generationThe number of the oldest generation to be garbage collected.
modeAn enumeration value that specifies whether the garbage collection is forced (F:System.GCCollectionMode.Default or F:System.GCCollectionMode.Forced) or optimized (F:System.GCCollectionMode.Optimized).
blockingtrue to perform a blocking garbage collection; false to perform a background garbage collection where possible.
Exceptions
T:System.ArgumentOutOfRangeExceptiongeneration is not valid.-or- mode is not one of the T:System.GCCollectionMode values.

Definition at line 191 of file GC.cs.

◆ Collect() [5/5]

static void System.GC.Collect ( int  generation,
GCCollectionMode  mode,
bool  blocking,
bool  compacting 
)
static

Forces a garbage collection from generation 0 through a specified generation, at a time specified by a T:System.GCCollectionMode value, with values that specify whether the collection should be blocking and compacting.

Parameters
generationThe number of the oldest generation to be garbage collected.
modeAn enumeration value that specifies whether the garbage collection is forced (F:System.GCCollectionMode.Default or F:System.GCCollectionMode.Forced) or optimized (F:System.GCCollectionMode.Optimized).
blockingtrue to perform a blocking garbage collection; false to perform a background garbage collection where possible. See the Remarks section for more information.
compactingtrue to compact the small object heap; false to sweep only. See the Remarks section for more information.

Definition at line 204 of file GC.cs.

◆ CollectionCount()

static int System.GC.CollectionCount ( int  generation)
static

Returns the number of times garbage collection has occurred for the specified generation of objects.

Parameters
generationThe generation of objects for which the garbage collection count is to be determined.
Returns
The number of times garbage collection has occurred for the specified generation since the process was started.
Exceptions
T:System.ArgumentOutOfRangeExceptiongeneration is less than 0.

Definition at line 242 of file GC.cs.

◆ EndNoGCRegion()

static void System.GC.EndNoGCRegion ( )
static

Ends the no GC region latency mode.

Exceptions
T:System.InvalidOperationExceptionThe garbage collector is not in no GC region latency mode. See the Remarks section for more information. -or-The no GC region latency mode was ended previously because a garbage collection was induced. -or-A memory allocation exceeded the amount specified in the call to the M:System.GC.TryStartNoGCRegion(System.Int64) method.

Definition at line 549 of file GC.cs.

◆ GetGeneration() [1/2]

static int System.GC.GetGeneration ( object  obj)

Returns the current generation number of the specified object.

Parameters
objThe object that generation information is retrieved for.
Returns
The current generation number of obj .

◆ GetGeneration() [2/2]

static int System.GC.GetGeneration ( WeakReference  wo)
static

Returns the current generation number of the target of a specified weak reference.

Parameters
woA T:System.WeakReference that refers to the target object whose generation number is to be determined.
Returns
The current generation number of the target of wo .
Exceptions
T:System.ArgumentExceptionGarbage collection has already been performed on wo .

Definition at line 276 of file GC.cs.

◆ GetTotalMemory()

static long System.GC.GetTotalMemory ( bool  forceFullCollection)
static

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.

Parameters
forceFullCollectiontrue to indicate that this method can wait for garbage collection to occur before returning; otherwise, false.
Returns
A number that is the best available approximation of the number of bytes currently allocated in managed memory.

Definition at line 342 of file GC.cs.

◆ KeepAlive()

static void System.GC.KeepAlive ( object  obj)
static

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.

Parameters
objThe object to reference.

Definition at line 267 of file GC.cs.

◆ RegisterForFullGCNotification()

static void System.GC.RegisterForFullGCNotification ( int  maxGenerationThreshold,
int  largeObjectHeapThreshold 
)
static

Specifies that a garbage collection notification should be raised when conditions favor full garbage collection and when the collection has been completed.

Parameters
maxGenerationThresholdA number between 1 and 99 that specifies when the notification should be raised based on the objects allocated in generation 2.
largeObjectHeapThresholdA number between 1 and 99 that specifies when the notification should be raised based on objects allocated in the large object heap.
Exceptions
T:System.ArgumentOutOfRangeExceptionmaxGenerationThreshold or largeObjectHeapThreshold is not between 1 and 99.

Definition at line 383 of file GC.cs.

◆ RemoveMemoryPressure()

static void System.GC.RemoveMemoryPressure ( long  bytesAllocated)
static

Informs the runtime that unmanaged memory has been released and no longer needs to be taken into account when scheduling garbage collection.

Parameters
bytesAllocatedThe amount of unmanaged memory that has been released.
Exceptions
T:System.ArgumentOutOfRangeExceptionbytesAllocated is less than or equal to 0. -or- On a 32-bit computer, bytesAllocated is larger than F:System.Int32.MaxValue.

Definition at line 130 of file GC.cs.

◆ ReRegisterForFinalize()

static void System.GC.ReRegisterForFinalize ( object  obj)
static

Requests that the system call the finalizer for the specified object for which M:System.GC.SuppressFinalize(System.Object) has previously been called.

Parameters
objThe object that a finalizer must be called for.
Exceptions
T:System.ArgumentNullExceptionobj is null.

Definition at line 327 of file GC.cs.

◆ SuppressFinalize()

static void System.GC.SuppressFinalize ( object  obj)
static

Requests that the common language runtime not call the finalizer for the specified object.

Parameters
objThe object whose finalizer must not be executed.
Exceptions
T:System.ArgumentNullExceptionobj is null.

Definition at line 308 of file GC.cs.

◆ TryStartNoGCRegion() [1/4]

static bool System.GC.TryStartNoGCRegion ( long  totalSize)
static

Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available.

Parameters
totalSizeThe amount of memory in bytes to allocate without triggering a garbage collection. It must be less than or equal to the size of an ephemeral segment. For information on the size of an ephemeral segement, see the "Ephemeral generations and segments" section in the Fundamentals of Garbage Collection article.
Returns
true if the runtime was able to commit the required amount of memory and the garbage collector is able to enter no GC region latency mode; otherwise, false.
Exceptions
T:System.ArgumentOutOfRangeExceptiontotalSize exceeds the ephemeral segment size.
T:System.InvalidOperationExceptionThe process is already in no GC region latency mode.

Definition at line 480 of file GC.cs.

◆ TryStartNoGCRegion() [2/4]

static bool System.GC.TryStartNoGCRegion ( long  totalSize,
long  lohSize 
)
static

Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available for the large object heap and the small object heap.

Parameters
totalSizeThe amount of memory in bytes to allocate without triggering a garbage collection. totalSizelohSize must be less than or equal to the size of an ephemeral segment. For information on the size of an ephemeral segement, see the "Ephemeral generations and segments" section in the Fundamentals of Garbage Collection article.
lohSizeThe number of bytes in totalSize to use for large object heap (LOH) allocations.
Returns
true if the runtime was able to commit the required amount of memory and the garbage collector is able to enter no GC region latency mode; otherwise, false.
Exceptions
T:System.ArgumentOutOfRangeExceptiontotalSizelohSize exceeds the ephemeral segment size.
T:System.InvalidOperationExceptionThe process is already in no GC region latency mode.

Definition at line 494 of file GC.cs.

◆ TryStartNoGCRegion() [3/4]

static bool System.GC.TryStartNoGCRegion ( long  totalSize,
bool  disallowFullBlockingGC 
)
static

Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available, and controls whether the garbage collector does a full blocking garbage collection if not enough memory is initially available.

Parameters
totalSizeThe amount of memory in bytes to allocate without triggering a garbage collection. It must be less than or equal to the size of an ephemeral segment. For information on the size of an ephemeral segement, see the "Ephemeral generations and segments" section in the Fundamentals of Garbage Collection article.
disallowFullBlockingGCtrue to omit a full blocking garbage collection if the garbage collector is initially unable to allocate totalSize bytes; otherwise, false.
Returns
true if the runtime was able to commit the required amount of memory and the garbage collector is able to enter no GC region latency mode; otherwise, false.
Exceptions
T:System.ArgumentOutOfRangeExceptiontotalSize exceeds the ephemeral segment size.
T:System.InvalidOperationExceptionThe process is already in no GC region latency mode.

Definition at line 509 of file GC.cs.

◆ TryStartNoGCRegion() [4/4]

static bool System.GC.TryStartNoGCRegion ( long  totalSize,
long  lohSize,
bool  disallowFullBlockingGC 
)
static

Attempts to disallow garbage collection during the execution of a critical path if a specified amount of memory is available for the large object heap and the small object heap, and controls whether the garbage collector does a full blocking garbage collection if not enough memory is initially available.

Parameters
totalSizeThe amount of memory in bytes to allocate without triggering a garbage collection. totalSizelohSize must be less than or equal to the size of an ephemeral segment. For information on the size of an ephemeral segement, see the "Ephemeral generations and segments" section in the Fundamentals of Garbage Collection article.
lohSizeThe number of bytes in totalSize to use for large object heap (LOH) allocations.
disallowFullBlockingGCtrue to omit a full blocking garbage collection if the garbage collector is initially unable to allocate the specified memory on the small object heap (SOH) and LOH; otherwise, false.
Returns
true if the runtime was able to commit the required amount of memory and the garbage collector is able to enter no GC region latency mode; otherwise, false.
Exceptions
T:System.ArgumentOutOfRangeExceptiontotalSizelohSize exceeds the ephemeral segment size.
T:System.InvalidOperationExceptionThe process is already in no GC region latency mode.

Definition at line 525 of file GC.cs.

◆ WaitForFullGCApproach() [1/2]

static GCNotificationStatus System.GC.WaitForFullGCApproach ( )
static

Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

Returns
The status of the registered garbage collection notification.

Definition at line 413 of file GC.cs.

◆ WaitForFullGCApproach() [2/2]

static GCNotificationStatus System.GC.WaitForFullGCApproach ( int  millisecondsTimeout)
static

Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime is imminent.

Parameters
millisecondsTimeoutThe length of time to wait before a notification status can be obtained. Specify -1 to wait indefinitely.
Returns
The status of the registered garbage collection notification.
Exceptions
T:System.ArgumentOutOfRangeExceptionmillisecondsTimeout must be either non-negative or less than or equal to F:System.Int32.MaxValue or -1.

Definition at line 424 of file GC.cs.

◆ WaitForFullGCComplete() [1/2]

static GCNotificationStatus System.GC.WaitForFullGCComplete ( )
static

Returns the status of a registered notification for determining whether a full, blocking garbage collection by the common language runtime has completed.

Returns
The status of the registered garbage collection notification.

Definition at line 436 of file GC.cs.

◆ WaitForFullGCComplete() [2/2]

static GCNotificationStatus System.GC.WaitForFullGCComplete ( int  millisecondsTimeout)
static

Returns, in a specified time-out period, the status of a registered notification for determining whether a full, blocking garbage collection by common language the runtime has completed.

Parameters
millisecondsTimeoutThe length of time to wait before a notification status can be obtained. Specify -1 to wait indefinitely.
Returns
The status of the registered garbage collection notification.
Exceptions
T:System.InvalidOperationExceptionmillisecondsTimeout must be either non-negative or less than or equal to F:System.Int32.MaxValue or -1.

Definition at line 447 of file GC.cs.

◆ WaitForPendingFinalizers()

static void System.GC.WaitForPendingFinalizers ( )
static

Suspends the current thread until the thread that is processing the queue of finalizers has emptied that queue.

Definition at line 291 of file GC.cs.

Property Documentation

◆ MaxGeneration

int System.GC.MaxGeneration
staticget

Gets the maximum number of generations that the system currently supports.

Returns
A value that ranges from zero to the maximum number of supported generations.

Definition at line 33 of file GC.cs.


The documentation for this class was generated from the following file: