Question

I made a simple implementation using EF 6 in Database first Mode (SQL Server 208 R2) . The main objective of this test was to see if the system would have memory issues using EF. After the creation of the Model I used EF 6.x EntityObjecct Generator to create the objects. The database is pretty simple: You create Companies and Users, then you add Users in Company. I created 1000 Companies and 10000 users, after that I used the following code to add all users to all companies:

var query = (from x in db.ACCESS_USER                                 
        select x).ToList();
var query1 = (from y in db.COMPANY                                 
        select y).ToList();
foreach (var item in query1)
{
    foreach (var user in query)
    {
        ACCESS_USERSINCOMPANY acComp = new ACCESS_USERSINCOMPANY();
        acComp.COMPANYID = item.COMPANYID;
        acComp.USERID = user.USERID;
        acComp.CREATE_DATE = DateTime.Now;
        db.AddToACCESS_USERSINCOMPANY(acComp);                       
        db.SaveChanges();   
    }                   
}

When I run this code the memory starts to grow and in about 2 min my computer run out of memory. I made one test Running de GC manually and the memory issue got way, but my speed decreased drastically. Any Ideas?

Was it helpful?

Solution

Entity Framework lacks a decent out-of-the-box method for bulk operations, including bulk insert. It's proxy mechanism, although useful for both lazy-load and change-track, will get in your way on bulk operations.

AFAIK this is not a memory leak, it's just the regular behavior of EF and all the proxyies, context and more that get's created when you iterate through entities bound to a context.

GC will collect all of that, eventually, but GC will not run synchonously nor with any kind of priority, as you may already have found why: performance hit.

From previous experiences I've found that, sometimes, it's best to just drop ORM's and write plain old SQL commands, specially for bulk insert.

Anyway, here follows some interesting links that might help you. Last one (although I've never used it) seems quite interesting:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top