码迷,mamicode.com
首页 > 其他好文 > 详细

SetProcessWorkingSetSize减少内存占用

时间:2018-01-27 18:56:26      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:col   orm   log   获取   kernel   env   roc   getc   释放内存   

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

SetProcessWorkingSetSize减少内存占用

标签:col   orm   log   获取   kernel   env   roc   getc   释放内存   

原文地址:https://www.cnblogs.com/zsy/p/8366559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!