如何计算私人工作集(内存)?

如何计算私人工作集(内存)?

问题描述:

我如何计算使用C#内存的私人工作集?我感兴趣的产生大致相同的数字作为的TaskMgr.exe

How do I calculate the private working set of memory using C#? I'm interested in produces roughly the same figure as taskmgr.exe.

我使用的过程命名空间,并使用类似 WorkingSet64 PrivateMemorySize64 ,但这些数字的方法/数据关闭的100MB或更多的时间。

I'm using the Process namespace and using methods/data like WorkingSet64 and PrivateMemorySize64, but these figures are off by 100MB or more at times.

这是一种高度可变数目,就不能计算。 Windows内存管理不断交换进出RAM页。从的TaskMgr.exe性能计数器得到它。你可以得到相同数量是这样的:

This is a highly variable number, you cannot calculate it. The Windows memory manager constantly swaps pages in and out of RAM. TaskMgr.exe gets it from a performance counter. You can get the same number like this:

using System;
using System.Diagnostics;

class Program {
    static void Main(string[] args) {
        string prcName = Process.GetCurrentProcess().ProcessName;
        var counter = new PerformanceCounter("Process", "Working Set - Private", prcName);
        Console.WriteLine("{0}K", counter.RawValue / 1024);
        Console.ReadLine();
    }
}

,一定要提防的数量确实没有多大的意义,它会下降,当其他进程开始,争夺RAM。

Do beware that the number really doesn't mean much, it will drop when other processes get started and compete for RAM.