Entity Framework 6 memory usage reaches 2GB

c# entity-framework-6 out-of-memory

Question

I am building an application and am using Entity Framework 6. But I am running in to problems with memory usage. No matter what I try I sooner or later run into an out of memory error. So far I have tried the following:

  • Using using for the the context.
  • Save changes in batches and disposing of the context.
  • Manually calling GC.Collect().

But none of these prevent the Entity framework of using more memory with every saveChanges I do. Eventually hitting the 2GB limit and crashing my program.

Is there any way I am unaware of to make the Entity Framework release all memory?

Edit

using (var sqlite = new myEntities())
{
    sqlite.Configuration.AutoDetectChangesEnabled = false;
    sqlite.Configuration.ValidateOnSaveEnabled = false;

    foreach (var someItem in someList)
    {
        var newItem = new Item
        {
             ...
        };

        sqlite.tableName.Add(newItem);

        if (++countRecords%1000 == 0)
        {
            sqlite.SaveChanges();

        }
    }
    sqlite.SaveChanges();
}

As described above I also have tried setting the context without the using and disposing it after the SaveChanges.

if (++countRecords%1000 == 0)
{
    sqlite.SaveChanges();
    sqlite.Dispose();
    sqlite = new myEntities()     
}
1
5
9/8/2016 1:58:47 PM

Popular Answer

If it is indeed a batch issue, try something like this:

int batchSize = 10;

for (int i = 0; i < = someList.Count / batchSize; i++)
{
    var batch = someList.Skip(batchSize * i).Take(batchSize);

    using (var sqllite = new nyEntities())
    {
        foreach(var item in batch)
        {
            var newItem = new Item() {...};

            sqllite.tableName.Add(newItem);
        }

        sqllite.SaveChanges();
    }
}

This inverts the using statement to dispose the sqllite after each batch, thus clearing it out and starting fresh for each batch.

This code was made in notepad++ so be careful to clean it up if you try it out.

1
9/8/2016 7:43:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow