CPU usage amd memory usage

G

Guest

I am using the following code to get the CPU usage

PerformanceCounter myCounter;
myCounter = new PerformanceCounter();

myCounter.CategoryName = "Processor";
myCounter.CounterName = "% Processor Time";
myCounter.InstanceName = "_Total";

for(int i=0; i < 20; i++)
myCounter.NextValue();

I ran this progam i am seeing values 0 or 100%, but when i see the task
manager the CPU usage is 6 to 22 %.

Am i missing something
 
B

Bruce Wood

Sirisha said:
I am using the following code to get the CPU usage

PerformanceCounter myCounter;
myCounter = new PerformanceCounter();

myCounter.CategoryName = "Processor";
myCounter.CounterName = "% Processor Time";
myCounter.InstanceName = "_Total";

for(int i=0; i < 20; i++)
myCounter.NextValue();

I ran this progam i am seeing values 0 or 100%, but when i see the task
manager the CPU usage is 6 to 22 %.

First of all, 20 reptitions aren't going to show you anything. You need
to take samples over 20,000, 200,000, or 2,000,000 reptitions, or
something of that order.

Second, the task manager is showing you an average CPU usage over
slices of time. Your program will finish in the blink of an eye and so
won't really register on the task manager's CPU monitor.

Third, since your program is doing nothing but processing, clearly
_while it's running_ it's using up 100% of the CPU, so your results are
not surprising.
 
G

Guest

Hi Bruce,

I wnat to get the values that the task manager is showing for the memory
usage and the CPU. How can i do that?
 
M

Mugunth

you must average out the CPU usage over a time slice.
CPU is either busy or idle, never 60% busy at anypoint of time.
When task manager reports 60% usage, it reports that, over a particular
time slice,
say 2000 ms, the CPU usage was 100% for 1200 ms.
It should be interpreted like this.


Instead of looping 20 times, check CPU usage over a period of time and
average out your timing.

Hope that helps...

Mugunth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top