Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中释放由列表分配的内存#_C#_List_Memory Management_Out Of Memory_Dispose - Fatal编程技术网

C# 如何在C中释放由列表分配的内存#

C# 如何在C中释放由列表分配的内存#,c#,list,memory-management,out-of-memory,dispose,C#,List,Memory Management,Out Of Memory,Dispose,我有这样一个代码: 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. 我无法按页检索数据,因为我

我有这样一个代码:

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.
我无法按页检索数据,因为我需要一次所有的表。在加载另一个表之前,如何处置(我的意思是释放该列表分配的空间)列表?谢谢

编辑:

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

编辑2: 这是我的完整跟踪:

   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 System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
   at WebApplication2.MyMethod in line 2292
   at WebApplication2.Controllers.MyController.MyActtion(String myString) in line 137
   at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
   at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
   at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Collections.Generic.List`1.set_容量(Int32值)
位于System.Collections.Generic.List`1.EnsureCapacity(Int32分钟)
at System.Collections.Generic.List`1.Add(T项)
位于System.Data.Entity.Core.Objects.EntityEntry.TakeSnapshot(仅布尔型SnapshotComplexProperties)
位于System.Data.Entity.Core.Objects.Internal.SnapshotChangeTrackingStrategy.TakeSnapshot(EntityEntry)
位于System.Data.Entity.Core.Objects.Internal.EntityWrapper`1.TakeSnapshot(EntityEntry)
位于System.Data.Entity.Core.Objects.ObjectStateManager.AddEntry(EntityWrapper wrappedObject、EntityKey passedKey、EntitySet EntitySet、String argumentName、Boolean isAdded)
位于System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 ConstructionEntityDelegate,EntityKey EntityKey,EntitySet EntitySet)
在System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper-Shaper)
位于System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleNumerator.MoveNext()
位于System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
位于System.Collections.Generic.List`1.InsertRange(Int32索引,IEnumerable`1集合)
位于System.Collections.Generic.List`1.AddRange(IEnumerable`1集合)
第2292行中的WebApplication2.MyMethod
在第137行的WebApplication2.Controllers.MyController.myAction(字符串myString)
位于System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[]参数)
位于System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数)
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数)
在System.Web.Mvc.Async.AsyncControllerActionInvoker.b_u39(IAsyncResult asyncResult,ActionInvokeInnerInvokeState)
位于System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
位于System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResultBase`1.End()
位于System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d()中
在System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.c__DisplayClass46.b__3f()中

根据我上面的评论,
ToList
在处理大量项目时可能会占用大量内存

因此,有时可以通过预先分配列表来获得更好的内存性能

因此:

var tableCount=db.Table1.Count();
var myList=新列表(表计数)//我不知道这里的类型参数
myList.AddRange(数据库表1)//或db.Table1.AsEnumerable()?

根据帖子评论中的其他说明,
db
是EF
DbContext
派生类变量,在3个调用之间共享。和
Table1
Table2
Table3
是该上下文中的
DbSet
实例

当您使用
var myList=db.Table1.ToList()
时,除了读取内存中的表并填充列表外,EF
DbContext
还使用相同的对象填充内部
DbSet.Local
集合(称为跟踪)以便能够检测并将所做的更改(最终)应用于基础对象

这就是问题所在。事件如果将
myList
变量设置为
null
,则内部缓存(列表)仍然保存所有这些对象,最终在某个点导致
OutOfMemoryException

因此,如果您不打算修改返回的对象并将其保存回数据库,则可以使用所谓的方法完全消除
DbContext
内部缓存:

var myList = db.Table1.AsNoTracking().ToList();
或者清理(
Dispose
并设置为
null
)并为每个列表进程使用新上下文(
db=new YourDbContext()


当然,您可以将上述方法与注释中提到的其他技术结合起来,以清理您自己的列表变量。

我需要一次所有表的可能副本。在加载另一个表之前,如何处理列表?两句话完全相反。为什么要处理列表?旁注:为什么不在数据库端执行操作,而不向客户端检索整个(可能很大)表?可能会有帮助:我认为您需要澄清您的问题。例如,如果
db
变量是3个调用之间共享的EF
DbContext
,即使清除并设置为
null
并在列表上强制执行GC,
DbContext
仍将在其本地缓存列表中保存所有这些对象,从而停止“释放”列表的所有努力。但这只是一个猜测。没有其他信息,你只能猜测。谢谢,但我还是有例外。
var myList = db.Table1.AsNoTracking().ToList();