首页 > 代码库 > .net C# 释放内存 例子

.net C# 释放内存 例子

namespace myCommon
{
    public class SysVar
    {

        [DllImport("kernel32.dll")]
        public static extern bool SetProcessWorkingSetSize(IntPtr handle, int minimumWorkingSetSize, int maximumWorkingSetSize);

        /// <summary>
        /// 释放内存
        /// </summary>
        public static void FlushMemory()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
            }
        }
    }

}

//使用例子:

        private void xfrmBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            myCommon.SysVar.FlushMemory();
        }