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

  • 相关阅读:
    TIME_WAIT和CLOSE_WAIT的区别
    shell备份脚本
    No package 'eventlog' found
    Linux下升级安装Python-3.6.2版本
    mysql的binlog安全删除的一种方法
    windows 清理 cbs.log 文件
    Linux crontab 查看所有用户的crontab任务
    java抽象类与接口回顾
    java类的回顾
    windows的MySQL安装
  • 原文地址:https://www.cnblogs.com/zsy/p/8366559.html
Copyright © 2020-2023  润新知