2015-05-05 47 views
0
private List<T> ReadCurrentFile(string currentExtractedFile, PurgingDetails purgingParams) 
{ 
    List<T> thinLogDoList = new List<T>(); 
    using (StreamReader sr = new StreamReader(currentExtractedFile)) 
    { 
     string currentLine = string.Empty; 
     Dictionary<string, string> ColumnNamesDictionary = null; 
     while ((currentLine = sr.ReadLine()) != null) 
     { 
      if (currentLine.IsNotNullOrEmpty() && currentLine.Contains("Æ")) 
      { 
       string[] columnNames = currentLine.Split(new char[] { 'Æ' }); 
       ColumnNamesDictionary = FillColumnNameDictionary(columnNames); 

       if (CheckForValidConditions(ColumnNamesDictionary, purgingParams)) 
       { 
        thinLogDoList.Add(FillThinLogDO(ColumnNamesDictionary)); 
       } 
      } 
     } 
    } 
    return thinLogDoList; 
} 

(上面的代码是用于读取的文件和通过填充对象将数据添加到列表中。) 该函数读取的大小为10 MB的文件它位于一个zip文件中,首先我提取zip文件,然后使用该函数读取数据并将其存储到List中,然后删除提取的zip文件。它正在为大约6L(6,00,000)数据工作,但超出该数据它会抛出异常。 我想读更多数据10L(10,00,000)我应该怎么做?的System.OutOfMemoryException:类型的异常“System.OutOfMemory”被抛出

+0

如果您使用印度格式的 – xanatos

+1

中的数字,这是相当令人困惑的,您无法真正能够做到这一点。一种方法是直接产生/返回一个IEnumerable 或者甚至可能使用Dictionary。使用字典可以大大改善性能并减少问题。 –

+0

什么是文件格式? – xanatos

回答

2

不要返回列表。相反,使用收益率返回来运行数据:

private IEnumerable<i1LogThinDO> ReadCurrentFile(string currentExtractedFile, 
               PurgingDetails purgingParams) 
{ 
    using (StreamReader sr = new StreamReader(currentExtractedFile)) 
    { 
     string currentLine = string.Empty; 
     Dictionary<string, string> ColumnNamesDictionary = null; 
     while ((currentLine = sr.ReadLine()) != null) 
     { 
      if (currentLine.IsNotNullOrEmpty() && currentLine.Contains("Æ")) 
      { 
       string[] columnNames = currentLine.Split(new char[] { 'Æ' }); 
       ColumnNamesDictionary = FillColumnNameDictionary(columnNames); 

       if (CheckForValidConditions(ColumnNamesDictionary, purgingParams)) 
       { 
        yield return FillThinLogDO(ColumnNamesDictionary); 
       } 
      } 
     } 
    } 
} 

这样,球就在呼叫者的院子里。调用者必须能够处理从该方法返回的数据,而不将它们全部保存在内存中。这可能意味着您必须重新设计调用方法,但是如果您可以在不将数据保留在内存中的情况下执行所有处理,则会大大减少应用程序的内存占用空间。

+0

谢谢@Zoran,但是您的解决方案也无法正常工作。 –

+0

你在调用ToList()或类似的方法吗?如果此方法的任何调用者具体化列表,此解决方案将不起作用。你必须修复调用者在foreach循环中运行,并且永远不要调用ToList()或类似的东西。 –

+0

仍抛出异常,应该如何处理? –

相关问题