unloading domain leaks ?

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()
{
}
}
}
 
E

eran.sandler

Hello greg,

I ran your sample program and by adding the "Virtual Bytes", "Private
Bytes" and "#Bytes in all heaps" I saw that there is no actual leak in
the sample code you sent.

The private bytes, virtual bytes and # bytes in all heaps all stay
flat.

There might be, however, a bug in the counters that unloading an
appdomain does not decrement the counter.

Hope it helps,
Eran
 
W

Willy Denoyette [MVP]

"Current Classes Loaded" is a global counter for all AppDomain's in the
current process.
This counter increments for each "proxy" class loaded in the default domain
with the call to CreateInstanceAndUnwrap. The counter indicates the number
of "classes loaded", that is not the curent "instances of the class".
As for the memory increase, running the code posted on v1.1 doesn't show
this increase, not sure what "memory" counter you are talking about though.

Willy.
 

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

Top