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

查看:1921
本文介绍了使用的PerformanceCounter跟踪每个进程的内存和CPU的使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用<一个href="http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx">System.Diagnostics.PerformanceCounter跟踪内存和CPU使用率的程序?

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

推荐答案

有关每个进程的数据:

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.

这篇关于使用的PerformanceCounter跟踪每个进程的内存和CPU的使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆