OutOfMemory exception from BinaryFormatter.Deserialize, coming from StringBuilder internal call

I have a .NET 4 WCF service that sends to the client some large objects (~ 115 MB) that are deserialized by the client. The first time you enter the object, the penalty is deserialized. However, all subsequent calls are called OutOfMemoryException. I checked that all of mine IDisposableswere wrapped in blocks using. I examined other issues, such as the BinaryFormatter outofmemory deserialization exception and Deserialize from MemoryStream throwing an OutOfMemory exception in C # . I tried some of the solutions recommended by people, including Simon Hewitt Optimized Serializer . However, in the end, he still relies on BinaryFormatterto deserialize objects.

I caught OutOfMemoryExceptionand looked at the stack trace (see below). It seems that the trace comes from a problem with memory usage in the class StringBuilder. I read other articles about how StringBuildermemory problems can be caused by the algorithm (length * 2) that they use when more space is required.

at System.Text.StringBuilder.ToString()    
at System.IO.BinaryReader.ReadString()    
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectString(BinaryHeaderEnum binaryHeaderEnum)    
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()    
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)    
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)    
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)

Is there a way to make it BinaryFormatterwork differently and not use it, StringBuilderor is there a good alternative BinaryFormatterthat better manages memory?

+5
source share
1 answer

BinaryFormatter - ( , ). , , , .., , protobuf-net json.net .

+1

All Articles