GC.RemoveMemoryPressure(Int64) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Informs the runtime that unmanaged memory has been released and no longer needs to be taken into account when scheduling garbage collection.
public:
static void RemoveMemoryPressure(long bytesAllocated);
[System.Security.SecurityCritical]
public static void RemoveMemoryPressure (long bytesAllocated);
public static void RemoveMemoryPressure (long bytesAllocated);
[<System.Security.SecurityCritical>]
static member RemoveMemoryPressure : int64 -> unit
static member RemoveMemoryPressure : int64 -> unit
Public Shared Sub RemoveMemoryPressure (bytesAllocated As Long)
Parameters
- bytesAllocated
- Int64
The amount of unmanaged memory that has been released.
- Attributes
Exceptions
bytesAllocated
is less than or equal to 0.
-or-
On a 32-bit computer, bytesAllocated
is larger than Int32.MaxValue.
Remarks
The common pattern for releasing native resources is via a type's finalizer. If a managed object uses native memory, it can free that native memory in its finalizer. The garbage collector only knows about managed memory and schedules collections based on this knowledge. Imagine a scenario where a small managed object is associated with a large amount of native memory usage, and this managed object now lives in gen2. A gen2 GC might not happen for some time, which means the large amount of native memory won't be released until the next gen2 happens. The runtime provides the AddMemoryPressure and RemoveMemoryPressure methods to help with this scenario. The runtime keeps an internal record of how much memory pressure these APIs added and removed, and triggers a gen2 GC if deemed productive. So this is not a feature of the GC but rather something that the runtime provides to trigger GCs.
If you have a convenient place to call these APIs, you don't necessarily have to use a finalizer. For example, if you know you can release the native memory when a specific method on the type is called, you can call the RemoveMemoryPressure method at that point instead of having a finalizer.
Caution
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.