Assemblies not unloading after AppDomain unload?
Asked Answered
P

1

6

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...

enter image description here

Porky answered 18/11, 2012 at 12:21 Comment(7)
Perhaps you are loading the assemblies in your main AppDomain too. By the way, the image resolution is too low and I cannot see anything.Redhead
I thought of that, but it's not the case (checked with debugger) - Also, assemblies appear more than once only when they are loaded into multiple AppDomains. Regarding the picture quality - it's stackoverflow that does that, if you take the image's url and paste it in another tab you'll be able to see it fine.Porky
The assemblies that are visible from your screen-shot are all .NET Framework assemblies. Does this happen with your assemblies as well?Redhead
Yes. From my observations it happens to all assemblies that were loaded before the creation of the new AppDomain. e.g. if an assembly was NOT loaded before the new AppDomain creation - it will be released properly when the new AppDomain unloads.Porky
Could you post your code that creates the AppDomains and loads the assemblies?Redhead
Added the code. The assemblies are loaded by themselves when I touch classes that are implemented in them, I do not explicitly load these assemblies.Porky
#4432641Britt
C
2

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.

Crockett answered 26/11, 2012 at 1:50 Comment(4)
The current assembly is ofcourse already loaded to the current app domain, so I don't see how calling GetExecutingAssembly could be problematic. besides, I know that the other app domain will load these assemblies, what I don't understand is why it doesn't release them when it's done? I don't think that what I'm seeing is normal - The concept of app domain is that after it's released - all of the assemblies loaded to it are released as well...Porky
1. How are you loading them into the alternate app domain? 2. Are you referencing the loaded assemblies in any context in the current app domain?Crockett
I am referencing them and using them in the current context. This is not a problem. I expect them to be loaded - The question is why are they loaded twice? I unloaded the other AppDomain, they should return to being loaded only once.Porky
Hmm, interesting indeed. Can you confirm that a call to AppDomain.CurrentDomain.GetAssemblies() in the current domain only lists the assemblies you're loading once?Crockett

© 2022 - 2024 — McMap. All rights reserved.