To reclaim objects, the garbage collector must stop all of an application's executing threads. In some situations, such as when an application retrieves data or displays content, a full garbage collection can occur at a critical time and impede performance. You can adjust the intrusiveness of the garbage collector by setting the GCSettings.LatencyMode property to one of the System.Runtime.GCLatencyMode values.
Latency refers to the time that the garbage collector intrudes in your application. During low latency periods, the garbage collector is more conservative and less intrusive in reclaiming objects. We recommend that you use the GCLatencyMode.LowLatency mode only for the short period of time when it is needed. Otherwise, if the system is under memory pressure, the garbage collector will trigger a collection, which can briefly pause the application and disrupt a time-critical operation.
During low latency periods, generation 2 collections are suppressed unless the following occurs:
The system receives a low memory notification from the operating system.
Your application code induces a collection by calling the GC.Collect method and specifying 2 for the generation parameter.
You should use the latency mode with applications that include a block of code that runs over a brief period of time and must run with minimal interruptions from the runtime. Although the LowLatency mode is designed to be used in scenarios where there are some time constraints, it is not intended to be a solution for scenarios where there are strict real-time constraints.
The following table lists the application scenarios that the GCLatencyMode values are suited for.
For applications that have no UI or server-side operations.
For most applications that have a UI.
For applications that have short-term, time-sensitive operations during which interruptions from the garbage collector could be disruptive. For example, applications that do animation rendering or data acquisition functions.
Default Garbage Collection Modes
If the LatencyMode property is not specified, the default mode is concurrent workstation garbage collection. The mode is dependent on the value of two runtime configuration settings:
If enabled, this setting specifies that the common language runtime runs workstation garbage collection on a separate thread to support concurrent operations. This setting is enabled by default.
If enabled, this setting specifies that the common language runtime runs server garbage collection; otherwise, it runs workstation garbage collection. You can enable server garbage collection only on computers with two or more processors. It is not enabled by default.
If this setting is enabled, <gcConcurrent> is automatically disabled.
The default values for GCLatencyMode are as follows:
Interactive when <gcConcurrent> is enabled and <gcServer> is disabled.
Batch when <gcConcurrent> is disabled, or <gcServer> is enabled.
Concurrent garbage collection is not supported in applications running the WOW64 x86 emulator on 64-bit systems that implement the Intel Itanium architecture (formerly called IA-64).
Guidelines for Using Low Latency
When you use LowLatency mode, consider the following guidelines:
Keep the period of time in low latency as short as possible.
Avoid allocating high amounts of memory during low latency periods. Low memory notifications can occur because garbage collection reclaims fewer objects.
While in the low latency mode, minimize the number of allocations you make, in particular allocations onto the Large Object Heap and pinned objects.
Be aware of threads that could be allocating. Because the LatencyMode property setting is process-wide, you could generate an OutOfMemoryException on any thread that may be allocating.
Wrap the low latency code in constrained execution regions (for more information, see Constrained Execution Regions).
You can force generation 2 collections during a low latency period by calling the GC.Collect(Int32, GCCollectionMode) method.
How to: Disable Concurrent Garbage Collection