被动回收Induced Collections

在大多数情况下,垃圾回收器可以确定执行回收的最佳时间,应让其独立运行。In most cases, the garbage collector can determine the best time to perform a collection, and you should let it run independently. 在某些不常见的情况下,强制回收可以提高应用程序的性能。There are rare situations when a forced collection might improve your application's performance. 在这种情况下,可以使用 GC.Collect 方法强制执行垃圾回收,从而诱导垃圾回收。In these cases, you can induce garbage collection by using the GC.Collect method to force a garbage collection.

如果应用代码中特定点使用的内存量大量减少,请使用 GC.Collect 方法。Use the GC.Collect method when there is a significant reduction in the amount of memory being used at a specific point in your application's code. 例如,如果应用使用包含多个控件的复杂对话框,那么在对话框关闭时调用 Collect 可以立即回收对话框占用的内存,从而提升性能。For example, if your application uses a complex dialog box that has several controls, calling Collect when the dialog box is closed could improve performance by immediately reclaiming the memory used by the dialog box. 请确保应用程序不会过于频繁地引发垃圾回收,否则当垃圾回收器无效率地尝试回收对象时,可能会使性能降低。Be sure that your application is not inducing garbage collection too frequently, because that can decrease performance if the garbage collector is trying to reclaim objects at non-optimal times. 可以向 Collect 方法提供 GCCollectionMode.Optimized 枚举值,以便仅在回收能够提高效率时才进行回收,如下一部分所述。You can supply a GCCollectionMode.Optimized enumeration value to the Collect method to collect only when collection would be productive, as discussed in the next section.

GC 回收模式GC collection mode

可以使用包含 GCCollectionMode 值的 GC.Collect 方法重载之一,指定强制回收的行为,如下所示。You can use one of the GC.Collect method overloads that includes a GCCollectionMode value to specify the behavior for a forced collection as follows.

GCCollectionModeGCCollectionMode value 说明Description
Default 对正在运行的 .NET 版本使用默认的垃圾回收设置。Uses the default garbage collection setting for the running version of .NET.
Forced 强制立即执行垃圾回收。Forces garbage collection to occur immediately. 这相当于调用 GC.Collect() 重载。This is equivalent to calling the GC.Collect() overload. 它会导致对所有分代进行完全阻塞回收。It results in a full blocking collection of all generations.

强制执行即时完全阻止式垃圾回收前,还可以将 GCSettings.LargeObjectHeapCompactionMode 属性设置为 GCLargeObjectHeapCompactionMode.CompactOnce,从而压缩大型对象堆。You can also compact the large object heap by setting the GCSettings.LargeObjectHeapCompactionMode property to GCLargeObjectHeapCompactionMode.CompactOnce before forcing an immediate full blocking garbage collection.
Optimized 使垃圾回收器可以确定当前时间是否是回收对象的最佳时间。Enables the garbage collector to determine whether the current time is optimal to reclaim objects.

垃圾回收器可能判定回收效率不够高,因此回收不合理,在这种情况下将返回而不回收对象。The garbage collector could determine that a collection would not be productive enough to be justified, in which case it will return without reclaiming objects.

后台回收或阻塞回收Background or blocking collections

可以调用 GC.Collect(Int32, GCCollectionMode, Boolean) 方法重载,指定诱导回收是否是阻止式。You can call the GC.Collect(Int32, GCCollectionMode, Boolean) method overload to specify whether an induced collection is blocking or not. 执行的回收类型取决于此方法的 modeblocking 参数组合。The type of collection performed depends on a combination of the method's mode and blocking parameters. modeGCCollectionMode 枚举的成员,且 blocking 值为 Booleanmode is a member of the GCCollectionMode enumeration, and blocking is a Boolean value. 下表汇总了 modeblocking 参数的交互。The following table summarizes the interaction of the mode and blocking arguments.

mode blocking = true blocking = false
ForcedDefaultForced or Default 尽快执行阻塞回收。A blocking collection is performed as soon as possible. 如果后台回收正在进行且分代为 0 或 1,Collect(Int32, GCCollectionMode, Boolean) 方法会立即触发阻止式回收,并在回收完成后返回结果。If a background collection is in progress and generation is 0 or 1, the Collect(Int32, GCCollectionMode, Boolean) method immediately triggers a blocking collection and returns when the collection is finished. 如果后台回收正在进行且 generation 参数为 2,此方法会等到后台回收完成,再触发第 2 代阻止式回收,然后返回结果。If a background collection is in progress and the generation parameter is 2, the method waits until the background collection is finished, triggers a blocking generation 2 collection, and then returns. 尽快执行回收。A collection is performed as soon as possible. Collect(Int32, GCCollectionMode, Boolean) 方法请求执行后台回收,但这并没有保证;阻止式回收仍可执行,具体视环境而定。The Collect(Int32, GCCollectionMode, Boolean) method requests a background collection, but this is not guaranteed; depending on the circumstances, a blocking collection may still be performed. 如果后台回收正在进行,该方法将立即返回。If a background collection is already in progress, the method returns immediately.
Optimized 可能会执行阻止式回收,具体视垃圾回收器的状态和 generation 参数而定。A blocking collection may be performed, depending on the state of the garbage collector and the generation parameter. 垃圾回收器会尽量提供最佳性能。The garbage collector tries to provide optimal performance. 根据垃圾回收器的状态,有时可执行回收。A collection may be performed, depending on the state of the garbage collector. Collect(Int32, GCCollectionMode, Boolean) 方法请求执行后台回收,但这并没有保证;阻止式回收仍可执行,具体视环境而定。The Collect(Int32, GCCollectionMode, Boolean) method requests a background collection, but this is not guaranteed; depending on the circumstances, a blocking collection may still be performed. 垃圾回收器会尽量提供最佳性能。The garbage collector tries to provide optimal performance. 如果后台回收正在进行,该方法将立即返回。If a background collection is already in progress, the method returns immediately.

请参阅See also