views:

410

answers:

2

Dear Devs

I have a very small wcf service hosted in a console app.

[ServiceContract]
public interface IService1
{
    [OperationContract]
    void DoService();
}

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class Service1 : IService1
{
    public void DoService()
    {

    }
}

and its being called as

using (ServiceReference1.Service1Client client = new ServiceReference1.Service1Client())
{
    client.DoService(new DoServiceRequest());
    client.Close();
} 

Please remember that service is published on basicHttpBindings.

Problem

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)

(namespace for all of them is System.ServiceModel.Channels)

HttpOutput.ListenerResponseHttpOutput.ListenerResponseOutputStream
BodyWriterMessage
BufferedMessage
HttpRequestContext.ListenerHttpContext.ListenerContextHttpInput.ListenerContextInputStream
HttpRequestContext.ListenerHttpContext 

Questions Why do we have lot of undisposed objects and how to control them.

Please Help

+1  A: 

You're requesting a new instance per call (InstanceContextMode=InstanceContextMode.PerCall). If there is no GC happening in the 1000 calls then the service instances will be uncollected. WCF requires you implement IDisposable

From MSDN : Discover Mighty Instance Management Techniques For Developing WCF Apps

Per-Call Services Per-call services are the Windows Communication Foundation default instantiation mode. When the service type is configured for per-call activation, a service instance, a common language runtime (CLR) object, exists only while a client call is in progress. Every client request gets a new dedicated service instance. Figure 2 illustrates how this single-call activation works.

Figure 2 Per-Call Instantiation

  1. The client calls the proxy and the proxy forwards the call to the service.
  2. Windows Communication Foundation creates a service instance and calls the method on it.
  3. After the method call returns, if the object implements IDisposable, then Windows Communication Foundation calls IDisposable.Dispose on it.
Preet Sangha
I have seen this article before and its true that it call Dispose method automatically if there is any and this is only if you have opened some resource by yourself and need to cleanup in this case you are required to clean them by yourself. But in my case i have no resource to clean. However i have already tried this but same result. Moreover i would like to tell you that GC have cleaned the Heap but Native memory is not cleaned due to those objects were collected UNDisposed.
Mubashar Ahmad
A: 

http://geekswithblogs.net/bcaraway/archive/2008/07/06/123622.aspx

See this link...This may help you understand better...Dont use using in WCF proxy

chugh97
Dear Chugh97 this article doesn't fall in my problem because issue described in this article appears only if you need cleanup in case of fault. but i made 1000 successful call not applicable, however i have tried that one as well.
Mubashar Ahmad