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

.net, c#, memory, performancecounter

Solution

You need to run the program as Administrator to access .NET CLR Memory category.

Try this simple test, using powershell:

When running as Administrator

[Diagnostics.PerformanceCounterCategory]::Exists(".NET CLR Memory")

True

When running without administrative privileges:

[Diagnostics.PerformanceCounterCategory]::Exists(".NET CLR Memory")

False

Problem

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.

Original source

Related problems