Connecting Tech Pros Worldwide Forums | Help | Site Map

CPU usage amd memory usage

Sirisha
Guest
 
Posts: n/a
#1: Oct 10 '06
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

Bruce Wood
Guest
 
Posts: n/a
#2: Oct 10 '06

re: CPU usage amd memory usage



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

Sirisha
Guest
 
Posts: n/a
#3: Oct 10 '06

re: CPU usage amd memory usage


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:
Quote:
>
Sirisha wrote:
Quote:
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.
>
>
Mugunth
Guest
 
Posts: n/a
#4: Oct 11 '06

re: CPU usage amd memory usage


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:
Quote:
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:
>
Quote:
Sirisha wrote:
Quote:
I am using the following code to get the CPU usage
>
Quote:
Quote:
PerformanceCounter myCounter;
myCounter = new PerformanceCounter();
>
Quote:
Quote:
myCounter.CategoryName = "Processor";
myCounter.CounterName = "% Processor Time";
myCounter.InstanceName = "_Total";
>
Quote:
Quote:
for(int i=0; i < 20; i++)
myCounter.NextValue();
>
Quote:
Quote:
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 %.
>
Quote:
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.
>
Quote:
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.
>
Quote:
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.
Closed Thread