Currently I'm optimizing memory-usage of an huge batch-program. The most memory is used by different DataTables. For examle, my DataTable dataTable
is using approx 260MB.
Like suggestet in the accepted answer of the thread "What is the memory overhead of storing data in a .NET DataTable?" I'm trying to move relevant data out of the DataTable. This is my code:
GC.Collect(); // force the garbage collector to free memory
// First stop point - Total process memory (taskmanager) = 900 MB
List<ExpandoObject> expandoList = new List<ExpandoObject>();
foreach (DataRow dataRow in dataTable.Rows)
{
dynamic expandoItem = new ExpandoObject();
expandoItem.FieldName = dataRow["FieldName"].ToString();
expandoList.Add(expandoItem);
}
// Second stop point - Total process memory (taskmanager) = 1055 MB
dataTable.Clear();
dataTable.Dispose();
dataTable = null;
GC.Collect(); // force the garbage collector to free memory
// Third stop point - Total process memory (taskmanager) = 1081 MB (wtf? even more!)
I'm using Clear, Dispose and setting to null because it is suggested in following thread: Datatable.Dispose() will make it remove from memory?
See the stop points comments to see memory usage at that points. I also tried it with using (DataTable dataTable = ...)
but the results were the same.
What am I doing wrong? And maybe is there a better way to minify data from DataTable?