且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-03-27 02:17:36

从this岗位:

为了让整个PC的CPU和内存使用:

using System.Diagnostics;

再全局声明:

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

然后得到的CPU时间,只需调用NextValue()$c$c>方法:

this.theCPUCounter.NextValue();

这将让你的CPU占用率

This will get you the CPU usage

对于内存的使用,同样的道理也适用,我相信:

As for memory usage, same thing applies I believe:

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

然后拿到内存使用,只需调用NextValue()$c$c>方法:

this.theMemCounter.NextValue();

特定进程的CPU和内存使用:

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

其中, 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);

其中, Process.GetCurrentProcess()。ProcessName 是你希望得到有关信息的进程名。

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

注意工作集可能无法在自己的权利,以确定进程的内存占用足够 - 看到的什么是专用字节,虚拟字节,工作集?

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

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

处理器\\%处理器时间进程\\处理器时间百分比之间的区别处理器是PC本身和过程是每个个体的过程。所以处理器的处理器时间将是在PC上使用。一个进程的处理器时间是指定的进程使用。对于类别名称的完整描述:性能监视器计数器

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

使用性能计数器的替代

使用System.Diagnostics.Process.TotalProcessorTime和System.Diagnostics.ProcessThread.TotalProcessorTime属性来计算你的处理器的使用,因为这文章介绍。

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