×
您的位置: 首页 > 编程笔记

使用的PerformanceCounter跟踪每个进程的内存和CPU的使用情况?(Using PerformanceCounter to track memory and CPU usage per

Java 时间:2020-06-15  查看:872   收藏

How can I use System.Diagnostics.PerformanceCounter to track the memory and CPU usage for a 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.

我如何使用System.Diagnostics.PerformanceCounter跟踪内存和CPU使用率的程序?

解决方案

有关每个进程的数据:

 进程p = / *在这里获得想要的程序* /;
的PerformanceCounter ramCounter =新的PerformanceCounter(过程,工作集,p.ProcessName);
的PerformanceCounter cpuCounter =新的PerformanceCounter(过程,%处理器时间,p.ProcessName);
而(真)
{
    Thread.sleep代码(500);
    双RAM = ramCounter.NextValue();
    双CPU = cpuCounter.NextValue();
    Console.WriteLine(RAM:+(RAM / 1024/1024)+MB; CPU:+(CPU)+%);
}

性能计数器也有其他比专柜工作组和处理器时间。

 

0% (0)
0% (0)