How to increase the maximum continuous memory block in a bunch of large objects

The situation is that I am making a WCF call to a remote server that returns an XML document as a string.

In most cases, this return value is several K, sometimes several tens of K, and sometimes several hundred K, but very rarely it can be several megabytes (the first problem is that I don’t know),

These are such rare cases that cause grief. I get a stack trace that starts:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Xml.BufferBuilder.AddBuffer()
   at System.Xml.BufferBuilder.AppendHelper(Char* pSource, Int32 count)
   at System.Xml.BufferBuilder.Append(Char[] value, Int32 start, Int32 count)
   at System.Xml.XmlTextReaderImpl.ParseText()
   at System.Xml.XmlTextReaderImpl.ParseElementContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlTextReader.Read()
   at System.Xml.XmlReader.ReadElementString()
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderMDRQuery.Read2_getMarketDataResponse()
   at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer2.Deserialize(XmlSerializationReader reader)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
   at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)

, , , StringBuilder.EnsureCapacity OutOfMemoryException ( , , , , ). , .

, :

  • - ? . , , , . , , .
  • . , -, (, , , )
  • - LOH, ?... . 32- ( , ), ( , ).
  • LOH? perfmon, , , .

: - , ?

+5
4

TransferMode , , "", " " " StreamedResponse".

maxBufferPoolSize maxBufferSize. , .

maxReceivedMessageSize , , , , .

, , . , . WCF - .

, , , . MSDN basicHttpBinding, .

LOH, , . ( ), , .

+5

- WCF, LOH 32- , , 64 . 32- , 4 64- Windows. , .

+2

, - , XmlSerializer , MSDN:

, XML . . :

XmlSerializer.XmlSerializer()

XmlSerializer.XmlSerializer(, String)

- , , , .

, . XmlSerializer ( , ).

, , Tess. .

+1

, , , Xml , .

WCF, HttpRequest, XmlDeserializer . , . , , . LOH, .

The problem that I noticed when creating .NET buffers tends to double the capacity each time the buffer fills up, which causes memory fragmentation, since for a 10 MB document it is necessary to allocate memory for many stages. If you know the required buffer size in advance, it is more efficient to distribute it immediately. Therefore, if you know how large the incoming document is, you can create a StringBuilder with this size.

+1
source

All Articles