Bootstrap

C# 程序,内存会被占满的问题,尝试清理下内存吧

引用个动态库

        [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize", ExactSpelling = true, CharSet =
System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
        private static extern int SetProcessWorkingSetSize(IntPtr process, int minimumWorkingSetSize, int maximumWorkingSetSize);

建个循环的线程,半小时清理一次

 new Thread(() =>
 {
     while (true)
     {
         Thread.Sleep(30*1000);

         var index = GC.GetGeneration(AppDomain.CurrentDomain);
         GC.Collect(index);
         GC.SuppressFinalize(AppDomain.CurrentDomain);

         if (Environment.OSVersion.Platform == PlatformID.Win32NT)
         {
             SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
         }
         Thread.Sleep(30 * 60 * 1000);
     }
 })
 { IsBackground = true }.Start();

初始占用了500M左右,清理完就剩50M了

;