I like to frequent on StackOverflow, and occasionally there is are those performance questions. They either focus on memory consumption, timing, or CPU utilization. Inevitably, a few reveal that they have scattered GC.Collect throughout their code in effort to keep memory consumption down.
I generally don’t like blanket statements, but “If you are using GC.Collect, you are doing something wrong” is a good things to keep in mind. I am hard pressed to think of scenarios where, in a production application, I would want to force a Garbage Collection. That’s what the CLR is for. It probably knows better than I do when garbage is a problem. There are plenty of reasons to not for a collection either, like forcing objects to promote to the next generation.
That got me thinking, where in the .NET Framework is GC.Collect used?
I searched the .NET Framework 4.0 GAC’s assemblies for calls to GC.Collect. Here is a diagram indicating my results (click for a larger view):
This really doesn’t come as a surprise to me, though there are a few odd balls out there.
In the next part, we’ll look at the top 50 NuGet packages to see which ones use GC.Collect.
If you have an interest in .Net Garbage collector, check this blog post about the new version of it in 4.5 : http://blogs.msdn.com/b/dotnet/archive/2012/07/20/the-net-framework-4-5-includes-new-garbage-collector-enhancements-for-client-and-server-apps.aspx