繁体   English   中英

在 C# 中处理大列表时出现 OutOfMemoryException

[英]OutOfMemoryException when processing large list in C#

我必须处理一些大的表格数据文件(例如:300.000 行 x 200 列),为每个单元格创建一个数据结构。

但是,在几千次迭代中,会抛出 System.OutOfMemoryException(计算机有 8GB RAM)。

在我的例子中,我将每一行解析为一个字典列表,因为我必须有列名和值,并通过使用这些信息创建一个节点来处理它。

这是代码:

// foreach record 
foreach (Dictionary<string, string> inst in c.TodasInstancias)
{
instance = null;
tmp = null;

// for each column
foreach (KeyValuePair<string, string> kv in inst)
{
    if ((tmp = this.Relations.Find(new Predicate<Relacionamento>(x => x.Target.Name == kv.Key))) != null)
   { // if this class has a relationship with other classes
       p = og.CreateUriNode(this.UriOntologiaPrefix + ":" + kv.Key);
       o = og.CreateUriNode(new Uri(tmp.CampoOrigem.Classe.Configs.Name_space + "/" + kv.Value));
       og.Assert(instancia, p, o);
   }
   else (kv.Key != c.Configs.Identificador)
   { // se for um campo comum de dados
       p = og.CreateUriNode(this.UriOntologiaPrefix + ":" + kv.Key);
       o = og.CreateLiteralNode(kv.Value);
       og.Assert(instancia, p, o);
   }
}
}

有关如何绕过此异常的任何提示?

使用可查询来处理大列表。 您可以使用 Skip and Take 检索批量记录,而不是您需要的所有数据。 这样你每次都会在内存中处理一个小列表。 前任:

   var count=context.entities.Count(); 

   for(i=0,i<count,i+=1000)//1000 can be any size of batch
   {
       var batch =context.entities.Skip(i).Take(1000);
       //Do Operations you need
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM