如何使用System.Diagnostics.PerformanceCounter跟踪进程的内存和 CPU 使用情况?
问问题
62572 次
3 回答
55
对于每个过程数据:
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)+" %");
}
除了工作集和处理器时间之外,性能计数器还有其他计数器。
于 2010-08-23T06:46:26.497 回答
3
于 2010-08-05T04:47:10.307 回答
3
如果您使用的是 .NET Core,System.Diagnostics.PerformanceCounter
则不是一个选项。试试这个:
System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
long ram = p.WorkingSet64;
Console.WriteLine($"RAM: {ram/1024/1024} MB");
于 2019-12-10T14:16:55.750 回答