C#_.Net程序减少内存占用的方法(SetProcessWorkingSetSize)

在Win32 API中有一个函数,MSDN官方解释:Sets the minimum and maximum working set sizes for the specified process.,中文解释大概是:设置操作系统实际划分给进程使用的内存容量。

通过下面的方法可以有效的减少程序的内存占用,当然这里的“有效”是存在假象的,具体的请参见代码后的网址,其已经讲的很清楚了,这里就偷懒了。

(Copy Code)

[DllImport("kernel32.dll")]
public static extern bool SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);

public void FlushMemory()
{
    GC.Collect();
    GC.WaitForPendingFinalizers();
    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
    {
        SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
    }
}

关于SetProcessWorkingSetSize这个函数,详见:http://hi.baidu.com/taobaoshoping/blog/item/a1f6baf52d523a21bd3109f5.html

posted @ 2010-09-12 16:55  小 .xin  阅读(765)  评论(0编辑  收藏  举报
回到页首