GC::TryStartNoGCRegion Method (Int64, Int64)
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.
Assembly: mscorlib (in mscorlib.dll)
public: [SecurityCriticalAttribute] static bool TryStartNoGCRegion( long long totalSize, long long lohSize )
Parameters
- totalSize
-
Type:
System::Int64
The amount of memory in bytes to allocate without triggering a garbage collection. totalSize –lohSize 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.
- lohSize
-
Type:
System::Int64
The number of bytes in totalSize to use for large object heap (LOH) allocations.
Return Value
Type: System::Booleantrue 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.
| Exception | Condition |
|---|---|
| ArgumentOutOfRangeException | totalSize – lohSize exceeds the ephemeral segment size. |
| InvalidOperationException | The process is already in no GC region latency mode. |
The TryStartNoGCRegion(Int64, Int64) method attempts to place the garbage collector in no GC region latency mode, which disallows garbage collection while an app executes a critical region of code. If the runtime is unable to initially allocate the requested amount of memory, the garbage collector performs a full blocking garbage collection in an attempt to free additional memory. The garbage collector enters no GC region latency mode if it is able to allocate lohSize for the LOH and totalSize – lohSize for the small object heap (SOH).
lohSize must be large enough to handle all memory allocations that occur in the critical path for the LOH, and totalSize – lohSize must be large enough to handle all memory allocations that occur in the critical path for the SOH. This includes allocations by the app, as well as allocations that the runtime makes on the app's behalf.
Important |
|---|
You cannot nest calls to the TryStartNoGCRegion method, and you should only call the EndNoGCRegion method if the runtime is currently in no GC region latency mode. In other words, you should not call TryStartNoGCRegion multiple times (after the first method call, subsequent calls will not succeed), and you should not expect calls to EndNoGCRegion to succeed just because the first call to TryStartNoGCRegion succeeded. |
You exit the no GC region latency mode by calling the EndNoGCRegion method.
Available since 4.6
