【问题标题】:How to use Performance Counter or Process class correctly in C# to get memory usage of current process?如何在 C# 中正确使用性能计数器或进程类来获取当前进程的内存使用情况?
【发布时间】:2012-01-23 13:16:39
【问题描述】:

根据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。

那我是不是做错了什么?

【问题讨论】:

    标签: c# .net performancecounter


    【解决方案1】:

    你必须告诉你的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
    

    【讨论】:

    • 两者给出的值是否完全相同?你可以发布它们吗?我想看看。
    • 奇怪的是,现在即使没有刷新方法,我的 PerformanceCounter 也可以给出正确的数字....顺便说一句,您的刷新方法工作正常。谢谢。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-08
      • 2011-11-05
      • 1970-01-01
      • 2015-03-11
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多