滞后时间模式Latency Modes

若要回收对象,垃圾回收器必须停止应用程序中所有正在执行的线程。To reclaim objects, the garbage collector must stop all the executing threads in an application. 在某些情况下(例如当应用程序检索数据或显示内容时),关键时刻可能发生完整的垃圾回收,从而妨碍性能。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. 可以通过将 GCSettings.LatencyMode 属性设置为其中一个 System.Runtime.GCLatencyMode 值来调节垃圾回收的干扰。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. System.Runtime.GCLatencyMode 枚举提供两种低延迟设置:The System.Runtime.GCLatencyMode enumeration provides two low latency settings:

  • LowLatency 禁止第 2 代集合,仅执行第 0 代和第 1 代集合。LowLatency suppresses generation 2 collections and performs only generation 0 and 1 collections. 只能在短时间内使用。It can be used only for short periods of time. 在更长时间内,如果系统处于内存压力下,垃圾回收器将触发一次回收,这样会暂时暂停应用程序并中断对时间要求很急的操作。Over longer periods, 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. 此设置仅对工作站垃圾回收可用。This setting is available only for workstation garbage collection.

  • SustainedLowLatency 禁止前台第 2 代回收,仅执行第 0 代、第 1 代和后台第 2 代回收。SustainedLowLatency suppresses foreground generation 2 collections and performs only generation 0, 1, and background generation 2 collections. 它可以长时间使用,并对工作站和服务器垃圾回收都可用。It can be used for longer periods of time, and is available for both workstation and server garbage collection. 如果并发垃圾回收已禁用,则无法使用此设置。This setting cannot be used if concurrent garbage collection is disabled.

在低延迟期间,除非发生以下情况,否则禁止第 2 代回收:During low latency periods, generation 2 collections are suppressed unless the following occurs:

  • 系统收到操作系统的低内存通知。The system receives a low memory notification from the operating system.

  • 应用程序代码通过调用 GC.Collect 方法并将 generation 参数指定为 2 来包含回收。Your application code induces a collection by calling the GC.Collect method and specifying 2 for the generation parameter.

下表列出了使用 GCLatencyMode 值的应用程序方案。The following table lists the application scenarios for using the GCLatencyMode values.

延迟模式Latency mode 应用程序方案Application scenarios
Batch 对于不具有 UI 或服务器端操作的应用程序。For applications that have no UI or server-side operations.

这是并发垃圾回收被禁用时的默认模式。This is the default mode when concurrent garbage collection is disabled.
Interactive 对于具有 UI 的大多数应用程序。For most applications that have a UI.

并发垃圾回收启用时,这是默认模式。This is the default mode when concurrent garbage collection is enabled.
LowLatency 对于具有短期时效性操作(操作期间垃圾回收器的干扰可能会引起中断)的应用程序。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.
SustainedLowLatency 适用于在有限但有可能更长的时间内具有时效性操作并且在此期间垃圾回收器中断具有破环性的应用程序。For applications that have time-sensitive operations for a contained but potentially longer duration of time during which interruptions from the garbage collector could be disruptive. 例如,需要随着交易时间内的市场数据变化做出快速响应的应用程序。For example, applications that need quick response times as market data changes during trading hours.

此模式会比其他模式产生更大的托管堆大小。This mode results in a larger managed heap size than other modes. 由于它不压缩托管堆,因此可能产生更多碎片。Because it does not compact the managed heap, higher fragmentation is possible. 确保有足够的可用内存。Ensure that sufficient memory is available.

低延迟使用指南Guidelines for Using Low Latency

使用 LowLatency 模式时,请参考以下指南: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. 由于 LatencyMode 属性设置属于进程范围的设置,因此可以在可分配的任何线程上生成 OutOfMemoryExceptionBecause 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).

  • 在低延迟期间,可以通过调用 GC.Collect(Int32, GCCollectionMode) 方法强制进行第 2 代回收。You can force generation 2 collections during a low latency period by calling the GC.Collect(Int32, GCCollectionMode) method.

请参阅See also