title: Memory & Handle Leaks in my ServiceHost
body: <p>I've got a ServiceHost in a service app that communicates with 1700 clients every 60 second. When I start the service, it climbs to about 1500 open handles almost immediately, then continues to add another 300 after about 5 minutes (and continues onward continuously after that.)</p>
  
  <p>I've looked at ProcessExplorer and in Split view, it shows hundreds of handles of type File named "Device\Afd" - which is a representative of the TCP socket being used to communicate (I believe). </p>
  
  <p>I'm only assuming that my handle leak is related to the ServiceHost because it represents the highest number of handles observed from Process Explorer. What I'm wondering is why doesn't the service close these? Do I need to set a timeout of some sort or do I need to actively close them myself somewhere?</p>
  
  <p>Here's how my ServiceHost is created:</p>
  
  <pre><code>wcfObject = new WcfObject();
  host = new ServiceHost(wcfObject, baseWcfAddress);
  
  ServiceBehaviorAttribute attribute = (ServiceBehaviorAttribute)host.Description.Behaviors[typeof(ServiceBehaviorAttribute)];
  attribute.ConcurrencyMode = ConcurrencyMode.Multiple;
  attribute.InstanceContextMode = InstanceContextMode.Single;
  </code></pre>
  
  <p>And in my app.config:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="WcfObject" behaviorConfiguration="DefaultBehavior"&gt;
          &lt;host&gt;
            &lt;baseAddresses &gt;
              &lt;!-- Defined in code --&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
          &lt;endpoint name="NetTcpEndPoint" address="" binding="netTcpBinding" bindingConfiguration="netTcpBinding" contract="IWcfObject"/&gt;
          &lt;endpoint name="NetTcpMetadataPoint" address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding name="netTcpBinding" maxReceivedMessageSize="9655360" maxBufferSize="9655360" maxBufferPoolSize="524288"&gt;
            &lt;readerQuotas maxArrayLength = "932000" maxStringContentLength="900000" maxDepth="32"/&gt;
            &lt;security mode="None"&gt;&lt;/security&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="DefaultBehavior" &gt;
            &lt;serviceMetadata httpGetEnabled="False" httpGetUrl="" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="True"/&gt;
            &lt;serviceThrottling maxConcurrentCalls="50" maxConcurrentSessions="100" /&gt;          
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>