GC.AddMemoryPressure(Int64) 方法
定义
通知运行时在安排垃圾回收时应考虑分配大量的非托管内存。Informs the runtime of a large allocation of unmanaged memory that should be taken into account when scheduling garbage collection.
public:
static void AddMemoryPressure(long bytesAllocated);
[System.Security.SecurityCritical]
public static void AddMemoryPressure (long bytesAllocated);
public static void AddMemoryPressure (long bytesAllocated);
[<System.Security.SecurityCritical>]
static member AddMemoryPressure : int64 -> unit
static member AddMemoryPressure : int64 -> unit
Public Shared Sub AddMemoryPressure (bytesAllocated As Long)
参数
- bytesAllocated
- Int64
已分配的非托管内存的增量。The incremental amount of unmanaged memory that has been allocated.
- 属性
例外
bytesAllocated 小于或等于 0。bytesAllocated is less than or equal to 0.
或-or-
在 32 位的计算机上,bytesAllocated 大于 MaxValue。On a 32-bit computer, bytesAllocated is larger than MaxValue.
注解
在确定何时计划垃圾回收时,运行时将考虑分配多少托管内存。In determining when to schedule garbage collection, the runtime takes into account how much managed memory is allocated. 如果某个小型托管对象分配了大量非托管内存,则运行时将仅考虑托管内存,从而低估计划垃圾回收的紧急性。If a small managed object allocates a large amount of unmanaged memory, the runtime takes into account only the managed memory, and thus underestimates the urgency of scheduling garbage collection. AddMemoryPressure方法将此额外压力通知给运行时系统内存。The AddMemoryPressure method informs the runtime of this additional pressure on system memory.
AddMemoryPressure和 RemoveMemoryPressure 方法只为仅依赖于终结器释放非托管资源的类型的类型提高了性能。The AddMemoryPressure and RemoveMemoryPressure methods improve performance only for types that exclusively depend on finalizers to release the unmanaged resources. 不需要在遵循 dispose 模式的类型中使用这些方法,其中,使用终结器仅在类型的使用者忘记调用的事件中清理非托管资源 Dispose 。It's not necessary to use these methods in types that follow the dispose pattern, where finalizers are used to clean up unmanaged resources only in the event that a consumer of the type forgets to call Dispose. 有关对象终止和释放模式的详细信息,请参阅 清理非托管资源。For more information on object finalization and the dispose pattern, see Cleaning Up Unmanaged Resources.
在最简单的用法模式下,托管对象在构造函数中分配非托管内存,并在方法中释放它 Finalize 。In the simplest usage pattern, a managed object allocates unmanaged memory in the constructor and releases it in the Finalize method. AddMemoryPressure在分配非托管内存后调用方法,并在 RemoveMemoryPressure 释放后调用方法。Call the AddMemoryPressure method after allocating the unmanaged memory, and call the RemoveMemoryPressure method after releasing it.
在更复杂的情况下,如果非托管内存分配在托管对象的生存期内发生了重大更改,则可以调用 AddMemoryPressure 和 RemoveMemoryPressure 方法将这些增量更改传递给运行时。In more complicated scenarios, where the unmanaged memory allocation changes substantially during the lifetime of the managed object, you can call the AddMemoryPressure and RemoveMemoryPressure methods to communicate these incremental changes to the runtime.
注意
您必须确保完全删除所添加的压力。You must ensure that you remove exactly the amount of pressure you add. 否则,可能会对长时间运行的应用程序中系统的性能产生负面影响。Failing to do so can adversely affect the performance of the system in applications that run for long periods of time.