Using PerformanceCounter to track memory and CPU usage per process?

我只是一个虾纸丫 提交于 2019-11-26 12:59:11

问题


How can I use System.Diagnostics.PerformanceCounter to track the memory and CPU usage for a process?


回答1:


For per process data:

Process p = /*get the desired process here*/;
PerformanceCounter ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName);
PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
while (true)
{
    Thread.Sleep(500);
    double ram = ramCounter.NextValue();
    double cpu = cpuCounter.NextValue();
    Console.WriteLine("RAM: "+(ram/1024/1024)+" MB; CPU: "+(cpu)+" %");
}

Performance counter also has other counters than Working set and Processor time.




回答2:


I think you want Windows Management Instrumentation.

EDIT: See here:

Measure a process CPU and RAM usage

How to get the CPU Usage in C#?



来源:https://stackoverflow.com/questions/3411805/using-performancecounter-to-track-memory-and-cpu-usage-per-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!