Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between destructor, dispose and finalize method

I am studying how garbage collector works in c#. I am confused over the use of Destructor, Dispose and Finalize methods.

As per my research and understandings, having a Destructor method within my class will tell the garbage collector to perform the garbage collection in the way mentioned in the destructor method which cannot be called explicitly on the instances of the class.

The Dispose method is meant to provide the user to control the garbage collection. The Finalize method frees the resources used by the class, but not the object itself.

I am not sure if I understand it the right way. Please clarify the doubts. Any further links or guides are welcome.

like image 910
Victor Mukherjee Avatar asked Dec 21 '12 10:12

Victor Mukherjee


People also ask

What is the difference between Dispose () and Finalize ()?

The main difference between dispose() and finalize() is that the method dispose() has to be explicitly invoked by the user whereas, the method finalize() is invoked by the garbage collector, just before the object is destroyed.

What is difference between finalize () method in Java and destructor in C ++?

The main difference between finalize() method and destructor() in c++ is that the destructor() is always called when object goes out of scope but in java finalize() method called by garbage collector sporadically before garbage collector free the objects when no reference to that objects exists.

Is finalize a destructor or method?

Finalizers (historically referred to as destructors) are used to perform any necessary final clean-up when a class instance is being collected by the garbage collector. In most cases, you can avoid writing a finalizer by using the System. Runtime.

Why we need to implement Dispose method instead of destructor?

It is always recommended to use Dispose method to clean unmanaged resources. You should not implement the Finalize method until it is extremely necessary. At runtime C#, C++ destructors are automatically converted to Finalize method.


1 Answers

Destructor implicitly calls the Finalize method, they are technically the same. Dispose is available with objects that implement the IDisposable interface.

You may see : Destructors C# - MSDN

The destructor implicitly calls Finalize on the base class of the object.

Example from the same link:

class Car {     ~Car()  // destructor     {         // cleanup statements...     } } 

The Destructor's code is implicitly translated to the following code:

protected override void Finalize() {     try     {         // Cleanup statements...     }     finally     {         base.Finalize();     } } 

Your understanding for the Destructor is right:

From MSDN

The programmer has no control over when the destructor is called because this is determined by the garbage collector. The garbage collector checks for objects that are no longer being used by the application. If it considers an object eligible for destruction, it calls the destructor (if any) and reclaims the memory used to store the object. Destructors are also called when the program exits. It is possible to force garbage collection by calling Collect, but most of the time, this should be avoided because it may create performance issues.

like image 135
Habib Avatar answered Oct 20 '22 11:10

Habib