Tuesday, June 26, 2007

Difference between finalize and dispose

Dispose is a method for realse from the memory for an object.

For eg:

.Dispose.

Finalize is used to fire when the object is going to release the memory.We can set a alert message to says that this object is going to dispose.


For Example if we are opening a file in our application,
this will be released in Dispose() method. Normally before releasing database connection, we have to close the database connection. we have

to place the code for Closing the file operation will be taken place in Finalize() method...

you can close open connections etc before the garbage collector takes over.

This code will be put in Finalize() method. The garbage collector does not close connections when the object gets destroyed.


In general, the Dispose pattern is used to release unmanaged resources in
a timely fashion. This allows you to do this in a deterministic fashion
- in other words, you have control over when they are released. The
Object.Finalize method is also used for the purpose of releasing
resources - but it is non-derministic. You have no control over when it
will be called by the GC. Further, implementing a Finalize method can
have an adverse affect on the performance of the GC because it takes two
passes of the GC to collect objects that override Finalize.

So, in general, if your using objects that manage unmanaged resources,
such as database connections, you implement IDisposable AND override
Finalize. This way, your covered if the client fails to call Dispose -
you know that your resources will then be released when the object is
GC'd. Of course, one you call Dispose - you don't need the finalize
method to be called by the GC and suffer an unnecessary performance
hit... So, generally you'll see the Dispose method implemented sort of
like this:

Public Sub Dispose()
' Do Clean up
System.GC.SupressFinalize(ME)
End Sub

This way, if the client does call dispose, then you don't suffer from
having implemented a Finalize because the object is removed from the
Finalize queue.


when you are writing a class that “owns” relatively scarce resources (such as SQL connections or file handles) which are not managed

by the .NET Common Language Runtime. Both Dispose() and Finalize() return resources by to a system — so if resources are controlled by the

CLR then the .NET automatic Garbage Collection mechanism will take care of the resources and you don’t really need Dispose() or Finalize() in

most situations. A Dispose() method is explicit, meaning you code Dispose() up and explicitly call it in your application or system code. A

Finalize() method is implicit, meaning you code Finalize() up but never actually call it — Finalize() is called behind the scenes by the .NET GC

mechanism. At this point you’ve told the majority of hiring manager what they want to hear. If prompted, you can peel away another layer of detail.

A scare-resource-using class with an explicit Dispose() method inside the class definition must inherit from the IDisposable interface. Typically

such a class will invoke a C# destructor which calls Dispose(), and will invoke the statement GC.SuppressFinalize(this) so that implicit automatic

garbage collection does not interfere with your explicit code. A scare-resource-using class with an explicit Finalize() method inside the class

definition is much more custom, but will usually invoke the statement base.Finalize() to bubble up all Finalize() calls through any inheritance

hierarchy.




Dispose type pattern in C# :
-----------------------------------------

// C#
public class Resource : IDisposable
{
private EmbeddedResource embedded;

public Resource()
{
Console.WriteLine("allocating resource");
embedded = new EmbeddedResource();
}

~Resource()
{
Dispose(false);
}

protected virtual void Dispose(bool disposing)
{
Console.WriteLine("release resource");
if (disposing)
{
embedded.Dispose();
}
}

public void Dispose()
{
Dispose(true);
}
}

No comments: