EF DbContext内存未释放

问题描述 投票:0回答:1

我无法释放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);
}

您可以在我的评论中显示内存使用情况。

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
1个回答
0
投票

除非有充分的理由,否则运行时不会将内存交还给操作系统;如果您刚刚使用过{一定数量的内存},则很有可能稍后要再次使用它,并且保持一段时间会更有效[GC.Collect只是处理内部簿记,因此它不需要很快就从OS要求more内存。您可以做一些事情来使运行时更积极地返回内存,但是... 37Mb基本上没有。另外,它仍需要占用内部.NET位(类型元数据,已加载的所有程序集,JIT,IL等)的内存。

© www.soinside.com 2019 - 2024. All rights reserved.