Solved

XMLSerializer and WCF in a Windows Service gives OutOfMemoryExceptions

Posted on 2010-09-15
6
1,611 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Return array 3 30
Convert VB web project to C# 3 59
Using sample Autorize.net c# simple example 1 50
What are MicroServices? 4 64
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

742 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