title: WCF Service Memory Leaks
body: <p>I have a very small wcf service hosted in a console app. </p>
  
  <pre><code>[ServiceContract]
  public interface IService1
  {
      [OperationContract]
      void DoService();
  }
  
  [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
  public class Service1 : IService1
  {
      public void DoService()
      {
  
      }
  }
  </code></pre>
  
  <p>and its being called as</p>
  
  <pre><code>using (ServiceReference1.Service1Client client = new ServiceReference1.Service1Client())
  {
      client.DoService(new DoServiceRequest());
      client.Close();
  } 
  </code></pre>
  
  <p>Please remember that service is published on basicHttpBindings. </p>
  
  <p><strong>Problem</strong></p>
  
  <p>Now when i performed above client code in a loop of 1000 i found big difference between "All Heap bytes" and "Private Bytes" performance counters (i used .net memory profiler). After investigation i found some of the objects are not properly disposed 
  following are the list of those objects (1000 undisposed instance were found --> equals to the client calls)</p>
  
  <p>(namespace for all of them is System.ServiceModel.Channels)</p>
  
  <pre><code>HttpOutput.ListenerResponseHttpOutput.ListenerResponseOutputStream
  BodyWriterMessage
  BufferedMessage
  HttpRequestContext.ListenerHttpContext.ListenerContextHttpInput.ListenerContextInputStream
  HttpRequestContext.ListenerHttpContext 
  </code></pre>
  
  <p><strong>Questions</strong>
  Why do we have lot of undisposed objects and how to control them.</p>
  
  <p>Please Help</p>