Udostępnij za pośrednictwem


Latency Modes

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 LatencyMode property to one of the 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. Generation 2 collections occur less frequently, which causes the application working set to grow over time. As a result, it is recommended that you use the 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.

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.

Latency mode

Application scenarios

Batch

For applications that have no UI or server-side operations. 

 

Interactive

For most applications that have a UI. 

LowLatency

For applications that have short-term, time-sensitive operations when 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:

  • <gcConcurrent>

    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.

  • <gcServer>

    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 when both <gcConcurrent> and <gcServer> are enabled.

Note

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.

See Also

Tasks

How to: Disable Concurrent Garbage Collection

Concepts

Induced Collections

Other Resources

Garbage Collection