首页 文章

测量子进程的CPU和RAM使用情况

提问于
浏览
1

我有以下过程:

public void Run()
        {

            ProcessStartInfo serverPInfo = new ProcessStartInfo("javaw", "-jar -Xms1024M -Xmx1024M \"C:\\Users\\David\\Documents\\Visual Studio 2012\\Projects\\ConsoleApplication8\\Debug\\craftbukkit.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?

我试过 serverP.WorkingSet64serverP.PrivateMemorySize64serverP.PagedMemorySize64 ,但它们都返回常量值 . 它们都没有改变(就像任务管理器中的RAM使用率计) .

我不知道如何获得当前的CPU使用率 .

我在互联网上看了一下,但我找到的大多数东西都是 PerformanceMonitor . 我不想使用它,因为可能有更多的"javaw"进程实例,我只想测量我在上面的代码中创建的子进程的CPU和RAM使用情况 .

任何帮助,将不胜感激 .

2 回答

  • 0

    您可以使用 PerformanceCounter 仅测量子进程活动 . 但是您需要使用其Process ID而不是Process Name . 当你创建它时,计数器的 RawValue 等于 ProcessId .

    Here is an answer describing such technique.

  • 0

    没有"current CPU usage"这样的东西 . 它只能在一段时间内测量,例如持续1秒 . 您可以使用TotalProcessorTime属性,每秒测量一次并减去之前的值 . 这将为您提供在上一个测量周期内使用CPU的时间 .
    为了测量RAM的使用情况,我正在使用WorkingSet64和PrivateMemorySize64 - 这对我很有用 .

相关问题