Proper use of the "% time in GC" performance counter

I am trying to get the performance counter "% time in GC" in the ".NET CLR Memory" category programmatically.

As described in PerfMon,

% Time in the GC is the percentage of elapsed time spent collecting garbage (GC) since the last GC cycle. This counter is usually an indicator of the work performed by the Garbage Collector on behalf of the application for collecting and compressing memory. This counter is updated only at the end of each GC and the counter value reflects the last observed value; this is not average.

Therefore, based on this description, I understand that I will have to periodically get a performance counter and independently calculate the average value.

The way I get a performance counter,

     string category = ".NET CLR Memory";
     string counter  = "% Time in GC";
     string instance = Process.GetCurrentProcess().ProcessName;
     PerformanceCounter gcPerf;

     // make sure the performance counter is available to query
     if (PerformanceCounterCategory.Exists(category) &&
         PerformanceCounterCategory.CounterExists(counter, category) &&
         PerformanceCounterCategory.InstanceExists(instance, category))
     {
        gcPerf = new PerformanceCounter(category, counter, instance);
     }

However, when I look at http://msdn.microsoft.com/en-us/library/xb29hack(v=vs.90).aspx , I understand that gcPerf.NextValue () will fetch and calculate the average value for you .

So what is the correct use of "% time in GC"? Is gcPerf.NextValue () called, returning the average since the method was last called?

+4
source share

Source: https://habr.com/ru/post/1541455/


All Articles