I've been hearing advices about putting codes to handle unmanaged resources in both finalizer and Dispose() method. What I don't understand is that since finalizers are called when GC occurs so we could technically assume that it gets called all the time. In that case why bother disposing an object? Am I missing something?
-
4Why did that question get downvoted? Unless it's a duplicate, it's a really good question to ask. – Max Yankov Nov 11 '13 at 09:35
-
1Agree @golergka, Also why close as too broad? Henk has managed to sum the answer in a single sentence. Some people are far too trigger happy. – Liam Nov 11 '13 at 09:36
-
3@golergka please explain why this is a "really good question"? Given the popularity of .NET, this question is bound to be asked before (and it has been, hence I close-voted with a duplicate). It shows no research effort or understanding of the subject. – CodeCaster Nov 11 '13 at 09:38
-
@CodeCaster I mentioned that it could be a duplicate, I just didn't check for that. But I don't agree with "no research effort" part. Research effort is relative; and I think that with requirements as high as you show in your approach, SO would have no newbie questions whatsoever — as all of them could be solved by reading documentation. – Max Yankov Nov 11 '13 at 10:18
-
I don't see that as a problem. – CodeCaster Nov 11 '13 at 10:19
-
@CodeCaster I do. I think that SO should be a welcoming place for newbies in programming or new members that don't know community rules; it is true that every one of us could just RTFM and not waste each other's time, but if you feel that way, you can just not waste your personal time on answering newbie questions. – Max Yankov Nov 11 '13 at 10:21
-
So you're advocating a place where anyone can just dump their question without trying to solve it themselves or even reading the documentation? That is not maintainable on the long run and that is [not how SO works](http://stackoverflow.com/questions/how-to-ask). – CodeCaster Nov 11 '13 at 10:27
2 Answers
In that case why bother disposing an object?
Because you don't have control over when a finalizer runs. It could well be that the GC runs too in-frequent and a program that relies on finalizers alone could crash for lack of resources (filehandles, db-connections).
The best-practice (Disposable pattern) uses finalizers as the backup-plan, quite often the execution of a finalizer is considered a bug that needs fixing.

- 263,252
- 30
- 330
- 514
-
or *if* a finalizer runs. Someone link the blog post I'm thinking about :) – Rotem Nov 11 '13 at 09:34
-
1There it is http://blogs.msdn.com/b/oldnewthing/archive/2010/08/09/10047586.aspx – Rotem Nov 11 '13 at 09:35
-
Yes, correct, it's a corner case, but it goes to show how little we can rely on finalizers to implement resource release logic. – Rotem Nov 11 '13 at 09:37
Quoting from MSDN http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
Although finalizers are effective in some cleanup scenarios, they have two significant drawbacks:
The finalizer is called when the GC detects that an object is eligible for collection. This happens at some undetermined period of time after the resource is not needed anymore. The delay between when the developer could or would like to release the resource and the time when the resource is actually released by the finalizer might be unacceptable in programs that acquire many scarce resources (resources that can be easily exhausted) or in cases in which resources are costly to keep in use (e.g., large unmanaged memory buffers).
When the CLR needs to call a finalizer, it must postpone collection of the object’s memory until the next round of garbage collection (the finalizers run between collections). This means that the object’s memory (and all objects it refers to) will not be released for a longer period of time.

- 1,637
- 1
- 14
- 27