Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to get the Memory Usage of a particular process in percentage (%). Below is the code which I used for CPU. But I am unable to get the same for Memory. There are so many counters in Memory Performance that I'm confused how to calculate or we can directly get the display in percentage (%). Pls help.


C#
PerformanceCounterCategory cpuProcessCategory = new PerformanceCounterCategory("Process");
               string[] instanceNames = cpuProcessCategory.GetInstanceNames();

             

               foreach (string name in instanceNames)
               {
                   try
                   {

PerformanceCounter cpuProcess = new PerformanceCounter("Process", "% Processor Time", name);
 float cpuUsage = cpuProcess.NextValue();
 Console.Write("Process: '{0}'   ", name);
}
catch
{}
}
Posted

1 solution

Hi.
1) Open MMC
2) File -> Add/Remove snap-in
3) In the list of available snap-ins, locate "Performance", mark it and click add
4) Expand the treeview and select "Performance" You will then see the performance graph.
5) Rightclick the grapch and select "Add counters"

Then a windows opens and there you can see all possible counters you can use in your code.
 
Share this answer
 
Comments
Abhik03 26-Apr-13 9:08am    
Hi AlluvialDepsoit, I'm unable to open MMC. It says, you cannot open MMC in author mode.
Can't we do it programatically?
StianSandberg 26-Apr-13 9:16am    
If you manage to open the MMC and add the Performance snap-in, you will see a list of all available counters. That will help you find the right one.

I guess the one you'll need is
var cpuProcess = new PerformanceCounter("Process", "Virtual Bytes", name);

Michael Groeger have written a nice article about this: http://www.codeproject.com/Articles/8590/An-Introduction-To-Performance-Counters

Find setting restricting the access, which is Local Computer Policy > User Configuration > Administrative Templates > Windows Components > Microsoft Management Console > Restrict the user from entering author mode, Enabled. Set it to disabled, then you can access MMC :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900