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.
您需要以管理员身份运行程序才能访问.NET CLR内存类别.
使用powershell尝试这个简单的测试:
以管理员身份运行时
[Diagnostics.PerformanceCounterCategory] ::存在(".NET CLR Memory")
真正
没有管理权限时运行:
[Diagnostics.PerformanceCounterCategory] ::存在(".NET CLR Memory")
假