问题描述
我怎样才能获得的 CPU 并使用.NET性能计数器类特定进程中的内存使用?并且也就是
How can I get the CPU and Memory usage of a particular process using .net Performance Counter class? And also what is the difference between
处理器\\%处理器时间
和进程\\处理器时间百分比
?
我在这两者之间有点混淆。
I am a bit confused between these two.
推荐答案
从this岗位:
为了让整个PC的CPU和内存使用:
using System.Diagnostics;
再全局声明:
private PerformanceCounter theCPUCounter =
new PerformanceCounter("Processor", "% Processor Time", "_Total");
然后得到的CPU时间,只需调用<$c$c>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");
然后拿到内存使用,只需调用<$c$c>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.
这篇关于什么是正确的性能计数器来获得一个进程的CPU和内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!