【发布时间】:2014-09-18 14:20:46
【问题描述】:
我有一个获取数据块然后更新并保存它们的过程
进程是每块 50K 记录(总共大约 400K),但我总是在大约一半时收到 System.OutOfMemoryException(当进程内存达到 1.4GB 时)
IEnumerable<Contacts> allContacts = objDB.Contacts.OrderBy(p=>p.Period).AsEnumerable();
int chunkSize = 50000;
int curCount = 0;
while (true)
{
var chunk = allContacts.Skip(curCount).Take(chunkSize).AsEnumerable();
curCount += chunkSize;
int count = chunk.Count();
if (count == 0) break;
UpdateContacts(chunk);
GC.Collect();
GC.WaitForPendingFinalizers();
}
我尝试强制垃圾收集器,在每次 SaveChanges 之后创建一个新上下文,在 .config 中添加 gcAllowVeryLargeObjects 键仍然不行
有什么想法吗?
UpdateContacts() 没什么特别的:
private void UpdateContacts(IEnumerable<Contacts> allContacts)
{
foreach (Contact contact in allContacts)
{
//update stuff here
}
objDB.SaveChanges();
objDB = new DBContext();
}
【问题讨论】:
-
我们有机会看到 UpdateContacts() 吗?
-
是的,我编辑了帖子
标签: c# .net database linq entity