WCF service memory leaks

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

[ServiceContract] public interface IService1 { [OperationContract] void DoService(); } [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)] public class Service1 : IService1 { public void DoService() { } } 

and they call him

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

Please remember that the service is published on basicHttpBindings.

Problem

Now that I have executed the client code above in a 1000 loop, I found a big difference between the performance counters โ€œAll bytesโ€ and โ€œPrivate bytesโ€ (I used the .net memory profiler). After the investigation, I found that some of the objects are incorrectly located. Below is a list of these objects (1000 invisible instances were found โ†’ equal to client calls)

(the 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 many undivided objects and how to control them.

Please, help

+6
memory-leaks wcf
source share
3 answers

I found a solution back in 2010, but I forgot to publish it. I really lost the exact track, but I remember that it was a bug of the .Net library, which was reported by Microsoft and was recognized by them. I do not have a link, but I would publish it as soon as I could find it. In any case, Microsoft fixed this problem in .net 4.0, and this is the exact solution that I implemented, I know that some of you may not be possible due to a change in the server environment, sometimes it is not in your hands.

0
source share

You request a new instance for each call (InstanceContextMode = InstanceContextMode.PerCall). If 1000 GC does not occur in calls, then service instances will be canceled. WCF requires you to implement IDisposable

From MSDN: Discover Powerful Instance Management Techniques for Developing WCF Applications

Services for Calls Services for Calls is the default creation mode of the Windows Communication Foundation. When a service type is configured to activate each call, a service instance, a common language runtime (CLR) object, exists only during a client call. Each client request receives a new dedicated service instance. Figure 2 shows how activation with a single call works.

Figure 2 Per-Call Instantiation
(source: microsoft.com )

  1. The client calls the proxy and the proxy forwards the service call.
  2. The Windows Communication Foundation creates the service instance and calls the method on it.
  3. After the method is called, it returns if the object implements IDisposable, then Windows Communication Foundation calls IDisposable. Dispose of it.
+4
source share

Have you accidentally turned on performance counters? How below?

 <system.serviceModel> <diagnostics performanceCounters="All" /> .. </system.serviceModel> 

In the paragraph "Increasing memory size for performance counters" at this link: http://msdn.microsoft.com/en-us/library/ms735098.aspx

"Private bytes" of rogues are mentioned when WCF performance counters are enabled. Changing it to ServiceOnly or turning it off completely can do the trick.

+1
source share

All Articles