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:
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()
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With