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