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);
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 大于 MaxValueOn 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.

在最简单的用法模式下,托管对象在构造函数中分配非托管内存,并在 DisposeFinalize 方法中释放它。In the simplest usage pattern, a managed object allocates unmanaged memory in the constructor and releases it in the Dispose or Finalize method. 在分配非托管内存后调用 AddMemoryPressure 方法,并在释放后调用 RemoveMemoryPressure 方法。Call the AddMemoryPressure method after allocating the unmanaged memory, and call the RemoveMemoryPressure method after releasing it.

在更复杂的情况下,如果非托管内存分配在托管对象的生存期内发生了重大更改,则可以调用 AddMemoryPressureRemoveMemoryPressure 方法将这些增量更改传递给运行时。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.

安全性

SecurityPermission
用于在操作垃圾回收优先级时调用非托管代码的能力。for the ability to call unmanaged code when manipulating garbage-collection priority. 关联的枚举: UnmanagedCodeAssociated enumeration: UnmanagedCode

SecurityCriticalAttribute
需要对直接调用方的完全信任。requires full trust for the immediate caller. 部分受信任的或透明的代码不能使用此成员。This member cannot be used by partially trusted or transparent code.

适用于