Consider the following method:
DoTimeIntensiveOperation()
{
var t = new Stopwatch();
foreach(var element in a_very_long_array)
{
DoATimeConsumingTask(element);
}
Console.WriteLine("Took me " + t.Elapsed);
return;
}
Do I need to call t.Stop()
before returning?
As far as I know, the garbage collector will destroy anything that doesn't have a reference chain going back to the main method. The only reference to the created Stopwatch
is t
, so when DoTimeIntensiveOperation
, t
will be freed and the Stopwatch
should be eligible for destruction. But does the fact that it's still "ticking" interfere with the GC?