C#: Accessing PerformanceCounters for the ".NET CLR Memory category"
- by Mads Ravn
I'm trying to access the performance counters located in ".NET CLR Memory category" through C# using the PerformanceCounter class. However a cannot instantiate the categories with what I would expect was the correct category/counter name
new PerformanceCounter(".NET CLR Memory", "# bytes in all heaps", Process.GetCurrentProcess().ProcessName);
I tried looping through categories and counters using the following code
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();
But could not find anything that seems to match. Is it not possible to observe these values from within the CLR or am I doing something wrong.
The environment, should it matter, is .NET 4.0 running on a 64-bit windows 7 box.