使用datacontractserializer时出现System.outofmemory异常 [英] System.outofmemory exception when using the datacontractserializer

查看:60
本文介绍了使用datacontractserializer时出现System.outofmemory异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用

  StringWriter的  xmlStr = new StringWriter();

            XmlTextWriter twa = new XmlTextWriter(xmlStr);
$


           

            lock(myLock)

            {

                mySerializer.WriteObject(twa,myHandlerMap);

  StringWriter  xmlStr = new StringWriter();
            XmlTextWriter twa = new XmlTextWriter(xmlStr);

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

}

 

我正在将系统输出内存异常:请参考调用堆栈。

i am getting system out of memory exception: Please refer the call stack.

RemoteStackTrace:   在System.String.GetStringForStringBuilder(字符串值,Int32 startIndex,Int32长度,Int32容量)

  在System.Text.StringBuilder.GetNewString(String currentString,Int32 requiredLength)

  在System.Text.StringBuilder.Append(Char值)

   at System.IO.StringWriter.Write(Char值)

  在System.Xml.XmlTextWriter.InternalWriteEndElement(Boolean longFormat)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.WriteString(XmlWriterDelegator xmlWriter,String value,XmlDictionaryString name,XmlDictionaryString ns)

   at WriteKeyValueOfstringstringToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,ClassDataContract)

  在System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context)

  在WriteArrayOfKeyValueOfstringstringToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,CollectionDataContract)

  在System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,Boolean verifyKnownType,RuntimeTypeHandle declaredTypeHandle)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle objectTypeHandle,Type objectType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle,Type
declaredType)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerializeWithSurrogate(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)

  在WriteGenericViewingMPStateToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,ClassDataContract)

  在System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,Boolean verifyKnownType,RuntimeTypeHandle declaredTypeHandle)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle objectTypeHandle,Type objectType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle,Type
declaredType)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerializeWithSurrogate(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteSerializationInfo(XmlWriterDelegator xmlWriter,Type objType,SerializationInfo serInfo)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteISerializable(XmlWriterDelegator xmlWriter,ISerializable obj)

  在System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContextComplex.InternalSerializeWithSurrogate(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)

  在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle)

   at WriteKeyValueOfstringStateSerializeSetjJgp3TWnToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,ClassDataContract)

  在System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context)

  在WriteArrayOfKeyValueOfstringStateSerializeSetjJgp3TWnToXml(XmlWriterDelegator,Object,XmlObjectSerializerWriteContext,CollectionDataContract)

  在System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext context)

  在System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer,Object graph)

  在System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer,Object graph)

  在System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer,Object graph)

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)

 

请让我知道为什么会发生此异常。是否有任何序列化大数据的restirction。目前,串行数据大约为250 MB,可能超过3 GB。

Please let me know why this exception occurs. Is there any restirction for serializing a large data. Currently the data that is seriailzed is around 250 MB and it may go beyond 3 GB.

 

 

 

推荐答案

XML Serlization占用比实际数据更多的内存来执行操作。因此,在某些情况下,如果内存不可用,您将看到内存错误。

XML Serlization takes more memory than you actual data to perform operations. So in some scenarios you will see the memory errors if the memory is not available.

如果您使用64位MSIL执行,那么它可以有更多的内存来进行序列化。

If you are using a 64bit MSIL to execute then it can have more memory to Serialize.

 

如果你有大量数据我会建议使用块方法或使用二进制格式化程序,它应该比xml serlization占用更少的内存。

If you are having large data i would suggest to go with chunk approach or use binary formatter which should take less memory than xml serlization.

 


这篇关于使用datacontractserializer时出现System.outofmemory异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆