Search This Blog

Wednesday, June 30, 2021

Csharp: Garbage Collector

Confusing Terms:
  1. IDispose -- unmanaged, user can call
  2. Difference between Close and Dispose
  3. GC.Collect -- managed, user can call
  4. Finalize -- unamanged, user can not call
  5. Destructor
  6. SupressFinalize
  7. Close
================================================================================
IDispose : 
  • Used for disposing unmanaged resources 
  • When we implement IDispose interface, Dispose is the method where we dispose the umanged resource. 
  • In some objects we have two options 1. dispose and 2. Close. For file streaming both options are same. 
Void dispose()
{
this.close();
Dispose(true);
GC.SupressFinalize(this);
}
===============================================================================
Difference between Close and Dispose

Close
1. Disconnected with DB
2. Release resources.
3. SQL Connection object not released, it is just close and can be open again. 

Dispose
1. Disconnected with DB
2. Release resources.
3. Release SQL connection object as well. 

Example: 
try 
    
        string constring = "Server=(local);Database=my; User Id=sa; Password=sa";                             SqlConnection sqlcon = new SqlConnection(constring); 
        sqlcon.Open(); // here connection is open 
        // some code here which will be execute 
    
catch // code will be execute when error occurred in try block } 
finally 
    
        sqlcon.Close(); // close the connection 
        sqlcon.Dispose(); // desroy the connection object 
    }

===============================================================================
GC.Collect:

1. this method is used to clear manged resources. 
2. Unlike Finalize which can not be called by user, GC.Collect can be called by user. 
3. Also GC.Collect is to work on managed resrouce while GC.Finalize is for unmanaged resources. 
4. It will force the GC generations to start. 

===============================================================================
Finalize():

1. This method is used to clear the unmanaged resource like IDispose. 
2. This method can only be called by GC as a part of destructor. 
3. We can not call it explicitly. We have to implemetn IDispose instead, if we want to take care of un managead resource. 

===============================================================================
Destructor:

1. Finalize also called as destructor of the class. Finalize()
2. It can not be explicitly called except for the destructor calling itself behind the scene. 
3. Finalize can be used to clean unmanged resource in C# with the help of GC. 

~myclass()
{
this.Dispose(); // behind the scene finalize would also be called
}

===============================================================================
SupressFinalize()

1. This method is to tell the CLR that we have already taken care of releasing unmanged resources and GC need not to act on it. 

===============================================================================

No comments:

Post a Comment