【问题标题】:What is the correct Performance Counter to get CPU and Memory Usage of a Process?获取进程的 CPU 和内存使用情况的正确性能计数器是什么?
【发布时间】:2011-06-08 11:23:34
【问题描述】:

如何使用 .NET PerformanceCounter 类获取特定进程的 CPU内存使用情况?还有和

有什么区别

Processor\% Processor TimeProcess\% Processor Time?

我对这两个有点困惑。

【问题讨论】:

  • 没有一种正确的方法来衡量内存使用情况。内存可以以许多不同的方式使用。例如,您是否计算交换到磁盘的内存或仅保留/提交但尚未写入的内存,...

标签: c# memory-management cpu-usage performancecounter


【解决方案1】:

获取类似于任务管理器中显示的值

PerformanceCounter total_cpu = new PerformanceCounter("Process", "% Processor Time", "_Total");
PerformanceCounter process_cpu = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
Console.writeln (((process_cpu.NextValue()/( (Environment.ProcessorCount)* total_cpu.NextValue()))*100) )

【讨论】:

    【解决方案2】:

    来自this 发帖:

    获取整个 PC CPU 和内存使用情况:

    using System.Diagnostics;
    

    然后全局声明:

    private PerformanceCounter theCPUCounter = 
       new PerformanceCounter("Processor", "% Processor Time", "_Total"); 
    

    然后要获取CPU时间,只需调用NextValue()方法:

    this.theCPUCounter.NextValue();
    

    这将为您提供 CPU 使用率

    至于内存使用,我相信同样适用:

    private PerformanceCounter theMemCounter = 
       new PerformanceCounter("Memory", "Available MBytes");
    

    然后要获取内存使用情况,只需调用NextValue()方法:

    this.theMemCounter.NextValue();
    

    对于特定进程的 CPU 和内存使用情况:

    private PerformanceCounter theCPUCounter = 
       new PerformanceCounter("Process", "% Processor Time",              
       Process.GetCurrentProcess().ProcessName);
    

    Process.GetCurrentProcess().ProcessName 是您希望获取相关信息的进程名称。

    private PerformanceCounter theMemCounter = 
       new PerformanceCounter("Process", "Working Set",
       Process.GetCurrentProcess().ProcessName);
    

    其中Process.GetCurrentProcess().ProcessName 是您希望获取有关信息的进程名称。

    请注意,工作集本身可能不足以确定进程的内存占用——请参阅What is private bytes, virtual bytes, working set?

    要检索所有类别,请参阅Walkthrough: Retrieving Categories and Counters

    Processor\% Processor TimeProcess\% Processor Time 之间的区别在于,Processor 来自 PC 本身,Process 来自各个进程。所以处理器的处理器时间将是PC上的使用。进程的处理器时间将是指定的进程使用情况。类别名称的完整描述:Performance Monitor Counters

    使用性能计数器的替代方法

    使用System.Diagnostics.Process.TotalProcessorTimeSystem.Diagnostics.ProcessThread.TotalProcessorTime 属性来计算您的处理器使用率,正如article 所描述的那样。

    【讨论】:

    • 但是它会给出调用函数的当前进程的 CPU/Mem 使用率吗?
    • 如果我按上述方式创建 CPUCounter,我会收到 InvalidOPerationException:“无法加载计数器名称数据,因为从注册表中读取了无效索引 ''。”
    • 感谢您的详细解答!致电new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName); 时,我得到一个百分比。我应该如何解释这个百分比?这是机器上所有内核的百分比吗?
    • @Legend 我的粗略测试表明它是每个处理器的处理器使用率的总和。对于 4 核,PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName) 最多可以返回400,这意味着该进程正在使用每个 CPU 的 100%。不幸的是,为什么在 anywhere 不清楚这一点,因为必须依靠粗略的测试。这是“如何获得进程的 CPU 使用率?”的最高投票/回答问题。对于 c#,仍然没有人提到它。各种 technet、msdn 和 msdn 博客文章都有相互矛盾的信息,只是为了使其更加混乱。
    • 对于仍然登陆这里的任何人,我想捎带一下@Quantic 关于计数器值的说法。如详细的here 所述,Processor (% Processor Time) 计数器将超过 100,并将给出计算机中所有处理器/内核/等的总使用量。但是,Processor (% Process Time) 会按逻辑处理器的数量进行缩放。要获得计算机的平均使用量,请将结果除以 Environment.ProcessorCount
    【解决方案3】:

    Pelo Hyper-V:

    private PerformanceCounter theMemCounter = new PerformanceCounter(
        "Hyper-v Dynamic Memory VM",
        "Physical Memory",
        Process.GetCurrentProcess().ProcessName); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 2012-04-03
      • 2010-10-24
      • 1970-01-01
      相关资源
      最近更新 更多