内存异常HashSet

时间:2016-06-16 09:36:59

标签: c# file stream load hashset

加载4GB文件时出现内存异常。我必须这样做两次,但我已经将memmory加载它们。当我有3gb或更多时,会发生memmory异常。:(。

方法:

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);
    }

它是一个包含许多行的表,我必须与类似的表进行比较。最后,我必须提供有关差异的报告。但我在这里失败了,我必须加载这个庞大的文件。

1 个答案:

答案 0 :(得分:0)

尝试使用BufferedStream

使用(BufferedStream bs = new BufferedStream(filestream)) {

}