把长度为0的StringBuilder OutOfMemoryException异常 [英] Putting length to 0 for OutOfMemoryException of StringBuilder

查看:211
本文介绍了把长度为0的StringBuilder OutOfMemoryException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code块它获取的文件,并要求追加数据如下图所示:

I have a block of code which gets a file and appends require data as shown below:

var srBuilder = new StringBuilder();
using (var file = new StreamReader(document.FullSourcePath, Encoding.ASCII))
            {
                while (!file.EndOfStream)
                {                 
                    var bytess = new char[numBytes];
                    file.ReadBlock(bytess , 0, bytess.Length);
                    srBuilder.Append(buff);
                }

                document.Document += srBuilder.ToString();  ////Exception occures here
            }

但是,当文件超过200 MB那么它的投掷OutOfMemoryException异常。

But when file is more than 200 MB then its throwing OutofMemoryException.

我的想法是让字符串生成器的长度如下为零:

What i thought is to make length of string builder to zero as below:

 while (!file.EndOfStream)
        {  
            srBuilder.Length = 0;      //// Here         
            var bytess = new char[numBytes];
            file.ReadBlock(bytess , 0, bytess.Length);
            srBuilder.Append(buff);
        }

这是最好的解决办法还是别的什么要求?

Is it best solution or anything else is required?

推荐答案

您的最终目标是要分配一个200 MB的ASCII文件到 document.Document 成员变量,这我认为是字符串类型。

Your ultimate goal is to assign a 200 MB ASCII file to the document.Document member variable, which I assume is of type string.

长话短说:你不能做到这一点,需要重新考虑你的方法。

Long story short: you cannot do this, and need to reconsider your approach.

这种或那种方式,你的这个要求将需要400 MB的连续内存(假设字符串的char [] 的UTF-16字符数组,体重每2个字节字符),这是几乎不可能得到一个32位的过程中,其中 OutOfMemoryException异常运行期抛出试图告诉你。

One way or another, this requirement of yours will require 400 MB of contiguous memory (given strings are char[] arrays of UTF-16 characters, weighing 2 bytes per character), which is nigh impossible to obtain in a 32 bit process, which the OutOfMemoryException that the runtime throws is trying to tell you.

任何绝招你会发现都将有它的缺点。

Any "trick" you're going to find is going to have its drawbacks.


  • 在块读取该文件并不重要,因为最终的结果是一样的:最后一个字符串将仍然需要400 MB的连续内存。

  • 设置的长度 0 的StringBuilder ,通过谷歌搜索C#的StringBuilder OutOfMemoryException异常发现,将会使异常消失,但只是导致文件的最后一个块分配给 document.Document 。可以肯定地说,你不希望这一点。

  • 更改项目仅在64位机器上的运行可能是一种选择,但最有可能不会(硬件和其他依赖),并且是隐藏的实际问题的解决方法脏,和就是喜欢应用创可贴给吹断的腿:你忽略了致命的设计错误。有一天,有人会养活你的应用程序有2 GB的文件,而你又回到了起点。

  • Reading the file in chunks doesn't matter, as the end result is the same: the final string will still require 400 MB of contiguous memory.
  • Setting the StringBuilder's Length to 0, found through Googling "C# StringBuilder OutOfMemoryException", will make the exception go away, but only cause the last chunk of the file to be assigned to document.Document. It's safe to say you don't want that as well.
  • Changing the project to run only on 64 bit machines might be an option, but most likely won't (hardware and other dependencies), and is a dirty workaround that hides the actual problem, and that is like applying a band-aid to a blown-off leg: you're ignoring the fatal design mistake. One day someone is going to feed your app a 2 GB file, and you're back to square one.

您需要的的文件,并对其进行处理一行行。

You need to stream the file, and process it line by line.

foreach (string line in File.ReadLines(filename))
{
    // process line
}

这篇关于把长度为0的StringBuilder OutOfMemoryException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆