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?