leaks unloading AppDomain

G

greg

Hi all,

Can anyone tell me why the code below leaks?
Here is what I do:
1) create new AppDomain
2) create an instance of AppDomain2Class in it,
3) call a AppDomain2Class.Run() method
4) unload AppDomain
No additional assemblies are involved ( even if they were, unloading AppDomain
supposed to unload all assemblies loaded in it)

Using PerfMon I can see that every call to
appDomain2.CreateInstanceAndUnwrap()
increases .NET CLR Loading "Current Classes Loaded" performance counter up
to 1 class
and it is never recovers back even after appdomain is unloaded

Also every 1000 iterations of the for loop looses around 525K of memory.

Is there something I missing here?

I am running this test under Windows XP SP2
..NET framework version: 1.1.4322.573

Cheers

Greg Volpert


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
namespace AppDomainTest
{
class AppDomain1Class
{
[STAThread]
static void Main(string[] args)
{
for (int counter = 0 ; counter < 1000 ; counter++ )
{
Console.WriteLine( counter.ToString() );
AppDomain appDomain2 = null;
try
{
// 1. Creating the new appdomain:
appDomain2 = AppDomain.CreateDomain("AppDomain2", null, null);

// 2. Creating an AppDomain2Class object in the new AppDomain
// and returning the proxy to it:
string strAssemlyName = typeof(AppDomain2Class).Assembly.GetName().Name;
string strTypeName = typeof(AppDomain2Class).FullName;
AppDomain2Class objTest = (AppDomain2Class)appDomain2.CreateInstanceAndUnwrap( strAssemlyName, strTypeName );

// 3. Calling the AppDomain2Class.Run() method remotely:
objTest.Run();
}
finally
{
// 4. Unloading appdomain and all its assemblies:
if ( appDomain2 != null )
AppDomain.Unload( appDomain2 );
}
}
}
}

public class AppDomain2Class : MarshalByRefObject
{
~AppDomain2Class()
{
// this is being called by GC, so it's not AppDomain2Class that is leaking
}

public void Run()
{
}
}
}
 
L

Lionel LASKE

My guess is that "Unload" marks your AppDomain freeable for garbage
collector but don't really unload your AppDomain from memory. May be when
your system reach the max simultanous AppDomain number, it launched a
garbage collector.

Lionel.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top