# Timing how long garbage collection is taking in C#

I'm concerned about how much time my application could be spending during garbage collection so I'm trying to figure out how to add some code to instrument this.

Based on some examples:

I've come up with the code below. I'm see GC taking around 200 ms locally. Does anyone know if the code below is reasonable? Or is there a better way?

class Program
{
public static void Main(string[] args)
{
var done = false;

var pollGC = new Action(() =>
{

Stopwatch gcTimer = new Stopwatch();

while (!done)
{
// Check for a notification of an approaching collection.
{
gcTimer.Restart();
}

// Check for a notification of a completed collection.
s = GC.WaitForFullGCComplete();
{
Console.WriteLine("GC has finished in {0} ms", gcTimer.ElapsedMilliseconds);
}

}

Console.WriteLine("Finished monitoring GC");

});

var doWork = new Action(() =>
{
while (!done)
{
try
{
}
catch (OutOfMemoryException)
{
}
}
});

Console.WriteLine(GCSettings.IsServerGC);

done = true;

}
}

• Who's marking this as "broken code"? The OP says it seems to be working properly. – Snowbody May 6 '15 at 20:10
• @Snowbody That code is broken. Will explain in answer. – Zer0 May 8 '15 at 22:36

Your design won't work. You're starting a timer using GC.WaitForFullGCApproach. But this method only indicates that a Full GC is imminent (not starting). It actually allows you some time to prepare for it and force a GC yourself using GC.Collect. Your numbers won't be accurate.

Why not try using the memory performance counters?

Alternatively you could do this:

while (!done)
{
{
//TODO - Do GC preparation here if need be
Console.WriteLine("Full GC is imminent.  Starting a GC manually.");
gcTimer.Restart();
GC.Collect();
gcTimer.Stop();
Console.WriteLine("GC has finished in {0} ms", gcTimer.ElapsedMilliseconds);
GC.WaitForFullGCComplete();
}
}

• None of those performance counters report how long garbage collection is taking (as far as I can tell). – Darragh May 11 '15 at 16:16
• % time in GC is effectively how long it's taking. But if you need absolute numbers I would suggest doing a call to GC.Collect in response to an approach notification and using a Stopwatch to measure just that. – Zer0 May 11 '15 at 18:50
• I'd rather not force a garbage collection myself...as I'm not trying to alter the 'normal' GC behavior...I'm just trying to monitor it. But as there doesn't seem to be any other way, I'll mark this as the 'answer'. – Darragh May 13 '15 at 12:12

For continuous monitoring I agree with Zer0 to look at the % time spent in GC performance counters.

However for a session based analysis a more informative approach would be to collect the ETW traces from .NET. You can see here for the different GC related events, GCStart and GCEnd would most likely be relevant for you.

view the .NET documentation for Garbage Collection ETW Events

You can specify the GC Collect Only option exists which would be very useful here.

The GC Collect Only Checkbox - Turns off all providers except those that describe garbage collections. Thus even the GC allocation sampling is turned off. This mode logs even less data than GC Only, and thus can collect data over an every longer period of time (say a day) in reasonable size (say 200 meg). The /GCCollectOnly command line option achieves the same effect.