C# 处理应用程序减少内存占用

系统启动起来以后,内存占用越来越大,使用析构函数、GC.Collect什么的也不见效果,后来查了好久,找到了个办法,就是使用 SetProcessWorkingSetSize函数。这个函数是Windows API 函数。下面是使用的方法:

复制代码
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet =
System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);

public void Dispose()
{
    GC.Collect();
    GC.SuppressFinalize(this);

    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
    {
        SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1-1);
    }
}
复制代码

使用这个函数也会有些问题,具体的可以参见:

http://hi.baidu.com/taobaoshoping/blog/item/a1f6baf52d523a21bd3109f5.html

本文章转载自 http://www.cnblogs.com/pdfw/archive/2009/04/22/1441477.html 

原文地址:https://www.cnblogs.com/qiyecao/p/4011104.html