2016-06-16 62 views
-1

加載4GB文件時出現內存異常。我必須這樣做兩次,但我已經使用內存來加載它們。當我已經離開或更多3GB的memmory例外occures :(內存異常HashSet

方法:。

public TableModel LoadTable(string path) { 

     TableModel model = new TableModel(); 

     using (FileStream filestream = new FileStream(path, FileMode.Open, FileAccess.Read)) { 
      using (StreamReader reader = new StreamReader(filestream, Encoding.UTF8)) { 

       string line = ""; 
       bool isHeader = true; 
       int counter = 0; 
       List<string> rows = new List<string>(); 

       while ((line = reader.ReadLine()) != null) { 

        if (isHeader) { 
         model.Columns = line.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries); 
         isHeader = false; 
         continue; 
        } else { 
         if (Settings.Default.RonudSet != 0) { 
          rows.Add(RoundDecimals(line)); 
         } else { 
          rows.Add(line); 
         } 
        } 
        counter++; 
       } 
       model.RowCount = counter; 
       model.ColumnsCount = model.Columns.Length; 
       model.Keys = new HashSet<string>(rows); 
      } 
     } 
     return model; 
    } 

例外:

的System.OutOfMemoryException是未處理 功能評價是禁用的,因爲一個徹頭徹尾的。內存異常

的TableModel:

class TableModel { 

    private string tableName; 
    private string[] columns; 
    private HashSet<string> keys; 

    public int ColumnsCount { get; set; } 
    public int RowCount { get; set; } 
    public string TableName { get { return tableName; } set { this.tableName = value; } } 
    public string[] Columns { get { return columns; } set { this.columns = value; } } 
    public HashSet<string> Keys { get { return keys; } set { this.keys = value; } } 
} 

開始比較:

if (newFile.Name.Equals(currFile.Name)) { 

        FileLoader loader = new FileLoader(); 
        TableModel newModel = loader.LoadTable(newFile.ToString()); 
        TableModel currentModel = loader.LoadTable(currFile.ToString()); 
        newModel.TableName = newFile.Name; 
        currentModel.TableName = currFile.Name; 
        new Compare(newModel, currentModel, currFile.Directory.Name); 
        break; 
       } 

比較:

private void CheckColumns() { 

     bool sameColumnCount = CheckColumnsCount(); 
     int counter = 0; 
     currentContent = new HashSet<string>(currentModel.Columns); 
     newContent = new HashSet<string>(newModel.Columns); 

     foreach (string header in currentContent) { 
      if (!newContent.Contains(header)) { 
       headersNotFoundInN.Add(header); 
      } 
     } 
     foreach (string header in newContent) { 
      if (!currentContent.Contains(header)) { 
       headersNotFoundInC.Add(header); 
      } 
     } 
     if (currentModel.ColumnsCount == newModel.ColumnsCount) { 
      for (int i = 0; i < currentModel.ColumnsCount; i++) { 
       if (currentModel.Columns[i] == newModel.Columns[i]) { 
        counter++; 
       } 
      } 
      if (counter == currentModel.ColumnsCount) { 
       headerSequence = true; 
      } else { 
       headerSequence = false; 
      } 
     } else { 
      headerSequence = false; 
     } 
     bool emptyNotFoundIn = false; 
     if (headersNotFoundInC.Count == 0 && headersNotFoundInN.Count == 0) { 

      emptyNotFoundIn = true; 
     } 
     ReportContent(sameColumnCount, headerSequence, emptyNotFoundIn); 
    } 
    private void CheckRows() { 

     bool sameRowCount = CheckRowCount(); 
     currentContent = new HashSet<string>(currentModel.Keys); 
     newContent = new HashSet<string>(newModel.Keys); 

     foreach (string key in currentContent) { 
      if (!newContent.Contains(key)) { 

       rowNotFoundInN.Add(key); 
      } 
     } 
     foreach (string key in newContent) { 
      if (!currentContent.Contains(key)) { 

       rowNotFoundInC.Add(key); 
      } 
     } 
     bool emptyNotFoundIn = false; 
     if (rowNotFoundInC.Count == 0 && rowNotFoundInN.Count == 0) { 

      emptyNotFoundIn = true; 
     } 
     ReportContent(sameRowCount, emptyNotFoundIn); 
    } 

它與我有類似的表比較多行的表。最後,我必須提供一份關於差異的報告。但我在這裏失敗,我不得不加載這個龐大的文件。

+2

該解決方案將是,以不存儲在存儲器中的一切。你真的需要什麼信息? –

+0

在clausle中拋出異常,同時將行(行)添加到我的表中 – Ams1

+0

另外,您說它是一個大表,並且您必須將它與另一個表進行比較。如果您已經使用這個術語,爲什麼不使用數據庫而不是大型文本文件? –

回答

0

嘗試使用BufferedStream

使用(BufferedStream BS =新BufferedStream(FILESTREAM)) {

}