PerformanceCounter (CPU load) staggers for 20 seconds

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Pete (Pete_at_discussions.microsoft.com)
Date: 01/26/05


Date: Tue, 25 Jan 2005 22:45:03 -0800

When using PerformanceCounter.NextValue to measure CPU load (percentage), it
returns quickly on every call but then takes 20 seconds at the 2 minute
boundary. After this it again returns quickly. Here's a sample:

//**********************************
using System.Diagnostics;
using System.Threading;

PerformanceCounter pc = new PerformanceCounter("Processor",
        "% Processor Time", "_Total");
pc.NextValue(); // prime (values are relative to last)
DateTime now = DateTime.Now;
for (int i = 0; i < 240; ++i) { // it staggers at i == 120
        int start = Environment.TickCount;
        decimal load = decimal.Round((decimal) pc.NextValue(), 0);
        int end = Environment.TickCount;
        Console.WriteLine("Period: {0}, Load: {1}, Delta: {2}ms",
        DateTime.Now - now, load, end - start);
        Thread.Sleep(1000);
}
//**********************************

This code displays the time it takes in milliseconds for each call to
PerformanceCounter.NextValue to measure CPU load as a percentage. It
typically takes 0ms for the first 119 calls. Then at 01:59 it takes 20
seconds. After that it again returns quickly. I'm seeing this on three
different systems (XP Home, XP Pro, Server 2003), and using .NET 1.1 and .NET
2.0 Beta2, both debug and release builds. Has anyone else had problems with
this issue?

In production code I'm calling PerformanceCounter.NextValue from a separate
thread, but it still seems to "freeze" ALL currently running threads of my
app - nice 20 second delay :-)

It only seems to happen at the 2 minute boundary, after that it doesn't
(seem to) happen again.

Thanks for any help.



Relevant Pages

  • PerformanceCounter (CPU load) staggers for 20 seconds after 2 minu
    ... PerformanceCounter.NextValue to measure CPU load as a percentage. ... In production code I'm calling PerformanceCounter.NextValue from a separate ... but it still seems to "freeze" ALL currently running threads of my ...
    (microsoft.public.dotnet.framework.performance)
  • Re: PerformanceCounter (CPU load) staggers for 20 seconds after 2 minu
    ... sounds as though the garbage collector thread has kicked in and suspended ... all other threads until it has completed, does the production code use alot ... > PerformanceCounter.NextValue to measure CPU load as a percentage. ...
    (microsoft.public.dotnet.framework.performance)
  • Re: program runs almost twice as slow on 2 CPU machine when 1 CPU
    ... I would suspect interrupts... ... it would be a good idea for you to learn what interrupts are ... stopped dead in its tracks when CPU load, ... case 1, run two app concurenlly, both app takes 12 minutes ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Q: How to improve CGI performance for this?
    ... provider (reportedly 25% CPU load at some times). ... The first thing to do is to identify if there is any one parts of your app that can be improved, ... If you are on Windows you can use Perl Performance Validator to get a performance profile of your application. ...
    (comp.lang.perl.misc)
  • Re: more on timing problems
    ... > to the CPU load: during an application startup phase I've got many ... > errors, but then, event if the app is consuming a lot of CPU time the ...
    (microsoft.public.development.device.drivers)