ObjectContext正在为分离的实体泄漏内存

spr*_*y76 8 .net c# memory-leaks entity-framework

我已经使用内存分析器检查了这个,并且没有真正的实体保留在内存中,但是散列集,字典和EntityKey对象 - 但我发现如何断开这些引用.

这么简单的问题:如何阻止上下文(或其ObjectStateManager)的大小无限增长

[是的,我知道应该避免长时间的生活环境,但在这种情况下,这是一个复杂的分析运行,需要加载几个分层数据(下面的示例只是一个最小的问题演示)所以最后它是一个"短"生活的单一操作环境.]

重现的步骤:

  • 创建一个新的控制台应用程
  • 为Northwind数据库创建EF模型(使用一些真正的SQL Server或从Compact Samples文件夹复制Northwind.sdf)
  • 使用以下代码:

代码[已更新,不再需要真正的数据库连接]:

class Program
{
    static void Main()
    {
        const double MiB = 1024 * 1024;
        using ( var context = new NorthwindEntities() )
        {
            var last = GC.GetTotalMemory(true) / MiB;
            Console.WriteLine("before run: {0:n3} MiB", last);
            var id = 0;
            while ( true )
            {
                Run(context, ref id);

                GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
                GC.WaitForPendingFinalizers();
                var current = GC.GetTotalMemory(true) / MiB;
                Console.WriteLine("after run: {0:n3} MiB (+{1:n3} MiB)", current, current - last);
                last = current;

                if ( Console.KeyAvailable )
                    break;
                Console.WriteLine(new string('-', 100));
            }
        }
    }

    static void Run(NorthwindEntities context, ref int id)
    {
        for ( int i = 0; i < 100000; i++ )
        {
            var category = new Category { Category_ID = ++id };
            category.EntityKey = new EntityKey("NorthwindEntities.Categories", "Category_ID", id);
            var product = new Product { Product_ID = id, Category_ID = id };
            product.EntityKey = new EntityKey("NorthwindEntities.Products", "Product_ID", id);
            product.Category = category;
            context.Attach(product);
            context.Detach(product);
            context.Detach(category);
        }

        var ctr = 0;
        Console.WriteLine("Enumerating living/attached objects:");
        const EntityState AllStates = EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged;
        foreach ( var entry in context.ObjectStateManager.GetObjectStateEntries(AllStates) )
            Console.WriteLine("  #{0} [{1}] {2}", ++ctr, entry.EntityKey, entry.Entity);
        if ( ctr == 0 )
            Console.WriteLine("  NOTHING (as expected)");
    }
}
Run Code Online (Sandbox Code Playgroud)

spr*_*y76 1

由于我只是在调用 SaveChanges() 之后直接分离实体,所以我现在正在计算分离实体的数量,当计数器达到 10,000 时,我从上下文中分离所有仍然存在的(和需要的)对象,并创建一个新的上下文我附加了所有分离的物体。缺点:EntityReferences 和 EntityCollections 的 IsLoaded 属性现在始终为 false(但我不依赖于此)。