Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

To implement Finalize or not 1

Status
Not open for further replies.

Becks25Not

Programmer
Jun 23, 2004
176
US
Hi All,

I have been reading up on performance tuning and have found conflicting information on implementing a Finalize sub. One source says don't do it, another says to. What are you thoughts?

Thanks,
Becca
 
Here's a great summary I ran into:
To surpass the disadvantage of finalizers another method is provided in .net framework i.e. Dispose. Using Dispose will not hurt your programs performance or kill the runtime time.

Differences between finalize and Dispose Methods.

Finalize method cannot be called explicitly, dotnet runtime calls this method implicitly to destruct the object. (In C#)
Dispose method Must be called explicitly at any time just like any other method. Contains the code to clean up the Unmanaged code accessed by the object.

No guarantee when the runtime executes the Fianlize method to destruct the object, though the object goes out of scope.

Dispose method Will be executed as soon as we call the method explicitly.

Since we cannot predict when the Finalize method is called, this type of collecting garbage is called non-deterministic finalization.

Since we dictate exactly when to collect garbage using Dispose method, this method is called deterministic finalization.

For a class to write the functionality of the Dispose method, the class must implement IDisposable interface.

So now you may ask me “why do we need finalize method when we have the Dispose method, which functions as good as a finalize method”. Here is the explanation for your doubt.
Assume that you had written a class in .net framework. This class uses a lot of unmanaged code and to clear the memory you had written a well functioning dispose method. So you will assume that everything is in place and will give the class to your friend to access.
Your friend in turn will access it. But he forgot to call the dispose method in the end. Then think what is going to happen……Memory Leakage. Why so, since your friend has used a lot of unmanaged code from your class and forgot to call the Dispose method and .net framework doesn’t know how to clean the unmanaged code.
To overcome such a case you will write your class with both dispose and finalize methods (finalize method will act as a backup method). In finalize method you will do nothing more than calling the dispose() method. In this case if your friend calls the dispose method, it will be well and good. If he forgets to call the dispose method, .net framework will call the finalize method while destructing the object. Eventually no memory leakage will be there.

One thing to note though, a form's Dispose method IS called automaticly.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Thanks Rick!! That does clear it up -- the problem with the Internet is that anyone can put stuff there!!
 
You should implement the IDisposable interface when your class contains obects that are unmanaged. There is no definitive list available, but good candidates are handles to files, handles to databases, network connections, etc.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top