Skip to main content
 首页 » 编程设计

c#之XML 序列化 : 64Bit Windows7, oct 处理器核心,32GB RAM 和 OutOfMemory 异常

2024年02月23日20insus

在处理 500 MB 数据并尝试将其序列化时,

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) 

请。让我知道如何解决?

请您参考如下方法:

500MB 对于 xml 来说相当大,听到它不开心我也不会感到惊讶。如果可能的话,我会看一些基于流的东西,即XmlWriter/XmlReader。没有更多细节,不清楚这里是否可行。或者,考虑另一种格式!说真的……xml 在大尺寸下表现不佳。

但是,首先要尝试的是...不要使用字符串(或 StringBuilder);而是通过 XmlReader/XmlWriter 上的 FileStream 直接从磁盘读取/写入磁盘。