String和StringBuilder上的内存不足

时间:2013-03-11 14:25:17

标签: .net string out-of-memory stringbuilder

我首先使用StringBuilder编写下面的代码,然后点击Out of Memory Exception,阅读相关问题并将其更改为字符串+ =但仍然收到相同的OOM错误。

我有~2100 +不同大小的文件,总共约200MB +。

由于限制,我无法使用附加模式文件访问。任何事件?

const string myPath = @"C:\values\";

var files = Directory.EnumerateFiles(myPath).Select(Path.GetFileNameWithoutExtension);
var filesGrouped = files.Where(f => f.Length > 12).GroupBy(f => f.Substring(0, f.Length - 9)).OrderBy(f => f.Key).ToList();

filesGrouped.ForEach(fg =>
    {
        var myRows = string.Empty;

        // download files
        fg.ToList().ForEach(gf =>
            {
                string myFile;
                string fileToRead = string.Format("{0}{1}.csv", myPath, gf);
                using (var sr = new StreamReader(fileToRead)) myFile = sr.ReadToEnd();
                //! OOM Error hits the line below
                myRows += myFile + ControlChars.NewLine;
            });

        // upload new file
        using (var sw = new StreamWriter(myPath + fg.Key + ".csv", false, Encoding.UTF8)) sw.Write(myRows);
    });

1 个答案:

答案 0 :(得分:1)

您可以尝试:

filesGrouped.ForEach(fg =>
{
    using (var sw = new StreamWriter(myPath + fg.Key + ".csv", false, Encoding.UTF8))
    {
        fg.ToList().ForEach(gf =>
        {
            string fileToRead = string.Format("{0}{1}.csv", myPath, gf);
            using (var sr = new StreamReader(fileToRead))
            {
                foreach (var line in sr.ReadLine())
                {
                    sw.WriteLine(line);
                }
                sw.Write(Environment.NewLine);
            }
        });

    }
});

如果这也失败了(文件内容在一行上),您必须选择使用相同的原则StreamReader.ReadStreamWriter.Write