美文网首页
2022-03-26 SetProcessWorkingSet

2022-03-26 SetProcessWorkingSet

作者: 1f658716b568 | 来源:发表于2022-03-26 11:33 被阅读0次
    [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
    public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
    
    /// <summary>
    /// 释放内存
    /// </summary>
    public static void ClearMemory()
    {undefined
         GC.Collect();
         GC.WaitForPendingFinalizers();
         if (Environment.OSVersion.Platform == PlatformID.Win32NT)
         {undefined
             SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
         }
    }
    
    如何获取当前应用占用的内存大小:
    
    /// <summary>
    /// 释放内存
    /// </summary>
    public static void ClearMemory()
    {undefined
         //获得当前工作进程
         Process proc = Process.GetCurrentProcess();
         long usedMemory = proc.PrivateMemorySize64;
         if (usedMemory > 1024 * 1024 * 20)
         {undefined
             GC.Collect();
             GC.WaitForPendingFinalizers();
             if (Environment.OSVersion.Platform == PlatformID.Win32NT)
             {undefined
                 SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1);
             }
         }
    }
    ————————————————
    版权声明:本文为CSDN博主「我爱程序」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/zsy619/article/details/79181719
    

    相关文章

      网友评论

          本文标题:2022-03-26 SetProcessWorkingSet

          本文链接:https://www.haomeiwen.com/subject/arzjjrtx.html