zoukankan      html  css  js  c++  java
  • C#释放内存的方法

    原文:https://blog.csdn.net/zsy619/article/details/79181719

    --------------------- 

    如何释放内存:

    [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);
             }
         }
    }
  • 相关阅读:
    BZOJ1787 [Ahoi2008]Meet 紧急集合[结论题]
    poj3728 The merchant[倍增]
    poj2750 Potted Flower[线段树]
    poj2482 Stars in Your Window[扫描线]
    poj2182 Lost Cows[BIT二分]
    UVA12096 The SetStack Computer
    第05组(65) 需求分析报告
    团队介绍与选题报告
    数据采集技术第三次作业
    结对编程作业
  • 原文地址:https://www.cnblogs.com/dabexiong/p/9907648.html
Copyright © 2011-2022 走看看