Reputation: 433
What is the difference between System.GC.Collect
and Dispose()
?
Aren't they used for the same purpose, and when should each be used; what is the best practice?
Upvotes: 4
Views: 6483
Reputation: 172408
System.GC.Collect is called when all objects which are there in memory are to be collected by the Garbage Collector. Objects which are referenced in managed code are not considered for Garbage collection. This method is used to force the system to reclaim the available memory.
Dispose() is not part of GC but as better practice you can use this. It should only be used for objects which uses unmanaged resources like FileStream etc. It should release all the resources that it owns. When you know that certain resources will not be released by GC then you can use the Dispose() method.
The using statement ensures the correct use of IDisposable objects.
On a side note: The GC does not call Dispose, it calls the finalizer(which you should call from Dispose(false))
Also, to make ensure that resources are always released appropriately, a Dispose method should be callable multiple times without throwing an exception.
MSDN says:
"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. "
Check this blog:-
GC.Collect() asks the system to perform a collection "now". You shouldn't mess with this; the system usually has a much better idea than you do of when collection is necessary.
So is the best practice:
You can use the using block or as you mentioned use the Dispose method.
Why should you use IDisposable and Dispose?
MSDN says
In many cases it is possible for objects that would otherwise always need to be finalized to avoid that cost by implementing the IDisposable interface. This interface provides an alternative method for reclaiming resources whose lifetime is well known to the programmer, and that actually happens quite a bit. Of course it's better still if your objects simply use only memory and therefore require no finalization or disposing at all; but if finalization is necessary and there are many cases where explicit management of your objects is easy and practical, then implementing the IDisposable interface is a great way to avoid, or at least reduce, finalization costs.
Also check this article on Improve garbage collector performance using finalize/dispose pattern
Upvotes: 5
Reputation: 32671
The purpose of GC.Collect is to to tell the Garbage collector that there are some objects in the memory that can be collected and it is the right time to collect. Though, as a rule of thumb you should leave that to GC itself. It is designed to do this job.
If there are some resources in your object that you think GC won't take care of. You should implement Dispose and take care of them yourself. You need to call Dispose Explicitly to dispose the resources you want. And if you are implementing IDisposable than you can also do that by the Using statement.
Upvotes: 6
Reputation: 38130
Best practice is to never need to call GC.Collect
, and to call Dispose
on all IDisposable
objects when you're done with them.
Upvotes: 6