GC::TryStartNoGCRegion Method (Int64, Boolean)

.NET Framework (current version)
 

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.

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

public:
[SecurityCriticalAttribute]
static bool TryStartNoGCRegion(
	long long totalSize,
	bool disallowFullBlockingGC
)

Parameters

totalSize
Type: System::Int64

The 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.

disallowFullBlockingGC
Type: System::Boolean

true to omit a full blocking garbage collection if the garbage collector is initially unable to allocate totalSize bytes; otherwise, false.

Return Value

Type: System::Boolean

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.

Exception Condition
ArgumentOutOfRangeException

totalSize exceeds the ephemeral segment size.

InvalidOperationException

The process is already in no GC region latency mode.

The TryStartNoGCRegion(Int64, Boolean) 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 and the disallowFullBlockingGC argument is false, the garbage collector performs a full blocking garbage collection in an attempt to free additional memory; otherwise, the allocation fails, and the method returns false. The garbage collector enters no GC region latency mode if it is able to allocate the required amount of memory, which in this case is actually 2 * totalSize (it attempts to allocate totalSize for the small object heap and totalSize for the large object heap).

totalSize must be large enough to handle all memory allocations that occur in the critical path. This includes allocations by the app, as well as allocations that the runtime makes on the app's behalf.

Setting disallowFullBlockingGC to true to prevent a full blocking garbage collection if not enough memory is initially available is most useful in load balancing scenarios: one system can call this method and report itself as ready to accept requests if it returns true, and have the load balancer redirect requests to other systems if it returns false. It can then do a full blocking garbage collection when it's not handling requests by calling the Collect(Int32, GCCollectionMode, Boolean, Boolean) method.

System_CAPS_importantImportant

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.

.NET Framework
Available since 4.6
Return to top
Show: