StringBuilder.ToString()抛出OutOfMemory例外 [英] StringBuilder.ToString() throws OutOfMemory Exception

查看:4720
本文介绍了StringBuilder.ToString()抛出OutOfMemory例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建一个的StringBuilder 长度132370292,当我尝试使用的ToString()来获取字符串方法,它会抛出 OutOfMemoryException异常

  StringBuilder的SB =新的StringBuilder (); 

的for(int i = 0; I< = 5000;我++)
{
SB.Append(一些垃圾数据进行测试我的实际数据从不同的来源产生。通过追加到字符串生成器)。
}


{
字符串str = SB.ToString(); //抛出OOM大多
Console.WriteLine(字符串创建成功);
}
赶上(OutOfMemoryException异常前)
{
的StreamWriter SW =新的StreamWriter(@C:\memo.txt,真正的);
sw.Write(SB.ToString()); //始终写入文件中没有任何错误
Console.WriteLine(写入到文件成功);
}



什么是对OOM,同时创造一个新的字符串的原因,以及为什么它不?'T抛出OOM,同时写入文件



机详细信息:64位的Windows 7,2GB内存
注:.NET 2.0版


解决方案

什么是对OOM,同时创造一个新的字符串的原因




由于你正在运行的内存 - 或者至少在CLR不能分配与您所请求的大小的对象。它就是这么简单。如果你想避免这些错误,不要试图创建不适合内存的字符串。需要注意的是,即使你有大量的内存,即使您运行的是64位CLR,是有限制的,可以创建的对象的大小。



< BLOCKQUOTE>

和为什么而写一个文件?


它不会抛出OOM

由于您有更多的磁盘空间比内存。



我敢肯定,代码的的完全按照你描述虽然。这条线将无法编译:

  sw.write(SB.ToString()); 



...因为方法而不是。如果你的真正的调用 SB.ToString(),那么这同样可能失败,因为海峡= SB的ToString()



似乎更可能是你的真正的以流写入文件时尚,如:

 使用(VAR作家= File.CreateText(...))
{
的for(int i = 0; I< 5000;我++)
{
writer.Write(MYTEXT);
}
}



这样,你永远需要有文字的巨额在内存 - 它只是它,因为它的推移,可能与的部分的缓冲,但还不足以导致内存问题


写入到磁盘

I have a created a StringBuilder of length "132370292", when I try to get the string using the ToString() method it throws OutOfMemoryException.

 StringBuilder SB = new StringBuilder();

 for(int i =0; i<=5000; i++)
 {
    SB.Append("Some Junk Data for testing. My Actual Data is created from different sources by Appending to the String Builder.");
 }

 try
 {
 string str = SB.ToString(); //Throws OOM mostly
 Console.WriteLine("String Created Successfully");
 }
 catch(OutOfMemoryException ex)
 {
 StreamWriter sw = new StreamWriter(@"c:\memo.txt", true);
 sw.Write(SB.ToString()); //Always writes to the file without any error
 Console.WriteLine("Written to File Successfully");
 }

What is the reason for the OOM while creating a new string and why it doesn't throw OOM while writing to a file ?

Machine Details: 64-bit, Windows-7, 2GB RAM Note: .NET version 2.0

解决方案

What is the reason for the OOM while creating a new string

Because you're running out of memory - or at least, the CLR can't allocate an object with the size you've requested. It's really that simple. If you want to avoid the errors, don't try to create strings that don't fit into memory. Note that even if you have a lot of memory, and even if you're running a 64-bit CLR, there are limits to the size of objects that can be created.

and why it doesn't throw OOM while writing to a file ?

Because you have more disk space than memory.

I'm pretty sure the code isn't exactly as you're describing though. This line would fail to compile:

sw.write(SB.ToString());

... because the method is Write rather than write. And if you're actually calling SB.ToString(), then that's just as likely to fail as str = SB.ToString().

It seems more likely that you're actually writing to the file in a streaming fashion, e.g.

using (var writer = File.CreateText(...))
{
    for (int i = 0; i < 5000; i++)
    {
        writer.Write(mytext);
    }
}

That way you never need to have huge amounts of text in memory - it just writes it to disk as it goes, possibly with some buffering, but not enough to cause memory issues.

这篇关于StringBuilder.ToString()抛出OutOfMemory例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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