By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,249 Members | 1,665 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,249 IT Pros & Developers. It's quick & easy.

CPU usage amd memory usage

P: n/a
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

--
Sirisha
Oct 10 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Sirisha wrote:
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.

Oct 10 '06 #2

P: n/a
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?
--
Sirisha
"Bruce Wood" wrote:
>
Sirisha wrote:
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.

Oct 10 '06 #3

P: n/a
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

On Oct 11, 1:43 am, Sirisha <spusap...@hotmail.comwrote:
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?
--
Sirisha

"Bruce Wood" wrote:
Sirisha wrote:
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.
Oct 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.