19

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Alex
  • 75,813
  • 86
  • 255
  • 348

1 Answers1

24

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
  • 114,645
  • 34
  • 221
  • 317
  • 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 Aug 08 '09 at 06:31
  • 2
    Memory usage is a bit complex, but my guess is you probably want to look at PrivateMemory. – Brian Rasmussen Aug 08 '09 at 06:33
  • 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 Aug 08 '09 at 06:34
  • The various ProcessorTime properties gives you kernel, user and sum for CPU usage. Is that not what you're looking for? – Brian Rasmussen Aug 08 '09 at 06:36
  • Sorry, let me clarify. I'm basically looking for the values that would be in the task manager as well. – Alex Aug 08 '09 at 06:38
  • @kdmurray: it is actually more complex than that. You also have memory mappings shared/private, reserved vs. commit and so forth. – Brian Rasmussen Aug 08 '09 at 06:38
  • 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 Aug 08 '09 at 06:40
  • 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 Aug 08 '09 at 06:41
  • Take a look at http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx for info on performance counters – Brian Rasmussen Aug 08 '09 at 06:42
  • I studied the page earlier today, and it seems way too complex for the task? – Alex Aug 08 '09 at 06:44
  • 3
    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 Aug 08 '09 at 06:51
  • 1
    @Alex To get a explanation of *why* the performance counter "has a delay" see [this old answer](http://stackoverflow.com/questions/8462331/i-need-to-call-accurate-cpu-usage-of-a-single-process/8462977#8462977) of mine where I go in to a little depth of the *why*. – Scott Chamberlain Dec 31 '13 at 14:43