如何在C#中正确使用Performance Counter或Process类来获取当前进程的内存使用情况?

Gqq*_*big 8 .net c# performancecounter

根据如何使用.NET PerformanceCounter来跟踪每个进程的内存和CPU使用情况? PerformanceCounter应该给我一个给定进程的内存使用次数.

根据MSDN,Process实例也可能给我或多或少相同的数字.

为了验证我的假设,我写了以下代码:

class Program
{
    static Process process = Process.GetCurrentProcess();

    static PerformanceCounter privateBytesCounter = new PerformanceCounter("Process", "Private Bytes", process.ProcessName);
    static PerformanceCounter workingSetCounter = new PerformanceCounter("Process", "Working Set", process.ProcessName);

    static void Main(string[] args)
    {


        GetMeasure();

        Console.WriteLine("\nPress enter to allocate great amount of memory");
        Console.ReadLine();
        int[] arr = new int[10000000];
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = i;
        }

        GetMeasure();

        privateBytesCounter.Dispose();
        workingSetCounter.Dispose();
        Console.ReadKey();
    }

    private static void GetMeasure()
    {
        Console.WriteLine("{0,38} {1,20}", "Private bytes", "working set");
        Console.WriteLine("process data{0,23} {1,20}", process.PrivateMemorySize64 / 1024, process.WorkingSet64 / 1024);
        Console.WriteLine("PerformanceCounter data{0,12} {1,20}", privateBytesCounter.NextValue() / 1024, workingSetCounter.NextValue() / 1024);
    }

}
Run Code Online (Sandbox Code Playgroud)

输出看起来像

                         Private bytes          working set
process data                  22880                17516
PerformanceCounter data       21608                15608

Press enter to allocate great amount of memory

                         Private bytes          working set
process data                  22880                17516
PerformanceCounter data       21608                15608
Run Code Online (Sandbox Code Playgroud)

完全相同的!相比之下,Process Explorer中显示的专用字节从32732增加到63620.

我做错了什么?

ken*_*n2k 9

您必须告诉您的process实例它应该刷新其缓存的数据.每次出于性能目的访问属性时,都不会收集数据.您必须手动请求数据更新.

private static void GetMeasure()
{
    process.Refresh();  // Updates process information

    Console.WriteLine("{0,38} {1,20}", "Private bytes", "working set");
    Console.WriteLine("process data{0,23} {1,20}", process.PrivateMemorySize64 / 1024, process.WorkingSet64 / 1024);
    Console.WriteLine("PerformanceCounter data{0,12} {1,20}", privateBytesCounter.NextValue() / 1024, workingSetCounter.NextValue() / 1024);
}
Run Code Online (Sandbox Code Playgroud)

那是给你的process.对于性能计数器,NextValue()应该每次检索一个新的新数据,所以我无法解释为什么它不在你的机器上.我的工作正常.

编辑:

随着process.Refresh()添加,这是我得到的:

                         Private bytes          working set
process data                  25596                22932
PerformanceCounter data       26172                23600

Press enter to allocate great amount of memory
                         Private bytes          working set
process data                  65704                61848
PerformanceCounter data       65828                61880
Run Code Online (Sandbox Code Playgroud)