相关疑难解决方法(0)

如何强制垃圾收集器运行?

采访者今天问我这件事......有答案吗?

c#

130
推荐指数
7
解决办法
17万
查看次数

如何释放C#中List分配的内存

我有这样的代码:

var myList = db.Table1.ToList();
/*doing some operations on the list*/
var myList = db.Table2.ToList();
/*again doing some operations on the list*/
var myList = db.Table3.ToList(); // I'm getting out of memory exception here.
Run Code Online (Sandbox Code Playgroud)

我不能按页检索数据,因为我需要一次所有的表.在加载另一个表之前,如何处理(我的意思是释放该列表所公开的空间)列表?谢谢.

编辑:

我加载后,实际上是从myList生成了很多(有时是数千个)子列表.所以我真的需要学习如何释放列表.

编辑2:这是我的完整Stacktrace:

   at System.Collections.Generic.List`1.set_Capacity(Int32 value)
   at System.Collections.Generic.List`1.EnsureCapacity(Int32 min)
   at System.Collections.Generic.List`1.Add(T item)
   at System.Data.Entity.Core.Objects.EntityEntry.TakeSnapshot(Boolean onlySnapshotComplexProperties)
   at System.Data.Entity.Core.Objects.Internal.SnapshotChangeTrackingStrategy.TakeSnapshot(EntityEntry entry)
   at System.Data.Entity.Core.Objects.Internal.EntityWrapper`1.TakeSnapshot(EntityEntry entry)
   at System.Data.Entity.Core.Objects.ObjectStateManager.AddEntry(IEntityWrapper wrappedObject, EntityKey passedKey, EntitySet entitySet, String argumentName, Boolean isAdded)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
   at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
   at …
Run Code Online (Sandbox Code Playgroud)

c# dispose memory-management list out-of-memory

-1
推荐指数
1
解决办法
973
查看次数

标签 统计

c# ×2

dispose ×1

list ×1

memory-management ×1

out-of-memory ×1