Explicitly freeing memory in c#

Recently I wrote a program which loads some CSV files in to DataTables and do some processing. These files are in large size and have nearly 1 million rows. When I load first file in to data table it works fine. But then I load the second file I get a “Out of Memory exception” even though first I make the previously loaded datatable to null. After searching for hours I found the following solution which solved my issue.

  1. var sourceDataTable = DataParserHelper.GetDataTableFromCsv(@"F:\FileSplitter\Data\5263\property_finance.csv");
  2.         
  3.          sourceDataTable.Clear();
  4.          sourceDataTable.Dispose();
  5.          sourceDataTable = null;            
  6.          GC.Collect();
  7.          GC.WaitForFullGCComplete();
  8.          GC.WaitForPendingFinalizers();

 

if you make the datatable null what happens is it frees the Datatable from the rows and columns. But actually those rows and columns retained in the memory and those are not ready for the garbage collector. But when you call Clear() all the memory will be cleared.

 

Hope this helps.

 

Happy Coding !!!

Comments

Popular posts from this blog

Responsive Web Design

Affine Cipher in C#

Contract First Development in WCF 4.5