Home > DeveloperSection > Forums > difference between dispose and finalize methods in c#?
Manoj Bhatt
Manoj Bhatt

Total Post:153

Points:1079
Posted on    January-24-2016 10:54 PM

 C# C#  .NET 
Ratings:


 1 Reply(s)
 276  View(s)
Rate this:
 I know about finalizer and dispose both are used for same task like to free unmanaged resources but have some differences.I want to know about its differences.Please give me a better solution.

thank you. 


Shiva Shukla
Shiva Shukla

Total Post:13

Points:91
Posted on    January-24-2016 11:01 PM

.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (For example, Windows API created objects, File, Database connection objects, COM objects, etc) is outside the scope of .NET framework we have to explicitly clean our resources. For these types of objects, .NET framework provides Object.Finalize method, which can be overridden and clean up code for unmanaged resources can be put in this section.
For example, lets say there are three objects: Object1, Object2, and Object3. Object2 has the finalize method overridden and remaining objects do not have the finalize method overridden.

Now when garbage collector runs for the first time it searches for objects whose memory has to free. He can see three objects but only cleans the memory for Object1 and Object3. Object2 it pushes to the finalization queue.

Now garbage collector runs for the second time. The garbage collector gets to know there are no objects to be released and then checks for the finalization queue and at this moment, it clears object2 from the memory.

So if you notice that object2 was released from memory in the second round and not first. That is why the best practice is not to write clean up Non .NET resources in Finalize method rather use the DISPOSE.

Dispose method belongs to ‘IDisposable’ interface. We had seen in the previous section how bad it can be to override the finalize method for writing the cleaning of unmanaged resources. So if any object wants to release its unmanaged code best is to implement IDisposable and override the Dispose method of IDisposable interface. Now once your class has exposed the Dispose method it is the responsibility of the client to call the Dispose method to do the cleanup.

Now here question arise why we use Dispose Method
To use or override dispose method, implement Idisposable interface in your class that comes under System namespace. The following example is a class that implements IDisposable interface for cleaning object of file.
  1. public class disposeexample: IDisposable  
  2. {  
  3.     // Flag: Has Dispose already been called?   
  4.     bool disposed = false;  
  5.     private FileStream filestream;  
  6.     private StreamReader streamreader;  
  7.     public string ReadFile()  
  8.     {  
  9.         try  
  10.         {  
  11.             filestream = new FileStream(Path.Combine(Environment.CurrentDirectory, "TextFile1.txt"), FileMode.Open, FileAccess.Read);  
  12.             streamreader = new StreamReader(filestream);  
  13.             return streamreader.ReadToEnd();  
  14.         }  
  15.         catch(Exception ex)  
  16.         {  
  17.             throw ex;  
  18.         }  
  19.         finally  
  20.         {  
  21.             streamreader.Close();  
  22.             filestream.Close();  
  23.             this.Dispose(true);  
  24.         }  
  25.     }  
  26.     protected virtual void Dispose(bool disposing)  
  27.     {  
  28.         if(disposed) return;  
  29.         if(disposing)  
  30.         {  
  31.             filestream.Dispose();  
  32.             streamreader.Dispose();  
  33.         }  
  34.         // Free any unmanaged objects here.   
  35.         //   
  36.         disposed = true;  
  37.     }  
  38. }  
  39. Output:-


Don't want to miss updates? Please click the below button!

Follow MindStick