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 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM