Pages

Tuesday, July 6, 2010

Dispose method and Finalize method

Finalize Method: As .net provides a great feature of memory management which handles all kind of allocations and de-allocations automatically and for de-allocating it uses Garbage Collector. So in .NET Garbage collector does almost all clean up activity for your objects. But all the process that can happen for de-allocation is applicable only that are managed by CLR or that are constructed according to the .Net but those object that are not managed by .Net CLR are un-managed codes/objects can not be de-allocated directly by GC So these unmanaged resources (ex: - Windows API created objects, File, Database connection objects, COM objects etc) that are outside of the scope of .NET framework are needed to be cleaned explicitly. For these types of objects .NET framework provides Object.Finalize method which can be overridden for our own code execution within this function and which can be used to clean up code for un-managed resources. This method is used in VB.Net which is called by CLR when Garbage Collector destroys an object. While in C# destructors are there for the same purpose.

Dispose Method: .NET provides “Finalize” method in which we can clean up our resources. But relying on this is not always good. This is because it is only called by the GC and you can't determine when it will be called (even though you can force it to be called, but that's not recommended).So the best is to implement “Idisposable” interface and implement the “Dispose” method where you can put your clean up routines.

But benefit of finalize over Dispose is that finalize is executed itself and not required to be called while Dispose is required to be called. So in case we require to execute dispose itself then we can call it within finalize which is executed by CLR while destroying the object. But in case we define call dispose this way also developer called dispose too at somewhere then call dispose can occur twice that can throw exception or some issue so to avoid call twice we can suppress the call to finalize after dispose by using GC.SuppressFinalize within dispose function some in that case if dispose is not called then it will be executed with finalize while if its called then suppressfinalize call will suppress the call to finalize method. Below is the sample code of the pattern. This is the
best way we do clean our unallocated resources and yes not to forget we do not get the hit
of running the Garbage collector twice in this case.


Note:- It will suppress the finalize method thus avoiding the two trip.
Publish Post


Public Class ClsTesting
Implements IDisposable
Public Overloads Sub Dispose()Implements IDisposable.Dispose
' write ytour clean up code here
GC.SuppressFinalize(Me)
End Sub
Protected Overrides Sub Finalize()
Dispose()
End Sub
End Class

Abstract Class And Interface Class

Interface Class: Its some kind of contract between developer/Class implementation and real world/caller of class object that commits what kind of the provisions will be their that can be accessed from the class implementation and also implementation is bound to provide the implementation of this contract.

Abstract Class: Its also define some kind of contract that a implementing class signs and bound to implement all the abstract functions for which no implementation is provided in the abstract class. So outer world of implementing class have knowledge of all the provisions and availability of functionality that can be called directly from the object of implementer class.

Benefit of Interface over Abstract class: As from the definition of the abstract class question arises that when everything that an Abstract class enforce or provide then why concept of Interface still exists the most well known answer of this question comes while an implementing class comes to the situation when it has to implement more than one kind of contracts to the outer world which makes the requirement of multiple inheritance which is not possible in case of abstract class as an implementer class can not implement more than one abstract class but it can implement more than one Interface.

Benefit of Abstract class Over Interface: Both seems to be related to contract between object calling and class providing implementation of interface. Also in case of existence of interface class an additional benefit it also facilitate with the feature of multiple inheritance then why abstract class exists. Answer to this query comes from abstract class's another feature that says if some of the implementation that is known and predefine also is common and usable in derived classes too then in case of abstract class we can implement this functionality in abstract class itself that will be available with all the derived classes which is not possible with usage interfaces.

If there is any other difference/benefit for these then I request to viewers to add their comments that can be beneficial for other viewers too.