[DllImport("psapi.dll")]
private static extern int EmptyWorkingSet(IntPtr hwProc);
/// <summary>
/// 释放内存
/// </summary>
public static void ClearMemory()
{
GC.Collect();
GC.WaitForPendingFinalizers();
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
//对于系统进程会拒绝访问,导致出错,此处对异常不进行处理。
try
{
Console.WriteLine("清理内存:" + process.ProcessName);
EmptyWorkingSet(process.Handle);
}
catch(Exception ex)
{
Console.WriteLine(process.ProcessName + " 异常:" + ex.Message);
}
}
}
05-12
05-12
2258
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-29
04-06
4811
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)