C# .NET - Difference between Finalize and Dispose

Asked By kunal mehta on 20-Aug-08 11:00 AM

Interview Question

Difference between Finalize and Dispose

Difference between Finalize and Dispose - Kalit Sikka replied to kunal mehta on 20-Aug-08 11:08 AM

Class instances often encapsulate control over resources that are not managed by the runtime, such as window handles (HWND), database connections, and so on. Therefore, you should provide both an explicit and an implicit way to free those resources. Provide implicit control by implementing the protected Finalize Method on an object (destructor syntax in C# and the Managed Extensions for C++). The garbage collector calls this method at some point after there are no longer any valid references to the object. In some cases, you might want to provide programmers using an object with the ability to explicitly release these external resources before the garbage collector frees the object. If an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used. To provide explicit control, implement the Dispose method provided by the IDisposable Interface. The consumer of the object should call this method when it is done using the object.

Dispose can be called even if other references to the object are alive. Note that even when you provide explicit control by way of Dispose, you should provide implicit cleanup using the Finalize method. Finalize provides a backup to prevent resources from

permanently leaking if the programmer fails to call Dispose.

check this... - Santhosh N replied to kunal mehta on 20-Aug-08 12:54 PM

finalize() method is automatically invoked by .net runtime environment to destroy objects which r out of scope. it works as a garbage collector. u donot give body to finalize() and u can not explicitly calll it.

but in case of dispose(), it is used to close database connections etc.
to give body to dispose method u have to implement IDisposable interface.

if u go to autogenerated code of form disegener there u can see dispose method been overloaded by form1 class.

u wouldn't see that form1 implement IDisposable interface but its the form class which implements this interface.

reply - Binny ch replied to kunal mehta on 20-Aug-08 12:59 PM

 

Design Pattern : If your classes use unmanaged resources, you need to implement both Dispose & Finalize. Dispose() is called by user code, that is, the code that is using your class.
Finalize/Destructor cannot be called by User code, it's called by Garbage Collector

Finalize : Is a destructor, called by Garbage Collector when the object goes out of scope. 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 : 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. Overriding Dispose() provides a way for the user code to free the unmanaged objects in your custom class.

As an aside, here's how the GC works:
The garbage collector keeps track of objects that have Finalize methods, using an internal structure called the finalization queue. Each time your application creates an object that has a Finalize method, the garbage collector places an entry in the finalization queue that points to that object. The finalization queue contains entries for all the objects in the managed heap that need to have their finalization code called before the garbage collector can reclaim their memory.

Implementing Finalize methods or destructors can have a negative impact on performance and you should avoid using them unnecessarily. Reclaiming the memory used by objects with Finalize methods requires at least two garbage collections. When the garbage collector performs a collection, it reclaims the memory for inaccessible objects without finalizers. At this time, it cannot collect the inaccessible objects that do have finalizers. Instead, it removes the entries for these objects from the finalization queue and places them in a list of objects marked as ready for finalization. Entries in this list point to the objects in the managed heap that are ready to have their finalization code called. The garbage collector calls the Finalize methods for the objects in this list and then removes the entries from the list. A future garbage collection will determine that the finalized objects are truly garbage because they are no longer pointed to by entries in the list of objects marked as ready for finalization. In this future garbage collection, the objects' memory is actually reclaimed.


reply - Binny ch replied to kunal mehta on 20-Aug-08 01:00 PM

Dispose() is called by the user of an object to indicate that he is finished with it, enabling that object to release any unmanaged resources it holds.

Finalize() is called by the run-time to allow an object which has not had Dispose() called on it to do the same. However

Dispose() operates determinalistically, whereas there is no guarantee that Finalize() will be called immediately when an object goes out of scope - or indeed at all, if the program ends before that object is GCed - and as such Dispose() is generally preferred

Difference between dispose and finalize:
As you know VB.net object are garbage collected unlike in VB where you can be sure
that the terminate event will run immediatley after the object's referece count
becomes zero.
Thus, you can place all resource cleanup code in a Finalize method. Note that at
runtime this may be called when garbage collector gets around to destroying these
objects.
In VB.net there is a work around to this.Microsoft actually recommends using "Dispose"
method for this.
Class CResourcewrapper
Public Sub dispose()
' call dispose on any contained managed resources
' free the unmanaged resources
' suprress finalization
GC.SuppressFinalize(Me)
End Sub
Protected overrides Sub finalize()
' do not call dispose any contained managed resources
' free the unmanaged resources
' call base class finalize method
MyBase.Finalize()
End Sub
End Class

There's a lot of copying and pasting going on, - Peter Bromberg replied to kunal mehta on 20-Aug-08 07:17 PM
but I think the crux of the issue is that Finalize is only called by the Garbage Collector, whereas Dispose is the method that you call.
Difference between Finalize and Dispose - Sagar P replied to kunal mehta on 21-Aug-08 12:22 AM

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.

Verify these links for more ideas..

http://kyapoocha.com/aspnet-interview-questions/what-is-the-difference-between-finalize-and-dispose/

http://www.allinterview.com/showanswers/28556.html

Best Luck!!!!!!!!!!!!!!!!!
Sujit.

Overview - Atul Shinde replied to kunal mehta on 21-Aug-08 01:22 AM

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.

Verify these links for more ideas..

http://kyapoocha.com/aspnet-interview-questions/what-is-the-difference-between-finalize-and-dispose/

http://www.allinterview.com/showanswers/28556.html

Finalize and Dispose - pravin kumar S replied to kunal mehta on 21-Aug-08 02:08 AM

Dispose : 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. Overriding Dispose() provides a way for the user code to free the unmanaged objects in your custom class.

Finalize : Is a destructor, called by Garbage Collector when the object goes out of scope. 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.

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.

Issue... - Atul Shinde replied to kunal mehta on 22-Aug-08 01:03 AM

Class instances often encapsulate control over resources that are not managed by the runtime, such as window handles (HWND), database connections, and so on. Therefore, you should provide both an explicit and an implicit way to free those resources. Provide implicit control by implementing the protected Finalize Method on an object (destructor syntax in C# and the Managed Extensions for C++). The garbage collector calls this method at some point after there are no longer any valid references to the object. In some cases, you might want to provide programmers using an object with the ability to explicitly release these external resources before the garbage collector frees the object. If an external resource is scarce or expensive, better performance can be achieved if the programmer explicitly releases resources when they are no longer being used. To provide explicit control, implement the Dispose method provided by the IDisposable Interface. The consumer of the object should call this method when it is done using the object.

Dispose can be called even if other references to the object are alive. Note that even when you provide explicit control by way of Dispose, you should provide implicit cleanup using the Finalize method. Finalize provides a backup to prevent resources from

permanently leaking if the programmer fails to call Dispose.

vijay replied to kunal mehta on 06-Jan-11 12:24 PM
Pls................send one example how to implement  dispose() and finalize () methods in .net