Solved

Need to unload an external assembly that is causing memory leak

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

13 Experts available now in Live!

Get 1:1 Help Now