Thursday, October 28, 2010

What is the difference Between Finalise and dispose methods

Few quick points on Finalize() and Dispose() in C#:

1. Finalize() is the C# equivalent of destructor ~Object() syntax in C#. In VB.Net you implement the Finalize() by overriding it. But, in C# the compiler translates the destructor to a Finalize() method.

2. Finalize() can NOT be overridden or called in C#.

3. Since, Finalize() is called by the Garbage Collector, it is non-deterministic.

4. Dispose() has to be implemented in classes implementing IDispose interface.

5. Its the right place for freeing-up unmanaged resources like file, handles, and connections etc.

6. Dispose() method is called explicitely in the code itself.

7. Dispose() method is automatically called (for objects which implement IDispose), when used in a "using" statement.

Finalize is the destructor which is normally used. Finalize is called automatically when the .NET runtime determines that the object is no longer being used.

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.

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.


Following the difference between Dispose and Finalize method,

1>CLR uses the Dispose and Finalize methods for performing garbage collection of runtime objects of .Net applications.

2>Clr has a Garbage Collector(GC) which periodically checks for unused and unreferenced objects in Heap.It call Finalize() method to free the memory used by such objects.

3>Dispose is another method which is invoked by Garbage Collector to release the memory occupied by an object.Dispose method needs to be explicitly called in code for removing an object from Heap.

4>Dispose method can be invoked only by the classes that IDisposable interface.

No comments:

Post a Comment