【问题标题】:Entity SaveChanges generates a System.OutOfMemoryException实体 SaveChanges 生成 System.OutOfMemoryException
【发布时间】: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(); 
}

【问题讨论】:

标签: c# .net database linq entity


【解决方案1】:

我最终生成了 UPDATE SQL 语句,并且只在第一行中使用 Select(...) 指定了我需要的列

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多