SetProcessWorkingSetSize減小內存佔用

[DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);orm

/// <summary>
/// 釋放內存
/// </summary>
public static void ClearMemory()
{
     GC.Collect();
     GC.WaitForPendingFinalizers();
     if (Environment.OSVersion.Platform == PlatformID.Win32NT)
     {
         SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
     }
}進程

如何獲取當前應用佔用的內存大小:內存

/// <summary>
/// 釋放內存
/// </summary>
public static void ClearMemory()
{
     //得到當前工做進程
     Process proc = Process.GetCurrentProcess();
     long usedMemory = proc.PrivateMemorySize64;
     if (usedMemory > 1024 * 1024 * 20)
     {
         GC.Collect();
         GC.WaitForPendingFinalizers();
         if (Environment.OSVersion.Platform == PlatformID.Win32NT)
         {
             SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
         }
     }
}it

相關文章
相關標籤/搜索