即使30 GB内存空闲,也没有足够的内存异常

时间:2013-09-30 06:36:22

标签: c# .net memory memory-management windows-server-2008

我有一个消耗大量内存的程序(x64)。 我正在win server 2008 R2 SP1上使用48 GB RAM(64 bit).net frame work 4.5运行它。

我还在app.config中设置了gcAllowVeryLargeObjects = true

当我运行该程序时,它会消耗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()

它提供“内存不足”,但我们仍然有30 GB内存空闲。 它是.net应用程序的限制还是给我这个错误的服务器。

2 个答案:

答案 0 :(得分:4)

您遇到了StringBuilder类的内部限制,它无法生成超过int.MaxValue个字符的字符串。这个限制在.NET中非常严格,gcAllowVeryLargeObjects有帮助,但没有解决它。核心问题是 int 类型不足以索引字符串。

您必须编写更智能的代码来解决此问题。这需要从使用StreamWriter而不是StringWriter开始。换句话说,写入文件而不是内存。

您仍然可以使用机器上的所有RAM,首先将文件写入文件系统缓存。你的程序不会慢任何。

答案 1 :(得分:0)

有一件事在我脑海中浮现,即使在这个问题上没有明确的证据。

请记住,在BCL中,List<T>个集合存在内存限制,无论32位还是64位。 single 集合实例的最大内存量可以在两种体系结构上分配不超过2GB

如果发生了一些泄漏,您可以查看您的类型的使用情况,特别是List<T>

希望这有帮助。

相关问题