Shabz
Shabz

Reputation: 31

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

I am trying to save a huge data to database through a WCF service call. I am not able to invoke the service. Its throwing a error.

codeProxy.SaveCodes(requestHeader, codes, Rawcodes);

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

I have configued the web config on server and client side to max limits.

client web.config

<binding name="WSHttpBinding_dataService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
                    <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>
                    <security mode="None">
                        <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>
                        <message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default" establishSecurityContext="true"/>
                    </security>
                </binding>

server web.config

<service name="ser.WSHttpBinding_dataService" behaviorConfiguration="ServiceBehavior">
      <endpoint contract="ser.IDataservice" binding="wsHttpBinding" bindingConfiguration="datacodeservice" />
      </service>

<wsHttpBinding>
        <binding name="datacodeservice" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
          <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false"/>
          <security mode ="None">
            <!--<message clientCredentialType="Certificate" />-->
          </security>
        </binding>


<serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceThrottling maxConcurrentSessions="100" />
          <dataContractSerializer maxItemsInObjectGraph="2147483646"/>

        </behavior>
      </serviceBehaviors>

Upvotes: 1

Views: 14591

Answers (2)

Liam
Liam

Reputation: 29624

If you encounter this issue on a .Net 2.0 service this hotfix may help

http://support.microsoft.com/kb/974065

Issue

When you are running a .NET Framework 2.0-based application, the application crashes. If you debug the application, you notice that a System.InsufficientMemoryException exception is thrown. Then, you receive an error message that resembles the following: Failed to allocate a managed memory buffer of bytes. The amount of available memory may be low.

CAUSE

Cause of Issue 1

This issue occurs intermittently when the application tries to allocate memory for a large object in a large object heap (LOH). The error message is triggered when the following conditions are true: The heap cannot provide sufficient memory to satisfy the LOH allocation request. A concurrent garbage collection is in progress at the same time. Cause of Issue 2

This issue occurs because garbage collector heap balancing for small object allocations is not performed diligently on computers that have more than 8 logical processors. Because of this, more garbage collections are triggered to maintain heap balancing when there is an unbalanced workload in different processors. Therefore, the application spends more time on garbage collection.

Upvotes: 1

Pavel
Pavel

Reputation: 554

Use Stream property in message contract of WCF operation to transfer large objects.

[MessageContract]
public class DocumentDescription
{
    [MessageBodyMember(Namespace = "http://example.com/App")]
    public Stream Stream { get; set; }
}

Configure your binding this way

<binding name="Binding_DocumentService" receiveTimeout="03:00:00"
        sendTimeout="02:00:00" transferMode="Streamed" maxReceivedMessageSize="2000000">
    <security mode="Transport" />
</binding>

To upload large data into SQL Server database use approach described in the following article Download and Upload images from SQL Server via ASP.Net MVC

Upvotes: 1

Related Questions