Memory leak issue.
-
Hi all, I have created a scheduling service which runs all the time, I am having a query regarding following set of code.
private DataSet GetDataSet() { DataSet excelDataSet = new DataSet(); OleDbConnection excelConnection = new OleDbConnection(myConnectionString); OleDbCommand excelCommand = new OleDbCommand(@"SELECT * FROM " +fileName, excelConnection); OleDbDataAdapter excelAdapter = new OleDbDataAdapter(excelCommand); excelConnection.Open(); excelAdapter.Fill(excelDataSet); if (excelConnection.State == ConnectionState.Open) excelConnection.Close(); return excelDataSet; }
Do I need to call dispose on OleDbDataAdapter and OleDbConnection connection object to avoid memory leaks or garbage collector will take care of this?Regards, Prakash Kalakoti
-
Hi all, I have created a scheduling service which runs all the time, I am having a query regarding following set of code.
private DataSet GetDataSet() { DataSet excelDataSet = new DataSet(); OleDbConnection excelConnection = new OleDbConnection(myConnectionString); OleDbCommand excelCommand = new OleDbCommand(@"SELECT * FROM " +fileName, excelConnection); OleDbDataAdapter excelAdapter = new OleDbDataAdapter(excelCommand); excelConnection.Open(); excelAdapter.Fill(excelDataSet); if (excelConnection.State == ConnectionState.Open) excelConnection.Close(); return excelDataSet; }
Do I need to call dispose on OleDbDataAdapter and OleDbConnection connection object to avoid memory leaks or garbage collector will take care of this?Regards, Prakash Kalakoti
Though the GC would collect all of the allocated managed memory periodically, its best to dispose off the DB objects when you are done. Your code should have been in the lines of : using(OleDbConnection excelConnection = new ...) { etc etc }
namaste, Nitin Koshy http://devwaves.blogspot.com ...and I thought I knew