4

我有一个消耗大量内存的程序(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()

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

4

2 回答 2

4

您遇到了 StringBuilder 类的内部限制,它无法生成包含多个int.MaxValue字符的字符串。这个限制在 .NET 中是一个非常严格的限制,gcAllowVeryLargeObjects 可以帮助但不能解决它。核心问题是int类型不够大,无法再索引字符串。

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

您仍然可以使用计算机上的所有 RAM,文件首先写入文件系统缓存。您的程序不会变慢。

于 2013-09-30T09:49:11.640 回答
0

我想到了一件事,即使这个问题没有明确的证据。

请记住,无论是位还是位,集合BCL都有内存限制。单个集合实例可以分配的最大内存量不超过两种架构。List<T>32642GB

List<T>如果发生泄漏,您可以查看您的类型的使用,特别是对于。

希望这可以帮助。

于 2013-09-30T06:42:19.077 回答