C#内存回收

 #region 内存回收
        [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
        public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
        ///  
        /// 释放内存
        ///  
        public static void ClearMemory()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
            }
        }
        #endregion

 

using System.Runtime.InteropServices; //可以用DllImport了

 

在你认为合适的地方调用ClearMemory()即可。

转载于:https://www.cnblogs.com/ChaunceyWan/p/11375542.html

你可能感兴趣的:(C#内存回收)