GC.AddMemoryPressure(Int64) 方法

定義

告知執行階段,在排定記憶體回收時應考慮的大量 Unmanaged 記憶體配置。

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

已配置的 Unmanaged 記憶體累加數量。

屬性

例外狀況

bytesAllocated 小於或等於 0。

-或-

在 32 位電腦上, bytesAllocated 大於 Int32.MaxValue

備註

在判斷何時排程垃圾收集時,執行時間會考慮配置多少受控記憶體。 如果小型 Managed 物件配置大量的 Unmanaged 記憶體,執行時間只會考慮 Managed 記憶體,因此會破壞排程垃圾收集的緊急性。 方法 AddMemoryPressure 會通知執行時間系統記憶體的額外壓力。

AddMemoryPressureRemoveMemoryPressure 方法只會針對獨佔相依于完成項來釋放 Unmanaged 資源的型別改善效能。 不需要在遵循處置模式的型別中使用這些方法,其中完成項只會在類型取用者忘記呼叫 Dispose 時清除 Unmanaged 資源。 如需物件最終化和處置模式的詳細資訊,請參閱 清除 Unmanaged 資源

在最簡單的使用模式中,Managed 物件會在建構函式中配置 Unmanaged 記憶體,並在 方法中 Finalize 釋放它。 AddMemoryPressure在配置 Unmanaged 記憶體之後呼叫 方法,並在釋放它之後呼叫 RemoveMemoryPressure 方法。

在更複雜的案例中,在 Managed 物件的存留期內,Unmanaged 記憶體配置會大幅變更,您可以呼叫 AddMemoryPressureRemoveMemoryPressure 方法來將這些累加變更傳達給執行時間。

警告

您必須確定您完全移除您新增的壓力量。 無法這麼做可能會對長時間執行之應用程式中系統的效能造成負面影響。

適用於