如何當即回收內存C#

C#如何當即回收內存

1.把對象賦值爲nullthis

2.當即調用GC.Collect();
 
注意:這個也只是強制垃圾回收器去回收,但具體何時執行不肯定。 
 
代碼:
 
 
[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);

 #region 內存回收
   public  void ClearMemory()
 {
     GC.Collect();
     GC.SuppressFinalize(this);
 
 
     if (Environment.OSVersion.Platform == PlatformID.Win32NT)
     {
         SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
     }
 }
 #endregion

ClearMemory();

 

 

https://blog.csdn.net/xwnxwn/article/details/78009071spa

相關文章
相關標籤/搜索