skip to main
|
skip to sidebar
Tips
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:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
Blog Archive
►
2013
(7)
►
July
(1)
►
June
(5)
►
January
(1)
►
2012
(98)
►
December
(7)
►
November
(9)
►
October
(25)
►
September
(4)
►
August
(17)
►
July
(6)
►
May
(8)
►
April
(12)
►
March
(3)
►
February
(5)
►
January
(2)
►
2011
(60)
►
December
(3)
►
November
(11)
►
October
(12)
►
August
(6)
►
July
(7)
►
June
(6)
►
May
(7)
►
March
(1)
►
February
(4)
►
January
(3)
►
2010
(98)
►
November
(2)
►
October
(8)
►
September
(8)
►
August
(2)
►
July
(1)
►
June
(10)
►
May
(9)
►
April
(31)
►
March
(20)
►
February
(7)
►
2009
(15)
►
November
(1)
►
June
(1)
►
May
(1)
►
April
(1)
►
March
(5)
►
February
(6)
►
2008
(142)
►
November
(3)
►
October
(14)
►
September
(4)
►
August
(6)
►
July
(19)
►
June
(9)
►
May
(12)
►
April
(8)
►
March
(13)
►
February
(22)
►
January
(32)
▼
2007
(250)
►
December
(17)
►
November
(19)
►
October
(1)
►
September
(11)
►
August
(25)
►
July
(25)
▼
June
(33)
Threads in .NET
Difference between finalize and dispose
Difference between Bitmap and meta files
Mastering VC++
VC++ Win32 Dialog box
Difference between Panel and GroupBox classes?
Marshalling Problem Practically applied
Marshalling Problem :(funny )
Address Increment with the help of sizeof() operator
can't invoke a non-const member function on a cons...
two input surfaces are same size for (2 In 1Out) D...
Lvalues and RValues
I implemented the StrConcat() fn in a VC++(unmanag...
DirectX transform is also a BHO ( Browser helper O...
Browser Helper Objects
No title
mutable keyword
Real use of BitFields
How to call Unmanaged DLL from C# application ?
Operator Keywords
Combo box problem
__FILE__, __LINE__ and __cplusplus macro
constant qualifier testing
cpp tips
Inpirational words
DirectX transform filters working mechanism
Name mangling
Different Style of Const in a fn
Rotation filter
sample applications for Pointer to derived types a...
sample applications for Pointer to derived types a...
Object conversion
Cpp Exceptions
►
May
(35)
►
April
(37)
►
March
(47)
►
2006
(5)
►
December
(1)
►
November
(1)
►
September
(3)
Links
Google News
Edit-Me
Edit-Me
No comments:
Post a Comment