I'm using an AppDomain in order to load assemblies and then unload them.
However, I'm having a very strage problem. After the AppDomain is unloaded - I can still see in process explorer that some assemblies are loaded multiple times! Why are there remainders of the loaded assemblies? Doesn't an AppDomain.Unload frees all the AppDomain's loaded memory?
You can see in the attached image:
Total AppDomains are 3 (I created 3 AppDomains in the process's life-cycle)
AppDomains: 1 (Currently only 1 AppDomain exists)
And for some reason, as you can see in the loaded DLL section bellow - Assemblies are loaded multiple times into the process..
Code:
AppDomain fetcherDomain = AppDomain.CreateDomain("StatusFetcher");
try
{
var fetcher = (LocalStatusFetcher)fetcherDomain.CreateInstanceFromAndUnwrap(Assembly.GetExecutingAssembly().CodeBase, typeof(LocalStatusFetcher).FullName);
//doing some other stuff that is not interesting...
}
finally
{
AppDomain.Unload(fetcherDomain);
}
And yes, LocalStatusFetcher does inherit MarshalByRefObject...
There's a very high possibility that the assemblies you're loading into the foreign application domain are being bled into the current one. There are tons of ways that this can happen, but your problem in particular is passing Assembly.GetExecutingAssembly().CodeBase
to the CreateInstanceFromAndUnwrap
method. A call to Assembly.GetExecutingAssembly()
loads the currently executing assembly into the current app domain, and passing the .CodeBase
property to
CreateInstanceFromAndUnwrap
will attempt to load the target assembly (located in the application path or in the GAC) into the target domain before instantiating your target proxy. Currently, I see nothing wrong with this code other than the possible bleeding issue.
If you have multiple application domains then you'll see multiple copies of the assembly in the LoadFrom context because the only assembly that is shared across AppDomains is mscorlib.dll. Unless I'm misunderstanding your question I think what you're seeing is normal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With