Solved

Need to unload an external assembly that is causing memory leak

Posted on 2012-04-01
2
388 Views
Last Modified: 2012-04-02
Hello,
I am maintaining a fax server Windows service written in C#. The service references a compiled external .NET library for imaging functions.
In several production environments, every few days, the imaging component starts to throw exceptions claiming that memory is corrupt, and after that it stops working altogether, crashing the service.
The component's developers are no help because this is an older version of the component which isn't supported, and currently I cannot upgrade. Also, this was tested in several production environments and occurs in all of them, so it is unlikely that this is really corrupted memory. From the steady increase in memory consumption I assume this is a memory leak on their part. Restarting the service always solves the problem.
I would like to unload the reference to the external component and re-load it after each call. I prefer this to restarting the service automatically on a schedule. However, I have tried to dynamically load the component using Assembly.LoadFrom and using reflection, but the component throws generalized meaningless exceptions whenever I invoke any methods.
Can anyone suggest how to do this?
Thanks!
0
Comment
Question by:rumblefish978
2 Comments
 
LVL 22

Accepted Solution

by:
ambience earned 400 total points
ID: 37794940
One solution is to load the assembly in a separate AppDomain rather than the main domain, unloading the AppDomian would reclaim resources - however that too does not work very well with unmanaged leaks for example. The referenced components may be using OS resources or native heap and such leaks can not be prevented even using AppDomain.

The best solution is to put the component in a separate (surrogate) exe process that remotes or use WCF if you wish. Remoting is pretty easy to setup and you may even do the hosting part without coding (just config). That exe can be recycled for each call and unloading a process will reclaim even leaked resources (at least most of them).
0
 

Author Closing Comment

by:rumblefish978
ID: 37794951
Thanks for the short but thorough explanation. I will implement it the way you said.
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

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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

19 Experts available now in Live!

Get 1:1 Help Now