小编Vor*_*456的帖子

C#优化内存使用:如何释放DataTable要求的内存

目前,我正在优化大型批处理程序的内存使用情况。最多的内存由不同的数据表使用。例如,我的DataTable dataTable使用大约260MB。

就像线程接受的答案中的建议“ 在.NET DataTable中存储数据的内存开销是多少? ”,我正在尝试将相关数据移出DataTable。这是我的代码:

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 - …
Run Code Online (Sandbox Code Playgroud)

c# memory datatable memory-optimization

5
推荐指数
1
解决办法
4008
查看次数

标签 统计

c# ×1

datatable ×1

memory ×1

memory-optimization ×1