VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之SetProcessWorkingSetSize减少内存占用

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

/// <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);
         }
     }
}


相关教程