[英]Using PerformanceCounter to track memory and CPU usage per process?
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.性能计数器还有除工作集和处理器时间之外的其他计数器。
I think you want Windows Management Instrumentation.我认为您需要Windows Management Instrumentation。
EDIT: See here:编辑:见这里:
If you are using .NET Core, the System.Diagnostics.PerformanceCounter
is not an option.如果您使用 .NET Core,则System.Diagnostics.PerformanceCounter
不是一个选项。 Try this instead:试试这个:
System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
long ram = p.WorkingSet64;
Console.WriteLine($"RAM: {ram/1024/1024} MB");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.