winform,WPF 释放内存垃圾,减少资源占用方法

 [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
        public void FlushMemory()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
            }
        }

 

用法:只需要在你希望释放的时候调用FlushMemory()即可..

http://xuxuhuy.blog.163.com/blog/static/1032932920116110204080/

posted @ 2018-02-27 09:22  zhaogaojian  阅读(968)  评论(0编辑  收藏  举报