Correct use of “% Time in GC” performance counter

放肆的年华 提交于 2019-12-10 06:58:08

问题


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

According to the description in PerfMon,

% Time in GC is the percentage of elapsed time that was spent in performing a garbage collection (GC) since the last GC cycle. This counter is usually an indicator of the work done by the Garbage Collector on behalf of the application to collect and compact memory. This counter is updated only at the end of every GC and the counter value reflects the last observed value; its not an average.

So based on that description, my understanding is that I will have to get the performance counter periodically and calculate the average value myself.

The way I get the performance counter is

     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, my understanding is that gcPerf.NextValue() will do the sampling and calculate the average for you.

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

来源:https://stackoverflow.com/questions/23788101/correct-use-of-time-in-gc-performance-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!