C#: Accessing PerformanceCounters for the ".NET CLR Memory category"

Posted by Mads Ravn on Stack Overflow See other posts from Stack Overflow or by Mads Ravn
Published on 2011-01-16T13:51:59Z Indexed on 2011/01/16 13:53 UTC
Read the original article Hit count: 356

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET