What is the Difference B/W Finalize() and Dispose() in .Net?
Answers were Sorted based on User's Feedback
Answer / sreeram pavan
Both are ways to destroy the object. By object i mean when
you have some unmanaged resources used in your class, you
have to make sure that you write the logic to destroy them
in the finalize() method. But this is the implicit way of
destroying the unmanaged resources, as finalize is called by
garbage collector when it find thats there is no reference
to this object from the stack.
There is an explicit way to destroy the unmanaged resources.
That is by implementing IDisposable interface. By
implementing this interface, ie you have to write the code
to destroy the resource in Dispose() method, you can call
the object to destroy itself when ever required in your code.
Is This Answer Correct ? | 51 Yes | 5 No |
Answer / karna
when we write the destructor in c#,the runtime replaces the
destructor with the finalize method in IL code,so we dont
know at what time the destructor is called by the garbage
collector.so the finalizer is non deterministic.if we want
to do the garbage collection for unmanages resources,we
have to implement our own destructor or finlizer
method.but,the runtime will take two round trips to remove
those objects from the heap.if runtime calls the own
destructor,it will take onely one round trip.
to remove the unmanaged resources without the performance
issues,we have to inherit the System.Idisposable interface
and implement the Dispose() method.here one problem is
there,we have to write a code in a way that,the dsipose
method must and should execute.
for that reason we have to keep the dispose methos for the
objcets in the finally block.
problem with the dispose is that,some times because of
some problmes dispose() methos may not execute.
so the good practice is to use both to achieve the desied
performance.
Is This Answer Correct ? | 20 Yes | 3 No |
Answer / vivek jagga
The GC call the Finalize() function automatically to destroy
the object called implicit destroy. when you want to destroy
a objects that you think no longer need and free it from
memory, then we will use the dispose function. For better
performance we will use the dispose function explicitly.
Is This Answer Correct ? | 21 Yes | 5 No |
Answer / prasanna
.NET provides "finalize" method in which clean up our
resources.but this is always not good so the best is to
implement interface and implement the "Dispose" method
where you can put your clean up routines
Is This Answer Correct ? | 23 Yes | 8 No |
Answer / rahupathi
Finalize and dispose are used to clean the objects from
memory. But Finalize will be called based on the GC. If you
finalize any object that will be moved to Finalize queaue.
When the time of the round trip of the GC. It will remove
the objects from Finlize queaue.So,it will not destroy the
object immediately. Unless dispose method will remove the
object immediatley without considering the GC.
Is This Answer Correct ? | 16 Yes | 3 No |
Answer / purushottam kumar, mcad, mcsd
Finalize :
1.Finalize() is called by the runtime
2.Is a destructor, called by Garbage Collector when the
object goes out of scope.
3.Implement it when you have unmanaged resources in your
code, and want to make sure that these resources are freed
when the Garbage collection happens.
Dispose :
1.Dispose() is called by the user
2.Same purpose as finalize, to free unmanaged resources.
However, implement this when you are writing a custom class,
that will be used by other users.
3.Overriding Dispose() provides a way for the user code to
free the unmanaged objects in your custom class.
Is This Answer Correct ? | 9 Yes | 0 No |
Answer / nitin kumar
Dispose() Method is used to release Unmanage Resources.
When Unmanage Resources are no longer in use and Referenced
and is called Manually.
and
Finalize() Method is similar to Dispose Method But it is
called by the Garbage Collector.
Is This Answer Correct ? | 9 Yes | 6 No |
Answer / rajkumar
If we want to delete resources(objects) those are not using,
u should not worry about that GC implicit y call Finalize()
method and remove all such object but if u want to delete
object forcefully(The large obj u want to delete after
completely task) then u can explicity call Dipose() method.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / planet of coders
To getting a great answer please visit this URL : http://planetofcoders.com/difference-finalize-dispose/
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / m.sivakumar
Dispose() is called by as an indication for an object to release any unmanaged resources it has held.
Finalize() is used for the same purpose as dispose however finalize doesn’t assure the garbage collection of an object.
Dispose() operates determinalistically due to which it is generally preferred.
Is This Answer Correct ? | 2 Yes | 2 No |
What is Assembly?
3 Answers Inspira Technologies,
How do you trap errors in ASP and how do you invoke a component in ASP ?
Explain difference between dataset and datareader?
i have 3+ exp in .net? i am going interview now but they asked me do you know any TOOL? which tool will i study please refer me?
Is post back property in asp net?
Differentiate the session object and application object?
When using the Pager object, inorder to know which page to go, which property you have to set to grid?
What is caching? What are the different types of caching?
Can we have multiple worker process in an ASP.NET application? If so then how it has been handled by application? And who handles it?
0 Answers InfoAxon Technologies,
How Clustered Index and Non clustered index stored on SQL server?
Which method do you use to enforce garbage collection in .net?
When is the ViewState available during the page processing cycle ?