今天我的申请今天投了一个OutOfMemoryException.对我来说,这几乎是不可能的,因为我有4GB的RAM和大量的虚拟内存.当我尝试将现有集合添加到新列表时发生错误.
List<Vehicle> vList = new List<Vehicle>(selectedVehicles);
Run Code Online (Sandbox Code Playgroud)
根据我的理解,这里分配的内存不多,因为我的新列表应该包含的车辆已经存在于内存中.我不得不承认这Vehicle是一个非常复杂的课程,我试图一次性将大约50.000个项目添加到新列表中.但是由于Vehicle应用程序中的所有内容都来自一个只有200MB大小的数据库:我不知道OutOfMemoryException在这一点上可能会导致什么.
我们大多倾向于遵循上述最佳实践.
但是,即使有足够的可用内存, StringBuilder也可能抛出OutOfMemoryException.它抛出OOM异常,因为它需要"连续的内存块".
一些链接供参考 StringBuilder OutOfMemoryException
而且还有更多......
你们中有多少人遇到过这个问题或意识到你们做了什么来解决这个问题?
有什么我想念的吗?
PS:我没有意识到这一点.
我已经改写了这个问题.
***同样的事情与手动连接一起使用(我将验证这一点并更新SO).引起我担忧的另一件事是系统中有足够的内存.这就是我在这里提出这个问题的原因,以检查是否有人遇到这个问题或者代码有什么严重错误.
我创建了一个StringBuilder长度"132370292",当我尝试使用ToString()它抛出的方法获取字符串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");
}
Run Code Online (Sandbox Code Playgroud)
创建新字符串时OOM的原因是什么?为什么在写入文件时它不会抛出OOM?
机器详细信息:64位,Windows-7,2GB RAM,.NET 2.0版
我有一个程序(x64)消耗大量内存。我正在运行win server 2008 R2 SP1它48 GB RAM(64 bit),.net frame work 4.5.
我也在gcAllowVeryLargeObjects = trueapp.config中设置了。
当我运行该程序时,它消耗了 18 GB 内存,之后出现异常
EXCEPTION: System.OutOfMemoryException: Insufficient memory to continue the execution of the program.
at System.Text.StringBuilder.ExpandByABlock(Int32 minBlockCharCount)
at System.Text.StringBuilder.Append(Char* value, Int32 valueCount)
at System.Text.StringBuilder.Append(String value)
at System.Xml.XmlTextEncoder.Write(String text)
at System.Xml.XmlTextWriter.WriteWhitespace(String ws)
at System.Xml.XmlElement.WriteElementTo(XmlWriter writer, XmlElement e)
at System.Xml.XmlNode.get_OuterXml()
at System.Security.Cryptography.Xml.Utils.PreProcessElementInput(XmlElement e
lem, XmlResolver xmlResolver, String baseUri)
at System.Security.Cryptography.Xml.Reference.CalculateHashValue(XmlDocument
document, CanonicalXmlNodeList refList)
at System.Security.Cryptography.Xml.SignedXml.BuildDigestedReferences()
at System.Security.Cryptography.Xml.SignedXml.ComputeSignature()
Run Code Online (Sandbox Code Playgroud)
它给出了“内存不足”,但我们仍然有 30 …
最近,我一直在研究 C#/ASP.NET 中的代码,该代码在选择大参数期间会引发错误:
引发了“System.OutOfMemoryException”类型的异常。在 System.Text.StringBuilder.ToString()
概述: 代码根据用户的选择从数据库中查询大量数据,并将其放入 Excel 文档中,然后由用户导出/下载。它首先使用 StringBuilder 附加 XML 的前缀:
const string startExcelXML = "<xml version>\r\n<Workbook " +
"xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" +
" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n " +
"xmlns:x=\"urn:schemas- microsoft-com:office:" +
"excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:" +
"office:spreadsheet\">\r\n <Styles>\r\n " +
"<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " +
"<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" +
"\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" +
"\r\n <Protection/>\r\n </Style>\r\n " +
"<Style ss:ID=\"BoldColumn\">\r\n <Font " +
"x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " +
"<Style ss:ID=\"StringLiteral\">\r\n <NumberFormat" +
" ss:Format=\"@\"/>\r\n </Style>\r\n <Style " +
"ss:ID=\"Decimal\">\r\n <NumberFormat " + …Run Code Online (Sandbox Code Playgroud) 我正在研究c#.我想知道如何释放stringbuilder n byte [] ....因为我在使用字符串生成器时遇到了内存异常....另一件事是String.Replace()也给出了内存不足异常或者还有其他方法可以做同样的事情....请告诉我如何克服这些问题...提前感谢