10 [__DynamicallyInvokable]
11 public static class GC 13 private enum StartNoGCRegionStatus
21 private enum EndNoGCRegionStatus
31 [__DynamicallyInvokable]
34 [SecuritySafeCritical]
35 [__DynamicallyInvokable]
38 return GetMaxGeneration();
44 internal static extern int GetGCLatencyMode();
48 internal static extern int SetGCLatencyMode(
int newLatencyMode);
52 [SuppressUnmanagedCodeSecurity]
53 internal static extern int _StartNoGCRegion(
long totalSize,
bool lohSizeKnown,
long lohSize,
bool disallowFullBlockingGC);
57 [SuppressUnmanagedCodeSecurity]
58 internal static extern int _EndNoGCRegion();
62 internal static extern int GetLOHCompactionMode();
66 internal static extern void SetLOHCompactionMode(
int newLOHCompactionyMode);
70 private static extern int GetGenerationWR(
IntPtr handle);
74 [SuppressUnmanagedCodeSecurity]
75 private static extern long GetTotalMemory();
79 [SuppressUnmanagedCodeSecurity]
80 private static extern void _Collect(
int generation,
int mode);
84 private static extern int GetMaxGeneration();
88 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
89 private static extern int _CollectionCount(
int generation,
int getSpecialGCCount);
93 internal static extern bool IsServerGC();
97 [SuppressUnmanagedCodeSecurity]
98 private static extern void _AddMemoryPressure(ulong bytesAllocated);
102 [SuppressUnmanagedCodeSecurity]
103 private static extern void _RemoveMemoryPressure(ulong bytesAllocated);
110 [__DynamicallyInvokable]
113 if (bytesAllocated <= 0)
117 if (4 ==
IntPtr.
Size && bytesAllocated >
int.MaxValue)
121 _AddMemoryPressure((ulong)bytesAllocated);
129 [__DynamicallyInvokable]
132 if (bytesAllocated <= 0)
136 if (4 ==
IntPtr.
Size && bytesAllocated >
int.MaxValue)
140 _RemoveMemoryPressure((ulong)bytesAllocated);
147 [SecuritySafeCritical]
154 [__DynamicallyInvokable]
161 [SecuritySafeCritical]
162 [__DynamicallyInvokable]
174 [SecuritySafeCritical]
175 [__DynamicallyInvokable]
178 Collect(generation, mode, blocking:
true);
189 [SecuritySafeCritical]
190 [__DynamicallyInvokable]
193 Collect(generation, mode, blocking, compacting:
false);
203 [SecuritySafeCritical]
227 else if (!compacting)
231 _Collect(generation, num);
239 [SecuritySafeCritical]
240 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
241 [__DynamicallyInvokable]
248 return _CollectionCount(generation, 0);
251 [SecuritySafeCritical]
252 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
253 internal static int CollectionCount(
int generation,
bool getSpecialGCCount)
259 return _CollectionCount(generation, getSpecialGCCount ? 1 : 0);
265 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
266 [__DynamicallyInvokable]
275 [SecuritySafeCritical]
278 int generationWR = GetGenerationWR(wo.m_handle);
285 [SuppressUnmanagedCodeSecurity]
286 private static extern void _WaitForPendingFinalizers();
289 [SecuritySafeCritical]
290 [__DynamicallyInvokable]
293 _WaitForPendingFinalizers();
298 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
299 private static extern void _SuppressFinalize(
object o);
305 [SecuritySafeCritical]
306 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
307 [__DynamicallyInvokable]
314 _SuppressFinalize(obj);
319 private static extern void _ReRegisterForFinalize(
object o);
325 [SecuritySafeCritical]
326 [__DynamicallyInvokable]
333 _ReRegisterForFinalize(obj);
340 [SecuritySafeCritical]
341 [__DynamicallyInvokable]
344 long totalMemory = GetTotalMemory();
345 if (!forceFullCollection)
350 long num2 = totalMemory;
357 num2 = GetTotalMemory();
358 num3 = (float)(num2 - totalMemory) / (float)totalMemory;
360 while (num-- > 0 && (!(-0.05 < (
double)num3) || !((double)num3 < 0.05)));
366 private static extern bool _RegisterForFullGCNotification(
int maxGenerationPercentage,
int largeObjectHeapPercentage);
369 private static extern bool _CancelFullGCNotification();
372 private static extern int _WaitForFullGCApproach(
int millisecondsTimeout);
375 private static extern int _WaitForFullGCComplete(
int millisecondsTimeout);
385 if (maxGenerationThreshold <= 0 || maxGenerationThreshold >= 100)
389 if (largeObjectHeapThreshold <= 0 || largeObjectHeapThreshold >= 100)
393 if (!_RegisterForFullGCNotification(maxGenerationThreshold, largeObjectHeapThreshold))
404 if (!_CancelFullGCNotification())
426 if (millisecondsTimeout < -1)
449 if (millisecondsTimeout < -1)
457 private static bool StartNoGCRegionWorker(
long totalSize,
bool hasLohSize,
long lohSize,
bool disallowFullBlockingGC)
459 switch (_StartNoGCRegion(totalSize, hasLohSize, lohSize, disallowFullBlockingGC))
462 throw new ArgumentOutOfRangeException(
"totalSize",
"totalSize is too large. For more information about setting the maximum size, see \"Latency Modes\" in http://go.microsoft.com/fwlink/?LinkId=522706");
482 return StartNoGCRegionWorker(totalSize, hasLohSize:
false, 0
L, disallowFullBlockingGC:
false);
496 return StartNoGCRegionWorker(totalSize, hasLohSize:
true, lohSize, disallowFullBlockingGC:
false);
511 return StartNoGCRegionWorker(totalSize, hasLohSize:
false, 0
L, disallowFullBlockingGC);
527 return StartNoGCRegionWorker(totalSize, hasLohSize:
true, lohSize, disallowFullBlockingGC);
531 private static EndNoGCRegionStatus EndNoGCRegionWorker()
533 switch (_EndNoGCRegion())
542 return EndNoGCRegionStatus.Succeeded;
551 EndNoGCRegionWorker();
GCCollectionMode
Specifies the behavior for a forced garbage collection.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
static void EndNoGCRegion()
Ends the no GC region latency mode.
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
static long GetTotalMemory(bool forceFullCollection)
Retrieves the number of bytes currently thought to be allocated. A parameter indicates whether this m...
static GCNotificationStatus WaitForFullGCApproach()
Returns the status of a registered notification for determining whether a full, blocking garbage coll...
static void ReRegisterForFinalize(object obj)
Requests that the system call the finalizer for the specified object for which M:System....
static void Collect(int generation)
Forces an immediate garbage collection from generation 0 through a specified generation.
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 ...
The exception that is thrown when the value of an argument is outside the allowable range of values a...
static int GetGeneration(object obj)
Returns the current generation number of the specified object.
static void KeepAlive(object obj)
References the specified object, which makes it ineligible for garbage collection from the start of t...
static bool TryStartNoGCRegion(long totalSize, long lohSize)
Attempts to disallow garbage collection during the execution of a critical path if a specified amount...
Cer
Specifies a method's behavior when called within a constrained execution region.
Represents a weak reference, which references an object while still allowing that object to be reclai...
static void RegisterForFullGCNotification(int maxGenerationThreshold, int largeObjectHeapThreshold)
Specifies that a garbage collection notification should be raised when conditions favor full garbage ...
Provides information about, and means to manipulate, the current environment and platform....
static GCNotificationStatus WaitForFullGCComplete(int millisecondsTimeout)
Returns, in a specified time-out period, the status of a registered notification for determining whet...
static void CancelFullGCNotification()
Cancels the registration of a garbage collection notification.
static void Collect(int generation, GCCollectionMode mode)
Forces a garbage collection from generation 0 through a specified generation, at a time specified by ...
static GCNotificationStatus WaitForFullGCComplete()
Returns the status of a registered notification for determining whether a full, blocking garbage coll...
static bool TryStartNoGCRegion(long totalSize, bool disallowFullBlockingGC)
Attempts to disallow garbage collection during the execution of a critical path if a specified amount...
A platform-specific type that is used to represent a pointer or a handle.
static int GetGeneration(WeakReference wo)
Returns the current generation number of the target of a specified weak reference.
static GCNotificationStatus WaitForFullGCApproach(int millisecondsTimeout)
Returns, in a specified time-out period, the status of a registered notification for determining whet...
static void Collect()
Forces an immediate garbage collection of all generations.
MethodImplOptions
Defines the details of how a method is implemented.
CharSet
Dictates which character set marshaled strings should use.
GCNotificationStatus
Provides information about the current registration for notification of the next full garbage collect...
Controls the system garbage collector, a service that automatically reclaims unused memory.
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
static int Size
Gets the size of this instance.
The notification was successful and the registration was not canceled.
static int MaxGeneration
Gets the maximum number of generations that the system currently supports.
static int CollectionCount(int generation)
Returns the number of times garbage collection has occurred for the specified generation of objects.
The exception that is thrown when a method call is invalid for the object's current state.
Consistency
Specifies a reliability contract.
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...
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 ...
Provides information about a specific culture (called a locale for unmanaged code development)....
static void AddMemoryPressure(long bytesAllocated)
Informs the runtime of a large allocation of unmanaged memory that should be taken into account when ...
static bool TryStartNoGCRegion(long totalSize)
Attempts to disallow garbage collection during the execution of a critical path if a specified amount...
static void WaitForPendingFinalizers()
Suspends the current thread until the thread that is processing the queue of finalizers has emptied t...
static void RemoveMemoryPressure(long bytesAllocated)
Informs the runtime that unmanaged memory has been released and no longer needs to be taken into acco...