15

Possible Duplicate:
Finalize vs Dispose

Hi,

Recently I was quizzed in an interview about finalize and dispose. When is each one of them used and how is Garbage Collector related to them. Kindly share links to enlighten more on the topic.

Kindly share ...

Thanks in advance.

Community
  • 1
  • 1
HotTester
  • 5,620
  • 15
  • 63
  • 97
  • You didn't get the job offer then? It is covered in any book about C# or the CLR. – Hans Passant Sep 05 '10 at 08:55
  • I answered the question but what puzzled me when the interviewer asked what happens when we explicitly implement Dispose, what is the role of GC and where does the Finalize stands ? – HotTester Sep 05 '10 at 09:01

2 Answers2

17
  1. Finalize: undeterministic nondeterministic destructor/finalizer called automatically by the Garbage Collector when there are no more references to this instance.
  2. Dispose: deterministically called by the developer on an object implementing IDisposable to free resources.
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    I think "nondeterministic" is the right word here. – Steven Sudit Sep 05 '10 at 08:55
  • @Steven, my English is very poor. Thanks for pointing this out. – Darin Dimitrov Sep 05 '10 at 08:57
  • So if the developer implements IDisposable then would Finalize still be called by the GC ? – HotTester Sep 05 '10 at 08:57
  • Yes it will when the object falls out of scope and when the CLR decides that it is a good time to perform a GC. – Darin Dimitrov Sep 05 '10 at 08:58
  • No, your English was completely understandable. I was just being picky about exactly the right word. – Steven Sudit Sep 05 '10 at 09:02
  • @Steven, no you are not picky, you are precise, it is important to use right words, programming is a precise science. – Darin Dimitrov Sep 05 '10 at 09:03
  • Note that the GC doesn't call the finalizer when there are no more references to the object, but when it actually has tried to dispose the object the first time. – Guffa Sep 05 '10 at 09:11
  • "So if the developer implements IDisposable then would Finalize still be called by the GC " - if the developer implements the usual IDisposable pattern (see MSDN), then the Dispose method will call GC.SuppressFinalize, which will prevent the system from calling the Finalizer. – Joe Sep 05 '10 at 14:58
17

Finalizers are run by the Garbage Collection before an object that is eligible for collection is reclaimed. Dispose() is meant for cleaning up unmanaged resources, like network connections, files, handles to OS stuff, &c. It works best in conjunction with the using block where the compiler makes sure that Dispose() will be called immediately once you are done with an object – and also ensures that you cannot work with the object anymore once it's disposed.

Note that finalizers don't have to run, so relying on that can be dangerous:

What this means for you: Your programs cannot rely on finalizers keeping things tidy. Finalizers are a safety net, not a primary means for resource reclamation. When you are finished with a resource, you need to release it by calling Close or Disconnect or whatever cleanup method is available on the object. (The IDisposable interface codifies this convention.)

Careful also with the precise time when an object becomes eligible for collection. Read the article linked above – it's neither scope (a weird word which has noting to do with the lifetime of an object – it's “the region of program text in which it is legal to refer to [a named entity] by its unqualified name.”) nor is it strictly reference counting as an object can become eligible for collection even before the last reference to it goes away.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 2
    I would like to add that Finalizers should be used with care. When you add a finalizer to your objects you are causing the runtime to hold onto the object for at least two GC cycles. Also, inside the finalizer you are not guaranteed that any members of your object have not be reclaimed. So accessing them may result in runtime exceptions. – taylorjonl Nov 25 '12 at 21:29