MemoryManagement.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace OTSIncAReportApp
  7. {
  8. public static class MemoryManagementClass
  9. {
  10. /// <summary>
  11. /// 回收内存
  12. /// </summary>
  13. public static void FlushMemory()
  14. {
  15. //回收内存
  16. MemoryManagement mm = new MemoryManagement();
  17. mm.FlushMemory();
  18. }
  19. [System.Runtime.InteropServices.DllImport("coredll.dll")]
  20. [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
  21. public static extern bool DeleteObject(IntPtr hgdiobj);
  22. public class MemoryManagement
  23. {
  24. [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  25. public static extern bool SetProcessWorkingSetSize(IntPtr proc, int min, int max);
  26. public void FlushMemory()
  27. {
  28. GC.Collect();
  29. GC.WaitForPendingFinalizers();
  30. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  31. { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); }
  32. }
  33. }
  34. }
  35. }