web-dev-qa-db-ja.com

プロセスのCPUおよびメモリ使用量を取得するための正しいパフォーマンスカウンターは何ですか?

.NET PerformanceCounterクラスを使用して特定のプロセスの[〜#〜] cpu [〜#〜]およびメモリ使用量を取得するにはどうすればよいですか?また、の違いは何ですか

Processor\% Processor TimeおよびProcess\% Processor Time

私はこれら2つを少し混同しています。

63

this 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 Time_と_Process\% Processor Time_の違いはProcessorはPC自体からのものであり、Processは個々のプロセスごとです。そのため、プロセッサのプロセッサ時間はPCでの使用量になります。プロセスのプロセッサー時間は、指定されたプロセス使用量になります。カテゴリ名の詳細な説明: パフォーマンスモニターカウンター

パフォーマンスカウンターを使用する代わりに

System.Diagnostics.Process.TotalProcessorTime および System.Diagnostics.ProcessThread.TotalProcessorTime プロパティを使用して、この article が説明するように、プロセッサの使用量を計算します。

111
SwDevMan81

Pelo Hyper-V:

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