【发布时间】:2020-01-20 17:00:41
【问题描述】:
我无法释放 EF DbContext 使用的 proc 内存。 尽管有 dispose 和 GC.Collect,我的应用程序并没有释放内存。
我创建了一个小程序:
static void Main(string[] args)
{
var test = new TestDb();
System.Threading.Thread.Sleep(1000);
// Here proc memory = 7 Mo
test.Fill();
System.Threading.Thread.Sleep(1000);
// Here proc memory = 58 Mo
test.Dispose();
System.Threading.Thread.Sleep(1000);
// Here proc memory = 37.8 Mo
test = null;
GC.Collect();
// Here proc memory = 37.8 Mo
// Why is not 0 here ???
System.Threading.Thread.Sleep(1000);
}
您可以使用我的 cmets 显示内存使用情况。
TestDb 类:
public class TestDb : IDisposable
{
private List<TaskAction> _actions;
public void Fill()
{
using (var entities = new myEntities())
{
entities.Configuration.LazyLoadingEnabled = false;
_actions = entities.TaskActions.ToList();
}
}
public void Dispose()
{
_actions = null;
GC.Collect();
}
}
如何释放所有使用的内存??
谢谢,
查理
【问题讨论】:
标签: c# entity-framework memory garbage-collection