none
Failed to allocate a managed memory buffer of 268435456 bytes. The amount of available memory may be low. RRS feed

  • Question

  • Hi Team,

    I am using WCF Service in my WPF Application.It is self hosted service.When i ran this from visual studio i am using wsHttpBinding and using netcpbinding when it is deployed in server.I am getting the following error when i ran from both visual studio using wshttpbinding and ran from server.My service is returning large data like 180000 List of Objects.Kindly provide a solution to overcome this error.Below the configuration and error details.

    WCF Binding configuration:

    <bindings>
    <binding name="NetTcpBindingEndpoint" closeTimeout="00:10:00"
    					openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
    					transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
    					hostNameComparisonMode="StrongWildcard" listenBacklog="10"
    					maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxConnections="10"
    					maxReceivedMessageSize="2147483647">
    					<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647"
    						maxBytesPerRead="4096" maxNameTableCharCount="16384" />
    					<reliableSession ordered="true" inactivityTimeout="00:10:00"
    						enabled="false" />
    					<security mode="Transport">
    						<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
    						<message clientCredentialType="Windows" />
    					</security>
    				</binding>
    			</netTcpBinding>
    			<wsHttpBinding>
    				<binding name="WSHttpBinding_Service" closeTimeout="00:10:00"
    					openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
    					bypassProxyOnLocal="false" transactionFlow="true" hostNameComparisonMode="StrongWildcard"
    					maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
    					messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
    					allowCookies="false">
    					<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
    						maxBytesPerRead="4096" maxNameTableCharCount="16384" />
    					<reliableSession ordered="true" inactivityTimeout="00:10:00"
    						enabled="false" />
    					<security mode="Message">
    						<transport clientCredentialType="Windows" proxyCredentialType="None"
    							realm="" />
    						<message clientCredentialType="Windows" negotiateServiceCredential="true"
    							algorithmSuite="Default" establishSecurityContext="true" />
    					</security>
    				</binding>
    			</wsHttpBinding>
    		</bindings>
    <behaviors>
    			<endpointBehaviors>
    				<behavior name="maxItemsInObjectBehavior">
    					<dataContractSerializer maxItemsInObjectGraph="2147483647" />
    				</behavior>
    			</endpointBehaviors>
    		</behaviors>


    Exception Details:

    Exception Type: System.InsufficientMemoryException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 

    Message: Failed to allocate a managed memory buffer of 268435456 bytes. The amount of available memory may be low.

    Stack Trace:

    System.Runtime.Fx.AllocateByteArray(Int32 size)
    System.Runtime.InternalBufferManager.PooledBufferManager.TakeBuffer(Int32 bufferSize)
    System.Runtime.BufferedOutputStream.AllocNextChunk(Int32 minimumChunkSize)
    System.Runtime.BufferedOutputStream.WriteCore(Byte[] buffer, Int32 offset, Int32 size)
    System.Runtime.BufferedOutputStream.Write(Byte[] buffer, Int32 offset, Int32 size)
    System.Xml.XmlStreamNodeWriter.FlushBuffer()
    System.Xml.XmlStreamNodeWriter.UnsafeWriteUTF8Chars(Char* chars, Int32 charCount)
    System.Xml.XmlStreamNodeWriter.WriteUTF8Chars(String value)
    System.Xml.XmlUTF8NodeWriter.WriteStartElement(String prefix, String localName)
    System.Xml.XmlUTF8NodeWriter.WriteStartElement(String prefix, XmlDictionaryString localName)
    System.Xml.XmlSigningNodeWriter.WriteStartElement(String prefix, XmlDictionaryString localName)
    System.Xml.XmlBaseWriter.WriteStartElement(String prefix, XmlDictionaryString localName, XmlDictionaryString namespaceUri)
    System.Runtime.Serialization.XmlWriterDelegator.WriteStartElementPrimitive(XmlDictionaryString localName, XmlDictionaryString namespaceUri)
    System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteString(XmlWriterDelegator xmlWriter, String value, XmlDictionaryString name, XmlDictionaryString ns)
    WriteAccountSearchDTOToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
    System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
    System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
    System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
    System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
    WriteArrayOfAccountSearchDTOToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
    System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
    System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
    System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
    System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
    WriteResultListOfAccountSearchDTOQ6p0XEGzToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
    System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
    System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
    System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
    System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
    System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
    System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
    System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
    System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
    System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph)
    System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
    System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(XmlDictionaryWriter writer, MessageVersion version, Object[] parameters, Object returnValue, Boolean isRequest)
    System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)
    System.ServiceModel.Channels.BodyWriterMessage.OnWriteBodyContents(XmlDictionaryWriter writer)
    System.ServiceModel.Security.SecurityAppliedMessage.WriteBodyToSignThenEncryptWithFragments(Stream stream, Boolean includeComments, String[] inclusivePrefixes, EncryptedData encryptedData, SymmetricAlgorithm algorithm, XmlDictionaryWriter writer)
    System.ServiceModel.Security.WSSecurityOneDotZeroSendSecurityHeader.ApplyBodySecurity(XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator)
    System.ServiceModel.Security.SecurityAppliedMessage.OnWriteMessage(XmlDictionaryWriter writer)
    System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota)
    System.ServiceModel.Channels.TextMessageEncoderFactory.TextMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
    System.ServiceModel.Channels.HttpOutput.SerializeBufferedMessage(Message message, Boolean shouldRecycleBuffer)
    System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
    System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.SendReplyCore(Message message, TimeSpan timeout)
    System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.SendReply(Message message, TimeSpan timeout)
    System.ServiceModel.Channels.HttpRequestContext.OnReply(Message message, TimeSpan timeout)
    System.ServiceModel.Activation.HostedHttpContext.OnReply(Message message, TimeSpan timeout)
    System.ServiceModel.Channels.RequestContextBase.Reply(Message message, TimeSpan timeout)
    System.ServiceModel.Channels.SecurityChannelListener`1.SecurityRequestContext.OnReply(Message message, TimeSpan timeout)
    System.ServiceModel.Channels.RequestContextBase.Reply(Message message, TimeSpan timeout)
    System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Reply(MessageRpc&amp; rpc)

    Thanks,

    Suresh Rajamani



    Monday, June 5, 2017 7:58 PM

All replies

  • >> Failed to allocate a managed memory buffer of 268435456 bytes. The amount of available memory may be low

    Based on this error, I suggest you check the computer memory whether it is a limit.

    From WCF Binding configuration, I found you used Buffered transferMode. Buffered transfers hold the entire message in a memory buffer until the transfer is complete. I would suggest you switch to Streamed. Streamed transfers only buffer the message headers and expose the message body as a stream, from which smaller portions can be read at a time.

    You could refer the link below for more information.

    # https://msdn.microsoft.com/en-us/library/system.servicemodel.transfermode(v=vs.110).aspx

    Note, Streaming is only available with following system-defined bindings: BasicHttpBinding, NetTcpBinding, NetNamedPipeBinding, WebHttpBinding.

    Reference: https://msdn.microsoft.com/en-us/library/ms733742(v=vs.110).aspx


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 6, 2017 2:24 AM