Click here to Skip to main content
15,906,285 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am Calling .Dispose() method on objects, does it helps in Garbage Collection?
Please find an example below;
DataSet recordSet=null;
try
{
    ... //connection, command and adapter realated statements.
    adapter.Fill(recordSet);
    return recordSet;
}
catch (Exception ex)
{
    throw;
}
finally
{
    if (recordSet!=null)
    {
        recordSet.Dispose();
    }
    recordSet=null;
    GC.Collect();
    GC.Collect(2);
}
Posted

1 solution

Garbage collection is best left to its own devices, don't try to coerce it with GC.Collect.

Dispose doesn't really help with garbage collection, but it does help by releasing unmanaged resources before they get GCed.

A using statement does all that exception stuff for you btw.
 
Share this answer
 
Comments
Mehdi Gholam 19-Sep-11 10:03am    
My 5!
avigodse 20-Sep-11 5:23am    
But as I have tried to remove the explicit calls of GC.Collect(), it did not collect the disposed object memory soon, but it waits till it reaches a point, and which is is very random or on at need.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900