List all processes and their current memory & CPU consumption?
Asked Answered
M

1

20

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.

Micromillimeter answered 8/8, 2009 at 6:23 Comment(0)
M
25

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

Mirador answered 8/8, 2009 at 6:28 Comment(12)
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.Micromillimeter
Memory usage is a bit complex, but my guess is you probably want to look at PrivateMemory.Mirador
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...Grimona
The various ProcessorTime properties gives you kernel, user and sum for CPU usage. Is that not what you're looking for?Mirador
Sorry, let me clarify. I'm basically looking for the values that would be in the task manager as well.Micromillimeter
@kdmurray: it is actually more complex than that. You also have memory mappings shared/private, reserved vs. commit and so forth.Mirador
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.Mirador
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.Micromillimeter
Take a look at msdn.microsoft.com/en-us/library/… for info on performance countersMirador
I studied the page earlier today, and it seems way too complex for the task?Micromillimeter
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.Mirador
@Micromillimeter To get a explanation of why the performance counter "has a delay" see this old answer of mine where I go in to a little depth of the why.Pogonia

© 2022 - 2024 — McMap. All rights reserved.