Solved

XMLSerializer and WCF in a Windows Service gives OutOfMemoryExceptions

Posted on 2010-09-15
6
1,602 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
Comment Utility
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
Comment Utility
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
Comment Utility
The multiple services option was indeed sadly not viable, because of our architectural constraints. Hopefully anybody else has some ideas left :)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Aspnet xml 2 36
Please explain "Promise Pattern" and how it's used 2 37
visual Basic Net vs c# net 8 53
Achieve json result 2 24
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now