Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 6 memory usage reaches 2GB

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()     
}
like image 279
BeebFreak Avatar asked Nov 08 '22 09:11

BeebFreak


1 Answers

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.

like image 89
Rick the Scapegoat Avatar answered Nov 14 '22 22:11

Rick the Scapegoat