2011-11-14 50 views
1

在處理500 MB數據,並試圖將其序列化,XML序列:64位Windows 7中,辛處理器芯,32GB的RAM和OutOfMemory例外

GetStringForStringBuilder(String value, Int32 startIndex, Int32 length, Int32 capacity) 

拋出System.OutOfMemoryException

XML序列:

StringWriter xmlStr = new StringWriter(); 

XmlTextWriter twa = new XmlTextWriter(xmlStr); 

lock(myLock) 
{ 
    mySerializer.WriteObject(twa, myHandlerMap); // OutOfMemoryException 

    twa.Flush(); 
    twa.Close(); 

    set.StringBuffer = xmlStr.ToString(); 

    xmlStr.Dispose() 
} 

在閱讀以下鏈接的最後一段之後: http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/10/10/stringbuilder-outofmemoryexception.aspx

我懷疑是否有可能解決這個問題沒有MS修復它?

異常跟蹤:

RemoteStackTrace:  
    at System.String.GetStringForStringBuilder(String value, Int32 startIndex, Int32 length, Int32 capacity) 
    at System.Text.StringBuilder.GetNewString(String currentString, Int32 requiredLength) 
    at System.Text.StringBuilder.Append(Char value) 
    at System.IO.StringWriter.Write(Char value) 
    at System.Xml.XmlTextWriter.InternalWriteEndElement(Boolean longFormat) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.WriteString(XmlWriterDelegator xmlWriter, String value, XmlDictionaryString name, XmlDictionaryString ns) 
    at WriteKeyValueOfstringstringToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract) 
    at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) 
    at WriteArrayOfKeyValueOfstringstringToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract) 
    at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerializeWithSurrogate(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) 
    at WriteGenericViewingMPStateToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract) 
    at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerializeWithSurrogate(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteSerializationInfo(XmlWriterDelegator xmlWriter, Type objType, SerializationInfo serInfo) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteISerializable(XmlWriterDelegator xmlWriter, ISerializable obj) 
    at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerializeWithSurrogate(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) 
    at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) 
    at WriteKeyValueOfstringStateSerializeSetjJgp3TWnToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract) 
    at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) 
    at WriteArrayOfKeyValueOfstringStateSerializeSetjJgp3TWnToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract) 
    at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) 
    at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph) 
    at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph) 
    at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph) 

PLS。讓我知道如何解決?

+0

他們說這不太可能發生在x64機器上,你是否100%確定你的應用程序運行在64位模式?如果這是客戶端/服務器應用程序,那麼這兩個組件是否都在x64機器上運行? –

+0

你是否證實罪魁禍首是LOH?一些託管的內存分析器能夠向您展示如何使用LOH。你有沒有試過在64位運行?如果您還想同時在內存中存儲序列化副本,則500 MB數據量很大。 32位進程可以訪問2 GB的虛擬內存。其餘2 GB由Windows使用。 –

+0

提供更多代碼如何序列化....否則,請考慮編寫自己的自定義序列化程序,即使對於XML。 – weismat

回答

3

對於xml來說500MB是相當大的,聽到它不快樂並不讓我感到意外。如果可能的話,我會看爲基礎,即XmlWriter/XmlReader。沒有更多細節,目前還不清楚這是否可行。或者,考慮另一種格式!嚴重的...... xml在大尺寸時播放不好。

首先要嘗試的是... 不要使用字符串(或StringBuilder);而是通過FileStream通過XmlReader/XmlWriter直接從/向磁盤讀/寫。

+0

請求。看到添加的代碼。這是提供的API,我只能使用它。不能寫我自己的方法直接從磁盤讀/寫。 :-( – Azodious

+0

@有道理的爲什麼不呢?爲什麼你需要在這裏使用'StringBuilder' –

+0

它是由另一個庫提供的API,數據應該在特定位置序列化(只知道那個lib) – Azodious

1

我寧願將此直接序列化爲文件。 500 MB是一個相當龐大的數據,當它用「chatty」xml序列化時,會獲得更多。