如何正确使用性能计数器或进程类在C#中获取当前进程的内存使用情况如何? [英] How to use Performance Counter or Process class correctly in C# to get memory usage of current process?

查看:469
本文介绍了如何正确使用性能计数器或进程类在C#中获取当前进程的内存使用情况如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据<一href="http://stackoverflow.com/questions/3411805/how-to-use-net-performancecounter-to-track-memory-and-cpu-usage-per-process">How使用.NET的PerformanceCounter跟踪每个进程的内存和CPU的使用情况? 的PerformanceCounter 应该给我一个给定的进程的内存使用量的数量。

According to How to use .NET PerformanceCounter to track memory and CPU usage per process? PerformanceCounter should give me the number of memory usage of a given process.

根据 MSDN 进程实例也可以给我更多或更少相同的号码。

According to MSDN, Process instance may also give me more or less the same number.

为了验证我的假设,我写了下面code:

In order to verify my assumptions, I wrote the following code:

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

完全一样!在此相反,在进程管理器显示专用字节从32732增加到63620。

Exactly the same! In the contrast, private bytes shown in Process Explorer increased from 32732 to 63620.

所以,我做错了什么?

推荐答案

您必须告诉你的过程比如,它应该刷新其缓存数据。数据将在每次你访问一个属性,可以提高性能的目的时没有收集。您必须手动需求数据更新。

You have to tell your process instance it should refresh its cached data. Data is not gathered each time you access to a property for performance purposes. You have to manually demand the data update.

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

这是为你的过程。 对于性能计数器, NextValue()应该是每次检索新的新的数据,所以我无法解释为什么它没有你的机器上。在我的正常工作。

That's for your process. For performance counters, NextValue() is supposed to retrieve a new fresh data each time, so I can't explain why it doesn't on your machine. On mine it works fine.

修改的:

随着 process.Refresh()补充说,这里就是我得到:

With the process.Refresh() added, here's what I get:

                         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

这篇关于如何正确使用性能计数器或进程类在C#中获取当前进程的内存使用情况如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆