简体   繁体   English

使用 PerformanceCounter 跟踪每个进程的内存和 CPU 使用情况?

[英]Using PerformanceCounter to track memory and CPU usage per process?

如何使用System.Diagnostics.PerformanceCounter跟踪进程的内存和 CPU 使用情况?

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.性能计数器还有除工作集和处理器时间之外的其他计数器。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM