讓.NET程序快速釋放內存的辦法
[DllImport("kernel32.dll")]
public static extern bool SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
public static void GarbageCollect()
{
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
public static void FlushMemory()
{
GarbageCollect();
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
Win32.SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
}
}
NET裡面還有一個類可以達到這個效果
System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet = new System.IntPtr(5);
采用定時釋放+窗體打開的時候,效果很好,估計程序占的內存會在20以下