title: How can I clear the PooledBufferManager for my used once WCF HTTP client?
body: <p>I have a WCF client application which makes a single service call with a very large response (1GB). I'm finding making this service call uses a lot of memory (500MB) which seems to never get reclaimed even though the response objects are no longer referenced by my code.</p>
  
  <p>I've used a memory profiler to see much of the memory usage lies in byte arrays created by PooledBufferManager instances.</p>
  
  <p>The proxy/client I am using has been auto generated by Visual Studio, so it is a class derived from System.ServiceModel.ClientBase&lt; TChannel >.</p>
  
  <p>I am using a custom binding with the following configuration:</p>
  
  <pre><code>  &lt;customBinding&gt;
      &lt;binding name="foo"
               closeTimeout="00:01:00"
               openTimeout="00:01:00"
               receiveTimeout="00:01:00"
               sendTimeout="00:01:00"&gt;
        &lt;transactionFlow/&gt;
        &lt;reliableSession ordered="true" inactivityTimeout="00:02:00"/&gt;
        &lt;security authenticationMode="SecureConversation" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"&gt;
          &lt;secureConversationBootstrap messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10"&gt;
            &lt;localClientSettings maxClockSkew="23:59:00"/&gt;
          &lt;/secureConversationBootstrap&gt;
          &lt;localClientSettings maxClockSkew="23:59:00"/&gt;
        &lt;/security&gt;
        &lt;mtomMessageEncoding&gt;
          &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/&gt;
        &lt;/mtomMessageEncoding&gt;
        &lt;httpTransport maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/&gt;
      &lt;/binding&gt;
    &lt;/customBinding&gt;
  </code></pre>
  
  <p>Reading around people talk about a few solutions to this:</p>
  
  <ul>
  <li>switching to streaming response mode instead of buffered, but I believe this is for TCP WCF connections, not for HTTP like mine.</li>
  <li>setting maxBufferPoolSize to zero to disable buffer pools. Setting this in the httpTransport element doesn't seem to have any effect for me.</li>
  <li>calling Clear() on the appropriate BufferManager/PooledBufferManager. I can't find the object to call this on from the context of the ClientBase derived instance I have. I did manage to find the appropriate PooledBufferManager instance using the debugger and digging many levels deep into private innerChannelFactory fields, but this isn't useful to interact with it from code.</li>
  <li>manually invoking GC.Collect() seems to reclaim about 50MB of the outstanding 500MB.</li>
  </ul>
  
  <p>What would be the best way to reclaim as much of this memory as possible used by this one time service call? I'm on the verge of making the service call in a dedicated process I can kill to reclaim the memory at this point.</p>