I have a DbContext with a dataset of> 20M records that needs to be converted to a different data format. So I read the data in memory, perform some tasks, and then delete the DbContext. The code works fine, but after a while I get OutOfMemoryExceptions. I was able to narrow it down to the next code snippet where I get 2M records, then release them and retrieve them again. The first search works fine, the second is an exception.
// first call runs fine using (var dbContext = new CustomDbContext()) { var list = dbContext.Items.Take(2000000).ToArray(); foreach (var item in list) { // perform conversion tasks... item.Converted = true; } } // second call throws exception using (var dbContext = new CustomDbContext()) { var list = dbContext.Items.Take(2000000).ToArray(); foreach (var item in list) { // perform conversion tasks... item.Converted = true; } }
Shouldn't the GC automatically free up all the memory allocated in the first use block, so the second block should work just as well as the first?
In my actual code, I don't get 2 million records at once, but something between 0 and 30K at each iteration. However, after about 15 minutes, my memory runs out, although all the objects should have been released.
source share