Friday, December 4, 2009

Rule of Thumb (For Myself)

Thou must call Dispose

If your object is an instance of class implements the IDisposable Interface, or has a Dispose or Close method, you must call it. If an instance member of your class implements IDisposable, your class should implements IDisposable too. The primary use of IDisposable interface is to release unmanaged resources.

if (this.dbTrans != null)
{
 dbTrans.Dispose();
 dbTrans = null;
}

if (this.dbReader != null)
{
 dbReader.Close();
 dbReader.Dispose();
 dbReader = null;
}

dbConn.Dispose();
dbConn = null; 

Thou must use "using" wherever needed

Do you know that "using" block can only be used for the classes that implement the IDisposable?

using (TransactionScope tx = new TransactionScope())
{
 try
 {
  iResult = SaveQueryHeader(_tableName, queryHeader, session);
 
  tx.Complete();
 }
 catch (Exception ex)
 {
  tx.Close();
  HandleException(ex);
 }
} 

Thou must not play with GC
// Naughty codes, keep-it close

//GC.Collect();
//GC.WaitForPendingFinalizers();
//long lMemory = GC.GetTotalMemory(false);
//GC.AddMemoryPressure(lMemory);

'System.OutOfMemoryException' Does Not Refer to Physical Memory

Adding memory will not solve the problem. That's because of your code, thou must find the culprit!