none
Implementing ISerializable throws OutOfMemoryException during Serialization RRS feed

  • Question

  • Hi all,

    I have used below code in GetObjectData

    public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                StringWriter sw = new StringWriter();
                this.datatableLog.WriteXml(sw, XmlWriteMode.WriteSchema);
                info.AddValue("EventsLog", sw.ToString(), typeof(string));
                sw.Close();

    But during serialization am getting OutOfMemory exception.

    my datatable has 12 columns and contains 2 lakh rows

    is there any alternative instead of StringWriter.

    Below is the exception am getting:

    StackTrace:
       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(String value)
       at System.IO.StringWriter.Write(String value)
       at System.Xml.XmlTextWriter.InternalWriteEndElement(Boolean longFormat)
       at System.Xml.XmlTextWriter.WriteEndElement()
       at System.Data.DataTextWriter.WriteEndElement()
       at System.Data.XmlDataTreeWriter.XmlDataRowWriter(DataRow row, String encodedTableName)
       at System.Data.XmlDataTreeWriter.Save(XmlWriter xw, Boolean writeSchema)
       at System.Data.DataTable.WriteXml(XmlWriter writer, XmlWriteMode mode, Boolean writeHierarchy)
       at System.Data.DataTable.WriteXml(TextWriter writer, XmlWriteMode mode, Boolean writeHierarchy)
       at System.Data.DataTable.WriteXml(TextWriter writer, XmlWriteMode mode)
       at Carrick.Framework.DTO.EventsListViewResponseDTO.GetObjectData(SerializationInfo info, StreamingContext context) in D:\Work_iQPlus\Carrick\Carrick.Framework\DTO\EventsListViewResponseDTO.cs:line 211
       at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
       at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter)
       at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)
       at Carrick.MessageBus.Framework.Services.Serializer.Serialize(Object objMessage, String strFileName, Int64& lngPayLoad)


    vinaya kumar

    Tuesday, June 5, 2012 3:27 PM

Answers

  • Hi Vinaya,

    Welcome to the MSDN Forum.

    Please take a look at this blog: http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/10/10/stringbuilder-outofmemoryexception.aspx 

    Unfortunately, there is no single answer to problems involving this error.  While some solutions exist for certain cases (for example, DataSets can be remoted using binary representation instead of XML, or code that uses StringBuilder directly can intialize the buffer with a size large enough to hold the data being manipulated to cut down on duplication from "growing" strings), the problem is a limitation of the design of elements from both the StringBuilder class and the LOH itself.  Microsoft is investigating these problems to find solutions that can both manage the overall problem, while still keeping the fundamental performance of the original design.

    And you can also try to change the platform to x64.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by RohitArora Wednesday, June 6, 2012 8:29 AM
    • Marked as answer by Vinaya Kumar Thursday, June 7, 2012 1:08 PM
    Wednesday, June 6, 2012 7:56 AM
    Moderator
  • But another change what i did is changing the datatype of each column to its respective types instead of string datacolumn.

    after that i was able to serialize upto 4 lakh records with 22 columns in datatable.

    thanks everyone for your reply.


    vinaya kumar

    Thursday, June 7, 2012 1:12 PM

All replies

  • Hi Vinaya,

    Welcome to the MSDN Forum.

    Please take a look at this blog: http://blogs.msdn.com/b/vsofficedeveloper/archive/2008/10/10/stringbuilder-outofmemoryexception.aspx 

    Unfortunately, there is no single answer to problems involving this error.  While some solutions exist for certain cases (for example, DataSets can be remoted using binary representation instead of XML, or code that uses StringBuilder directly can intialize the buffer with a size large enough to hold the data being manipulated to cut down on duplication from "growing" strings), the problem is a limitation of the design of elements from both the StringBuilder class and the LOH itself.  Microsoft is investigating these problems to find solutions that can both manage the overall problem, while still keeping the fundamental performance of the original design.

    And you can also try to change the platform to x64.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by RohitArora Wednesday, June 6, 2012 8:29 AM
    • Marked as answer by Vinaya Kumar Thursday, June 7, 2012 1:08 PM
    Wednesday, June 6, 2012 7:56 AM
    Moderator
  • The amount of data can be the culprit.

    Mark Answered, if it solves your question
    Rohit Arora

    Wednesday, June 6, 2012 8:29 AM
  • Thanks Mike

    It works great :)


    vinaya kumar

    Thursday, June 7, 2012 1:08 PM
  • But another change what i did is changing the datatype of each column to its respective types instead of string datacolumn.

    after that i was able to serialize upto 4 lakh records with 22 columns in datatable.

    thanks everyone for your reply.


    vinaya kumar

    Thursday, June 7, 2012 1:12 PM