Ani*_*jee 64 c# memory-management performancecounter cpu-usage
如何使用.NET 类获取特定进程的CPU和内存使用情况PerformanceCounter
?还有什么区别
Processor\% Processor Time
和Process\% Processor Time
?
我对这两者感到有些困惑.
SwD*_*n81 113
从这篇文章:
要获得整个PC CPU和内存使用情况:
using System.Diagnostics;
Run Code Online (Sandbox Code Playgroud)
然后全局声明:
private PerformanceCounter theCPUCounter =
new PerformanceCounter("Processor", "% Processor Time", "_Total");
Run Code Online (Sandbox Code Playgroud)
然后要获得CPU时间,只需调用NextValue()
方法:
this.theCPUCounter.NextValue();
Run Code Online (Sandbox Code Playgroud)
这将为您提供CPU使用率
至于内存使用情况,我认为同样适用:
private PerformanceCounter theMemCounter =
new PerformanceCounter("Memory", "Available MBytes");
Run Code Online (Sandbox Code Playgroud)
然后要获取内存使用情况,只需调用NextValue()
方法:
this.theMemCounter.NextValue();
Run Code Online (Sandbox Code Playgroud)
对于特定进程CPU和内存使用情况:
private PerformanceCounter theCPUCounter =
new PerformanceCounter("Process", "% Processor Time",
Process.GetCurrentProcess().ProcessName);
Run Code Online (Sandbox Code Playgroud)
Process.GetCurrentProcess().ProcessName
您希望获取有关信息的进程名称在哪里.
private PerformanceCounter theMemCounter =
new PerformanceCounter("Process", "Working Set",
Process.GetCurrentProcess().ProcessName);
Run Code Online (Sandbox Code Playgroud)
Process.GetCurrentProcess().ProcessName
您希望获取有关信息的进程名称在哪里.
请注意,工作集本身可能不足以确定进程的内存占用 - 请参阅什么是专用字节,虚拟字节,工作集?
要检索所有类别,请参阅演练:检索类别和计数器
之间的差异Processor\% Processor Time
,并Process\% Processor Time
为Processor
从PC本身Process
是每个个体的过程.因此,处理器的处理器时间将在PC上使用.进程的处理器时间将是指定的进程使用情况.有关类别名称的完整说明:性能监视器计数器
使用性能计数器的替代方法
使用System.Diagnostics.Process.TotalProcessorTime和System.Diagnostics.ProcessThread.TotalProcessorTime属性来计算您的处理器使用情况,如本文所述.