Bootstrap

C# WinForm应用程序降低系统内存占用

C# WinForm应用程序降低系统内存占用


定期清理执行垃圾回收代码:


//在程序中用一个计时器,每隔几秒钟调用一次该函数,打开任务管理器,你会有惊奇的发现
#region 内存回收
[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
/// <summary>
/// 释放内存
/// </summary>
public static void ClearRAM()
{
	GC.Collect();
	GC.WaitForPendingFinalizers();
	if (Environment.OSVersion.Platform == PlatformID.Win32NT)
	{
		SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
	}
}
#endregion

;