Click here to Skip to main content
15,846,346 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used the below code for serialization,

using (MemoryStream buffer = new MemoryStream())
System.Runtime.Serialization.IFormatter serializationFormatter = new BinaryFormatter();
serializationFormatter.Serialize(buffer, tableItems);
serializedTableData = buffer.ToArray();

I am getting the below error message in this code : serializationFormatter.Serialize(buffer, tableItems);

Error Msg :
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.

   at System.IO.MemoryStream.set_Capacity(Int32 value)

   at System.IO.MemoryStream.EnsureCapacity(Int32 value)

   at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)

   at System.IO.BinaryWriter.Write(String value)

   at System.Runtime.Serialization.Formatters.Binary.BinaryObjectString.Write(__BinaryWriter sout)

   at System.Runtime.Serialization.Formatters.Binary.__BinaryWriter.WriteObjectString(Int32 objectId, String value)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteString(NameInfo memberNameInfo, NameInfo typeNameInfo, Object stringObject)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteKnownValueClass(NameInfo memberNameInfo, NameInfo typeNameInfo, Object data)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArrayMember(WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, Object data)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)

   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)

1 solution

We can't help you much with this: it needs your data to work out what is going on. It would appear from the stack trace that the system is trying to allocate a large enough buffer and failing - but we can't see how big it needs to be, or how much memory your system has already used.

So, put a breakpoint on the Serialize line, and start looking at your tableItems: how big is it? Bear in mind that there is an absolute limit of 2GB for any single object in .NET, so if this is a HUGE table, the memory stream it tries to allocate may not be possible - but as I say, we can't tell.

Get some information and it may be clearer.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900