Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct Performance Counter to get CPU and Memory Usage of a Process?

How can I get the CPU and Memory usage of a particular process using the .NET PerformanceCounter class? And also what is the difference between

Processor\% Processor Time and Process\% Processor Time?

I am a bit confused between these two.

like image 787
Anindya Chatterjee Avatar asked Jan 13 '11 12:01

Anindya Chatterjee


People also ask

What are CPU performance counters?

The CPU performance counters are counting the number of instructions, clock ticks and multi counters ticks. They are used to measure the run-time of a c-function. The result is stored in a global variable. Copyright © Infineon Technologies AG 2020.

How do I check my CPU memory usage?

Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab and click Memory in the left panel. The Memory window lets you see your current RAM usage, check RAM speed, and view other memory hardware specifications.


1 Answers

From this post:

To get the entire PC CPU and Memory usage:

using System.Diagnostics; 

Then declare globally:

private PerformanceCounter theCPUCounter =     new PerformanceCounter("Processor", "% Processor Time", "_Total");  

Then to get the CPU time, simply call the NextValue() method:

this.theCPUCounter.NextValue(); 

This will get you the CPU usage

As for memory usage, same thing applies I believe:

private PerformanceCounter theMemCounter =     new PerformanceCounter("Memory", "Available MBytes"); 

Then to get the memory usage, simply call the NextValue() method:

this.theMemCounter.NextValue(); 

For a specific process CPU and Memory usage:

private PerformanceCounter theCPUCounter =     new PerformanceCounter("Process", "% Processor Time",                  Process.GetCurrentProcess().ProcessName); 

where Process.GetCurrentProcess().ProcessName is the process name you wish to get the information about.

private PerformanceCounter theMemCounter =     new PerformanceCounter("Process", "Working Set",    Process.GetCurrentProcess().ProcessName); 

where Process.GetCurrentProcess().ProcessName is the process name you wish to get the information about.

Note that Working Set may not be sufficient in its own right to determine the process' memory footprint -- see What is private bytes, virtual bytes, working set?

To retrieve all Categories, see Walkthrough: Retrieving Categories and Counters

The difference between Processor\% Processor Time and Process\% Processor Time is Processor is from the PC itself and Process is per individual process. So the processor time of the processor would be usage on the PC. Processor time of a process would be the specified processes usage. For full description of category names: Performance Monitor Counters

An alternative to using the Performance Counter

Use System.Diagnostics.Process.TotalProcessorTime and System.Diagnostics.ProcessThread.TotalProcessorTime properties to calculate your processor usage as this article describes.

like image 92
SwDevMan81 Avatar answered Sep 18 '22 15:09

SwDevMan81