如何在运行时从代码中获取应用程序 "所有堆中的字节数 "性能计数器?

问题描述 投票:0回答:1

我试图在运行时获取特定的应用程序 "所有堆中的字节数 "性能计数器,计数器类别是".NET CLR内存 "应用程序的实例呈现在性能计数器窗口中(见附件),但当我试图从c#代码中获取对象PerformanceCounter的值时,我得到一个错误。"Instance does not exist in the specified Category".

我的代码。

PerformanceCounter performaceCounter = new PerformanceCounter(".NET CLR Memory"
    , "# Bytes in all Heaps"
    , instanceName: "my app instance name"
);
c# visual-studio memory-leaks performancecounter
1个回答
0
投票

根据我的测试,我可以成功运行有关PerformanceCounter的代码。

试着使用 Process.GetCurrentProcess().ProcessName 替换你的 instance name.

你可以参考下面的代码。

  private void button1_Click(object sender, EventArgs e)
    {
        PerformanceCounter PC = new PerformanceCounter(".NET CLR Memory", "# Bytes in all heaps", Process.GetCurrentProcess().ProcessName);
        MessageBox.Show(PC.NextValue().ToString());
    }

我从这个实例中得到一些NextValue的信息。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.