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

时间:2012-01-23 13:16:40

标签: c# .net performancecounter

根据How to use .NET PerformanceCounter to track memory and CPU usage per process? PerformanceCounter应该给出给定进程的内存使用次数。

根据MSDNProcess实例也可能给我或多或少相同的数字。

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

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

}

输出看起来像

                         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

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

我做错了什么?

2 个答案:

答案 0 :(得分: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);
}

那是你的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

答案 1 :(得分:4)

警告:我的内存分析器(.NET Memory Profiler)显示Process.Refresh()临时分配了大量内存,因此,如果您定期通过以下方式读取性能计数器,请记住这一点。使用计时器。