Best Practice for Forcing Garbage Collection in C#

The best practise is to not force a garbage collection.

According to MSDN:

“It is possible to force garbage
collection by calling Collect, but
most of the time, this should be
avoided because it may create
performance issues. “

However, if you can reliably test your code to confirm that calling Collect() won’t have a negative impact then go ahead…

Just try to make sure objects are cleaned up when you no longer need them. If you have custom objects, look at using the “using statement” and the IDisposable interface.

This link has some good practical advice with regards to freeing up memory / garbage collection etc:

http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx

Leave a Comment