Free Memory : Memory « Windows « C# / CSharp Tutorial






using System;
using System.IO;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

public class MainClass
{
    public static void Main()
    {
        IntPtr ptr = Marshal.AllocHGlobal(1024);
        GC.AddMemoryPressure(1024);
        if (ptr != IntPtr.Zero) {
            Marshal.FreeHGlobal(ptr);
            ptr = IntPtr.Zero;
            GC.RemoveMemoryPressure(1024);
        }
    }
}








29.13.Memory
29.13.1.Allocate memory with System.Runtime.MemoryFailPoint
29.13.2.Allocate and free memory
29.13.3.Demonstrate stackalloc
29.13.4.Free Memory
29.13.5.Free memory: Marshal.FreeHGlobal
29.13.6.Create Heap and destroy Heap