Measure CPU and RAM Usage of Child Process
I have the following process:
public void Run()
{
ProcessStartInfo serverPInfo = new ProcessStartInfo("javaw", "-jar -Xms1024M -Xmx1024M "C:UsersDavidDocumentsVisual Studio 2012ProjectsConsoleApplication8Debugcraftbukkit.jar" -o true -nojline");
serverPInfo.RedirectStandardInput = true;
serverPInfo.RedirectStandardOutput = true;
serverPInfo.RedirectStandardError = true;
serverPInfo.UseShellExecute = false;
serverP = new Process();
serverP.StartInfo = serverPInfo;
serverP.OutputDataReceived += new DataReceivedEventHandler(ServerOutputDataReceived);
serverP.ErrorDataReceived += new DataReceivedEventHandler(ServerErrorDataReceived);
serverP.Start();
serverP.BeginOutputReadLine();
serverP.BeginErrorReadLine();
serverP.WaitForExit();
}
How can I measure the process' CPU and RAM usage?
I tried serverP.WorkingSet64
, serverP.PrivateMemorySize64
and serverP.PagedMemorySize64
, but they all return constant values. None of them change (like the RAM usage meter in Task Manager).
I have no idea how to get the current CPU usage.
I looked on the internet, but most of the stuff I found was PerformanceMonitor
. I don't want to use this since there may be more instances of the "javaw" process and I just want to measure the CPU and RAM usage of the child process I created in the code above.
Any help would be appreciated.
You can use PerformanceCounter
to measure only a child process activity. But you need to use its Process ID instead of Process Name. The counter's RawValue
equals to ProcessId
, when you create it.
Here is an answer describing such technique.
There is no such a thing as "current CPU usage". It can be measured only against some period of time, last 1 second for instance. You can use TotalProcessorTime property, measure it every second and subtract the previous value. This will give you how much time CPU was used in the last measurement period.
To measure RAM usage I'm using WorkingSet64 and PrivateMemorySize64 - this works well for me.
上一篇: 特定进程的远程处理器/内存使用情况
下一篇: 测量子进程的CPU和RAM使用情况