Solved

Need to unload an external assembly that is causing memory leak

Posted on 2012-04-01
2
390 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 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