Possible Duplicate:
When is it acceptable to call GC.Collect?
From what I know the CLR does all this garbage collection for you, but is there a reason to manually call GC.Collect()
?
Is it for cases when you close a file, dispose of an image resource or an unmanaged resource, that you should call GC.Collect()
to quickly clean up the unused memory immediately?
Is it for cases when you close a file, dispose of an image resource or an unmanaged resource, that you should call GC.Collect() to quickly clean up the unused memory immediately?
No, not really. This is typically handled via IDisposable
.
There are very few reasons to call GC.Dispose()
directly, and doing so often causes a lot of harm, since it interferes with the internal heuristics of the GC in .NET. There are, however, rare occurrences when it's useful.
For example, if you have a operation that is a rare operation which uses a large object graph, and you know that your going to be "idle" afterwards, you might want to call GC.Collect
immediately afterwards to release the memory used by the objects. That being said, this is often still best left up to the system to handle.
For the most part, I've found the most useful scenario for GC.Collect()
is for debugging. It can help you guarantee that you don't have a leak, since it allows you to force a full Gen2 collection.
http://blogs.msdn.com/b/ricom/archive/2003/12/02/40780.aspx
http://blogs.msdn.com/b/ricom/archive/2004/11/29/271829.aspx
Right from the equine's oral orifice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With