C # optimizes memory usage: how to free memory declared by DataTable

I am currently optimizing memory usage with a huge batch program. Most of the memory is used by various data tables. For example, my data table dataTableuses about 260 MB.

For example, the sentence in the accepted answer from the topic " What are the memory costs for storing data in the .NET DataTable? ". I am trying to remove the relevant data from a 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 use Clear, Dispose and set to null because it is suggested in the following topic: Datatable.Dispose () will force it to be deleted from memory?

, . using (DataTable dataTable = ...), .

? , DataTable?

+5
1

- : .NET EXE footprint

:

TaskManager .NET.

.NET , , , . TaskManager , .NET. .NET , , , , .

, > PerfMon.

: . , DataTable .

GarbageCollector :

long memoryInMB = GC.GetTotalMemory(forceFullCollection: true) / 1024 / 1024;

, 28 . , DataTable : -/

, , .

GC, Dispose Finalize .

+5

All Articles