views:

1359

answers:

2

Hello, how can I get a list of all processes in C# and then for each process current memory and CPU consumption?

Sample code is highly appreciated.

Thank you!

+3  A: 

The Process class has a GetProcesses method that will let you enumerate the running processes and list a bunch of stats like memory usage and CPU time. Look at the documentation under properties for the stats.

Memory usage is a complex matter. There is really no single number, that describe the usage. Please see Russinovich's excellent series on the matter. The first installment is here: http://blogs.technet.com/markrussinovich/archive/2008/07/21/3092070.aspx

Brian Rasmussen
I know about the class, but I couldn't figure out how to get CPU consumption, or which of the 100 memory related fields actually means the real RAM consumption.
Alex
Memory usage is a bit complex, but my guess is you probably want to look at PrivateMemory.
Brian Rasmussen
I guess it depends what you mean by RAM (memory) consumption. Windows stores at least two values for the amount of memory being used by a process. The physical memory in use, and the size of the virtual memory allocated to the process. both are consumption...
kdmurray
The various ProcessorTime properties gives you kernel, user and sum for CPU usage. Is that not what you're looking for?
Brian Rasmussen
Sorry, let me clarify. I'm basically looking for the values that would be in the task manager as well.
Alex
@kdmurray: it is actually more complex than that. You also have memory mappings shared/private, reserved vs. commit and so forth.
Brian Rasmussen
Task manager has numerous options for memory usage similar to what the Process class gives you. As for CPU load % I believe there's a performance counter for that, but I am not sure. I'll have a look.
Brian Rasmussen
I tried experimenting with the performance counter but it doesn't seem to give an immediate read (it takes like a second to read - measurement time?) or i do something wrong, i dont know.
Alex
Take a look at http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx for info on performance counters
Brian Rasmussen
I studied the page earlier today, and it seems way too complex for the task?
Alex
Well process monitor is a bit complex. If you want to give the user a simplified view of the world, you need to figure out how you want to simplify it.
Brian Rasmussen
+2  A: 

See following link
http://www.codeproject.com/KB/system/processescpuusage.aspx

eip10