2013-09-30 56 views
4

我有一個程序(x64)會消耗大量內存。 我在win server 2008 R2 SP1上運行它48 GB RAM(64 bit),.net frame work 4.5即使30 GB內存空閒,內存異常也不充足

我也曾在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應用程序或服務器給我這個錯誤的限制。

+1

它可能是碎片問題,如果您強制GC發生錯誤?或者可能是這樣的:http://stackoverflow.com/a/7537821/212121 – Giedrius

+0

通常框架拋出的OOM沒有堆棧。查看'StringBuilder.ExpandByABlock'源代碼 - 它在某些情況下故意拋出OOM。 – mikalai

回答

0

即使在這個問題上沒有明確的證據,我仍然會想到一件事。

請記住,在BCLList<T>集合的內存限制,無論是32位還是64位。最大內存量單個集合實例可以在兩種體系結構上分配不多於2GB

您可以對您的使用類型看,expecially爲List<T>,如果有一些泄漏發生。

希望這會有所幫助。

+1

gcAllowVeryLargeObjects:在64位平臺上,啓用總大小大於2千兆字節(GB)的數組:http://msdn.microsoft.com/en-us/library/hh285054%28v=vs.110%29。 ASPX – Giedrius

3

您正在運行到StringBuilder類的內部限制,它無法產生具有超過int.MaxValue字符的字符串。這個限制在.NET中是非常嚴格的,gcAllowVeryLargeObjects可以幫助但不能解決它。核心問題是int類型不夠大,無法索引字符串。

你必須編寫更聰明的代碼來解決這個問題。這需要使用StreamWriter而不是StringWriter開始。換句話說,寫入文件而不是內存。

你仍然可以使用所有的機器上的RAM你有,文件首先寫入到文件系統緩存。你的程序不會變慢。