让.NET程序快速释放内存的办法

让.NET程序快速释放内存的办法
[DllImport("kernel32.dll")]
        public static extern bool SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);

public static void GarbageCollect()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        public static void FlushMemory()
        {
            GarbageCollect();

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



NET里面还有一个类可以达到这个效果
   System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet = new System.IntPtr(5);

采用定时释放+窗体打开的时候,效果很好,估计程序占的内存会在20以下
posted @ 2010-08-12 12:00  路灯下的诗人  阅读(774)  评论(0编辑  收藏  举报