Why a call to PerformanceCounter is slow?

我只是一个虾纸丫 提交于 2019-12-11 08:08:45

问题


I'm working on a server that reads the '% CPU' and 'Available memory' from PerformanceCounters. It works good on my development machine. But on actual servers, it is really slow to read from these two PerformanceCounters. at least at the first two read operations.
It can take up to 4-6 minutes performing the code below:

        Stopwatch watch = new Stopwatch();

        Log.Instance.Debug("Going to initialize Diagnostic's PerformanceCounters");

        watch.Start();

        m_CPUPerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
        m_MemoryPerformanceCounter = new PerformanceCounter("Memory", "Available MBytes", true);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        //Ensure an updated value...
        System.Threading.Thread.Sleep(1000);

        m_CPUPerformanceCounter.NextValue();
        m_MemoryPerformanceCounter.NextValue();

        watch.Stop();

        Log.Instance.Debug("Finished initializing Diagnosticss PerformanceCounters. Time elapsed: {0}", watch.Elapsed);  

When I run this code on my development machine, it will finish in less than 2 seconds (sometimes even less). But on the actual servers that clients of our product should use, it will take a long time. See below:

Finished initializing Diagnosticss PerformanceCounters. Time elapsed: 00:03:59.6706860

It is important that these read operations (and later 'read' operation) will execute really fast. Even at the beginning.

My development machine is Windows 7, 64 Bit, 8 GB RAM.
Clients servers are Windows server 2008 R2 Enterprise, 64 Bit, 4 GB RAM.

I Googled(and Binged) it, but couldn't find any answer. Why is this happening? And how can I fix it?


回答1:


The problem is most likely with counters provided by additional software installed on the monitored system.

In this case, you should do a performance counter initialization scan for all performance counters registered in system registry, which can be done with ProcMon.



来源:https://stackoverflow.com/questions/21990206/why-a-call-to-performancecounter-is-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!