Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取进程的CPU和内存使用率的正确性能计数器是什么?_C#_Memory Management_Cpu Usage_Performancecounter - Fatal编程技术网

C# 获取进程的CPU和内存使用率的正确性能计数器是什么?

C# 获取进程的CPU和内存使用率的正确性能计数器是什么?,c#,memory-management,cpu-usage,performancecounter,C#,Memory Management,Cpu Usage,Performancecounter,如何使用.NETPerformanceCounter类获取特定进程的CPU和内存使用量?又有什么区别呢 处理器\%处理器时间和处理\%处理器时间 我对这两者有点困惑。发帖: 要了解整个PC的CPU和内存使用情况: using System.Diagnostics; private PerformanceCounter theCPUCounter = new PerformanceCounter("Process", "% Processor Time",

如何使用.NET
PerformanceCounter
类获取特定进程的CPU内存使用量?又有什么区别呢

处理器\%处理器时间
处理\%处理器时间

我对这两者有点困惑。

发帖:

要了解整个PC的CPU和内存使用情况:

using System.Diagnostics;
private PerformanceCounter theCPUCounter = 
   new PerformanceCounter("Process", "% Processor Time",              
   Process.GetCurrentProcess().ProcessName);
然后在全球范围内宣布:

private PerformanceCounter theCPUCounter = 
   new PerformanceCounter("Processor", "% Processor Time", "_Total"); 
然后,要获得CPU时间,只需调用以下方法:

这将使您了解CPU的使用情况

至于内存使用,我认为同样的情况也适用:

private PerformanceCounter theMemCounter = 
   new PerformanceCounter("Memory", "Available MBytes");
然后,要获取内存使用情况,只需调用以下方法:

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

using System.Diagnostics;
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
是您希望获取有关信息的进程名称

private PerformanceCounter theMemCounter = 
   new PerformanceCounter("Process", "Working Set",
   Process.GetCurrentProcess().ProcessName);
请注意,工作集本身可能不足以确定进程的内存占用——请参阅

要检索所有类别,请参见

Processor\%Processor Time
Process\%Processor Time
之间的差异是
Processor
来自PC本身,而
Process
是每个单独的进程。因此,处理器的处理器时间将是PC上的使用情况。进程的处理器时间将是指定的进程使用情况。有关类别名称的完整说明:

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

使用和属性计算处理器的使用情况,如下所述。

Pelo Hyper-V:

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

但是它会给出调用函数的当前进程的CPU/Mem使用情况吗?如果我像上面那样创建CPU计数器,我会得到一个InvalidOPerationException:“无法加载计数器名称数据,因为从注册表读取了无效索引“”。感谢您提供的详细答案!调用新PerformanceCounter时(“进程”,“处理器时间”,Process.GetCurrentProcess().ProcessName)我得到一个百分比。我应该如何解释这个百分比?这是机器上所有内核的百分比吗?@Legend我粗略的测试表明,这是每个处理器的处理器使用量之和。对于4个内核,
PerformanceCounter(“进程”,“处理器时间”,Process.GetCurrentProcess().ProcessName)
最多可返回
400
,这意味着进程正在使用每个CPU的100%。为什么在任何地方都不清楚这一点是不幸的,因为必须依靠粗略的测试。对于c#,这是“如何获得进程的CPU使用率?”的投票率/回答率最高的问题,但仍然没有人提及。各种technet、msdn和msdn的博客帖子都有相互矛盾的信息,只是为了让人更加困惑。对于仍然在这里登陆的人,我想借用@Quantic对计数器值的说法。如详细所述,
处理器(%Processor Time)
计数器将为100,并将给出计算机中所有处理器/核心/etc的总使用量。但是,
处理器(%ProcessTime)
按逻辑处理器的数量进行缩放。要获得一台计算机的平均使用率,请将结果除以Environment.ProcessorCount,没有一种正确的方法可以测量内存使用率。内存可以以许多不同的方式使用。例如,您是否计算交换到磁盘的内存或刚刚保留/提交但尚未写入的内存,。。。