当前位置:编程学习 > C#/ASP.NET >>

让.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以下
    
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,