定时整理内存

 public class FlushMemUtil
    {
         
        public static void StartTimer()
        {
            #region 定时整理内存 

            var flushTimer = new Timer(FlushMemory);
            flushTimer.Change(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(30));
            #endregion
        }

        #region   定时刷新内存   
        /// <summary>
        /// 刷新内存
        /// </summary>
        static void FlushMemory(object sender = null)
        {
            try
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    Win32API.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteErrLog(e.Message, e);
            }

        }
        #endregion

    }

  

  [DllImport("kernel32.dll")]
        internal static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);

  

原文地址:https://www.cnblogs.com/robertyao/p/14788939.html