C#:访问".NET CLR内存类别"的PerformanceCounters

Mad*_*avn 9 .net c# memory performancecounter

我正在尝试使用PerformanceCounter类通过C#访问位于".NET CLR内存类别"中的性能计数器.但是,无法使用我期望的正确类别/计数器名称来实例化类别

new PerformanceCounter(".NET CLR Memory", "# bytes in all heaps", Process.GetCurrentProcess().ProcessName);
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下代码循环遍历类别和计数器

string[] categories = PerformanceCounterCategory.GetCategories().Select(c => c.CategoryName).OrderBy(s => s).ToArray();
string toInspect = string.Join(",\r\n", categories);

System.Text.StringBuilder interestingToInspect = new System.Text.StringBuilder();
string[] interestingCategories = categories.Where(s => s.StartsWith(".NET") || s.Contains("Memory")).ToArray();
foreach (string interestingCategory in interestingCategories)
{
    PerformanceCounterCategory cat = new PerformanceCounterCategory(interestingCategory);
    foreach (PerformanceCounter counter in cat.GetCounters())
    {
        interestingToInspect.AppendLine(interestingCategory + ":" + counter.CounterName);
    }
}
toInspect = interestingToInspect.ToString();
Run Code Online (Sandbox Code Playgroud)

但找不到任何似乎匹配的东西.是不可能从CLR中观察这些值,或者我做错了什么.

重要的是,环境是在64位Windows 7机器上运行的.NET 4.0.

Ole*_* D. 6

您需要以管理员身份运行程序才能访问.NET CLR内存类别.

使用powershell尝试这个简单的测试:

以管理员身份运行时

[Diagnostics.PerformanceCounterCategory] ​​::存在(".NET CLR Memory")

真正

没有管理权限时运行:

[Diagnostics.PerformanceCounterCategory] ​​::存在(".NET CLR Memory")

  • 这是不正确的.拥有管理员访问权限就足够了,但您不需要*管理员访问权限.请参阅http://stackoverflow.com/a/20174576/834521:将自己添加到组*Performance Monitor Users*,然后注销并登录. (2认同)