#738 – You Shouldn’t Explicitly Force Garbage Collection

For purposes of understanding what happens when objects get garbage collected, you can force garbage collection to happen by calling the GC.Collect method.  Calling this method in production code is almost always a bad idea.

The garbage collector is finely tuned for maximum performance and when you call GC.Collect explicitly, you disturb that tuning and make the garbage collector less efficient.

When you feel that you need to call GC.Collect in order to explicit release objects, it’s generally a sign of poor design in some area of your code.  (E.g. Not making proper use of a Dispose method on an object when you are done using it).


About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #738 – You Shouldn’t Explicitly Force Garbage Collection

  1. Pingback: #937 – Forcing a Garbage Collection | 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: