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

标签 c# .net performancecounter system.diagnostics

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

最佳答案

对于每个进程数据:

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)+" %");
}

性能计数器除了工作集和处理器时间还有其他计数器。

关于c# - 使用 PerformanceCounter 跟踪每个进程的内存和 CPU 使用情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3411805/

相关文章:

c# - 如何让消息对话框居中

.net - MSDN 文档错误? "If the procedure is Shared, all its local variables are automatically shared. This includes the Static variables."

c# - 如何模拟 gremlin 服务器或创建内存图以进行单元测试?

azure - 可以通过 Perfmon 查看 azure 性能诊断

c# - 无法将两个docker、一个数据库、一个.net core应用程序连接在一起

c# - 使静态资源在 Azure Web 角色中可用

c# - 在 C# 中通过 Imgur API 匿名获取图像信息

.net - 如何从 DataGridView 数据单元格获取按键、按键事件

.net - % RT 检查时间

powershell - 具有独立于区域设置的 ID 的 get-counter