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

发布于 2024-10-12 06:34:01 字数 222 浏览 7 评论 0原文

如何使用 .NET PerformanceCounter 类获取特定进程的 CPU内存使用情况? 之间有什么区别

Processor\% Processor TimeProcess\% Processor Time

?我对这两者有点困惑。

How can I get the CPU and Memory usage of a particular process using the .NET PerformanceCounter class? And also what is the difference between

Processor\% Processor Time and Process\% Processor Time?

I am a bit confused between these two.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北城半夏 2024-10-19 06:34:01

来自 post:

要获取整个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 是您希望获取相关信息的进程名称。

请注意,工作集本身可能不足以确定进程的内存占用量 - 请参阅什么是私有字节、虚拟字节、工作集吗?

要检索所有类别,请参阅演练:检索类别和计数器

Processor\% Processor TimeProcess\% Processor Time 之间的区别> 是Processor 来自PC 本身,Process 是每个单独的进程。因此处理器的处理器时间将是PC 上的使用时间。进程的处理器时间将是指定的进程使用情况。有关类别名称的完整说明:性能监视器计数器

替代方案使用性能计数器

使用 System.Diagnostics .Process.TotalProcessorTimeSystem.Diagnostics。 ProcessThread.TotalProcessorTime 属性用于计算处理器使用情况,如本文文章所述。

From this post:

To get the entire PC CPU and Memory usage:

using System.Diagnostics;

Then declare globally:

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

Then to get the CPU time, simply call the NextValue() method:

this.theCPUCounter.NextValue();

This will get you the CPU usage

As for memory usage, same thing applies I believe:

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

Then to get the memory usage, simply call the NextValue() method:

this.theMemCounter.NextValue();

For a specific process CPU and Memory usage:

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

where Process.GetCurrentProcess().ProcessName is the process name you wish to get the information about.

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

where Process.GetCurrentProcess().ProcessName is the process name you wish to get the information about.

Note that Working Set may not be sufficient in its own right to determine the process' memory footprint -- see What is private bytes, virtual bytes, working set?

To retrieve all Categories, see Walkthrough: Retrieving Categories and Counters

The difference between Processor\% Processor Time and Process\% Processor Time is Processor is from the PC itself and Process is per individual process. So the processor time of the processor would be usage on the PC. Processor time of a process would be the specified processes usage. For full description of category names: Performance Monitor Counters

An alternative to using the Performance Counter

Use System.Diagnostics.Process.TotalProcessorTime and System.Diagnostics.ProcessThread.TotalProcessorTime properties to calculate your processor usage as this article describes.

段念尘 2024-10-19 06:34:01

Pelo Hyper-V:

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

Pelo Hyper-V:

private PerformanceCounter theMemCounter = new PerformanceCounter(
    "Hyper-v Dynamic Memory VM",
    "Physical Memory",
    Process.GetCurrentProcess().ProcessName); 
空城仅有旧梦在 2024-10-19 06:34:01

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

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

To get the value similar to that displayed in task manager

PerformanceCounter total_cpu = new PerformanceCounter("Process", "% Processor Time", "_Total");
PerformanceCounter process_cpu = new PerformanceCounter("Process", "% Processor Time", Process.GetCurrentProcess().ProcessName);
Console.WriteLine(((process_cpu.NextValue()/( (Environment.ProcessorCount)* total_cpu.NextValue()))*100) )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文