如何跟踪进程的内存和CPU使用情况?

3

如何获取进程的CPU和内存使用情况?需要传递哪些值?

Process p = new Process();
PerformanceCounter ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName);
PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);

while (true)
{
    Thread.Sleep(500);
    double ram = ramCounter.NextValue();
    double cpu = cpuCounter.NextValue();
    Console.WriteLine("RAM: " + (ram / 1024 / 1024) + " MB; CPU: " + (cpu) + " %");
    Console.ReadLine();
}

1
@CodeCaster,你的编辑真正改变了问题的意图,获取控制台应用程序的性能与获取MVC应用程序的性能非常不同,而且问题的写法让人觉得OP现在想要控制台应用程序的性能,这并不是原始标签和标题所指示的。 - Scott Chamberlain
@Scott这不是我的问题,是OP不能正确表达自己的问题。在标题中说“MVC”并应用“model-view-controller”标签,在我看来,并不意味着“将这个Console应用程序代码转换为MVC”。如果是这样,请OP自由地将那个短语编辑到问题中。 - CodeCaster
@Scott 话虽如此,由于现有回答已经假定了这一点,我已经添加了“asp.net-mvc”标签。(并且,在我看来,如果有人要回答一个处于这种状态的问题,他们应该编辑它使之更易读)。 - CodeCaster
2
下次从答案(或问题)复制代码时,请确保链接并给予适当的归属:https://dev59.com/KnA75IYBdhLWcg3wOWRd#3545253 - rene
1个回答

2

对于Process,您可以使用静态方法GetCurrentProcess。结果将放入性能计数器中。

在以下控制器中,性能计数器只创建一次,然后被重复使用。计时器保证在第一次调用之前已经过去了500毫秒。

public class PerformanceController : Controller
{
    static PerformanceCounter ramCounter;
    static PerformanceCounter cpuCounter;

    static Timer timer;
    static ManualResetEvent waiter = new ManualResetEvent(false);

    static Performance lastMeasure = new Performance(); // the Model (in Mvc)

    static PerformanceController()
    {
        // Get the current process
        using (var p = Process.GetCurrentProcess())
        {
            ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName);
            cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
        }
        // make sure some time has passed before first NextValue call
        timer = new Timer(s =>
        {
            waiter.Set();
        }, null, 500, Timeout.Infinite);

        // clean-up
        AppDomain.CurrentDomain.DomainUnload += (s, e) => {
            var time = (IDisposable)timer;
            if (time != null) time.Dispose();
            var wait = (IDisposable)waiter;
            if (wait != null) wait.Dispose();
            var rc = (IDisposable)ramCounter;
            if (rc != null) rc.Dispose();
            var cc = (IDisposable)cpuCounter;
            if (cc != null) cc.Dispose();
        };
    }

    private static  Performance GetReading()
    {
        // wait for the first reading 
        waiter.WaitOne();
        // maybe cache its values for a few seconds
        lastMeasure.Cpu = cpuCounter.NextValue();
        lastMeasure.Ram = ramCounter.NextValue();
        return lastMeasure;
    }

    //
    // GET: /Performance/
    public ActionResult Index()
    {
        return View(GetReading());
    }
}

性能模型非常简单:
public class Performance
{
    public double Ram { get; set; }
    public double Cpu { get; set; }
}

以下视图完成了实现。
@model MvcApplication1.Models.Performance
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div><span>Ram</span><span>@Model.Ram</span> </div>
<div><span>Cpu</span><span>@Model.Cpu</span> </div>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接