Solved

XMLSerializer and WCF in a Windows Service gives OutOfMemoryExceptions

Posted on 2010-09-15
6
1,609 Views
Last Modified: 2012-06-21
In our application we are currently noticing that once a while our Windows Services generates an OutOfMemoryException on one of our WCF services. We have digged into the code and found out that WCF is in our case using a specific constructor from the XmlSerializer which causes an Assembly leak (Sort of Memory leak).

What this XmlSerializer does, is that it generates dynamic assemblies from  the schema's so that it can serialize and deserialize all the sent and received data according to the contract. This assembly is then attached to the AppDomain in which the Windows Service is running, but then never gets cleaned because the garbage collector won't do it. Everytime a WCF call is made a new XmlSerializer gets instantiated which causes the amount of generated assemblies to add slowly with in the end an OutOfMemoryException as the result.

The solutions we tried (and failed):
Using a static XmlSerializer: This works, it will generate only a few dynamic assemblies once, so you won't run into the OutOfMemoryException. However we can't simply inject this XmlSerializer into WCF so it can use our Serializer. It keeps generating new ones. Help on how to inject our static XmlSerializer in a dynamic way into WCF would be great.
Not use WCF: This is not an option. We are bound to WCF and we are not allowed to change it to WebRequest or something else.
Change the types: We tried to change the type to XmlElement, but for some reason the XmlSerializer tries to (de)serialize the received Xml to a XmlElement while this is not necesarry. The result is still a OutOfMemoryException.
Restarting service daily: We can not simply restart the windows service daily. This will interrupt processing too much.

We searched the web quite extensively and how the XmlSerializer works is according to MS-design, but I don't see any references to people who use it in combination with WCF and a Windows Service.

Any thoughts?
0
Comment
Question by:existenz2
  • 4
  • 2
6 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 33690659
One more option would be to break up the service into multiple services.  That way each service would be handling less assemblies.
0
 
LVL 14

Author Comment

by:existenz2
ID: 33698656
Would be possible, but then we still would run into a OOM-exception. I'm probably also not getting approval for such an architecural change, but it's definatly a direction I didn't look at yet so I'll give it a shot.

Anybody else with some thoughts?
0
 
LVL 14

Author Comment

by:existenz2
ID: 33714270
The multiple services option was indeed sadly not viable, because of our architectural constraints. Hopefully anybody else has some ideas left :)
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 33715224
Can you describe the Web Service in more detail?  How many methods?  How many assemblies? (is it possible to send sample code?[not sure if it would help])


How about converting the Windows Service to an IIS service.  With IIS you can assign multiple worker processes to the application and you can automatically recycle the worker processes.
0
 
LVL 14

Author Comment

by:existenz2
ID: 33715532
It's 4 methods in 1 assembly that are causing the problems. Sample code is not possible due to security restrictions. Converting it to a IIS services is neither allowed due to architectural constraints, so I'm pretty stuck in the corner of Windows Services.

One idea:
I could configure the Recovery settings to restart the application when it goes OOM. That would give us some kind of recycling of the worker processes in which the memory gets cleaned. It's not nice and definatly not the best way to do it, but it will probably work.
0
 
LVL 14

Author Closing Comment

by:existenz2
ID: 33723199
Thanks for the help. We managed to come up with a working solution inside WCF.

In the end it was pretty easy.

Awarding points for the help/thoughts which steered us in this direction.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Here I am going to explain creating proxies at runtime for WCF Service. So basically we use to generate proxies using Add Service Reference and then giving the Url of the WCF service then generate proxy files at client side. Ok, what if something ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question