Solved

XMLSerializer and WCF in a Windows Service gives OutOfMemoryExceptions

Posted on 2010-09-15
6
1,603 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

895 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

14 Experts available now in Live!

Get 1:1 Help Now