CreateInstranceAndUnwrap slow perfomance

  • Thread starter Gnanaprakash Rathinam
  • Start date
G

Gnanaprakash Rathinam

Hi Expert,

We are trying to create an object in secondary appdomain from default
appdomain using CreateInstanceAndUnwrap api, But this call seems to be
taking lot of time to return, I know the call involve loading of required
assembly of the class/type and create object of type and return the proxy
object to the default domain. But I look for any other alternative way of
doing this???


// Sample c# code
class Test
{

static void Main()
{
CreateInstanceInSecondaryAppdomain(); // OK!
}

static void CreateInstanceInSecondaryAppdomain()
{
try
{

// Create a new AppDomain.
AppDomain ad =
AppDomain.CreateDomain("SecondaryAppDomain");


MarshalByRefType instance = (MarshalByRefType)
ad.CreateInstanceAndUnwrap(
Assembly.GetCallingAssembly().FullName,
"CreateInstanceAndUnWrap.MarshalByRefType",
true,
BindingFlags.Default,
null,
null,
null,
null,
null
);

// I'm done using the other AppDomain, so
// I'll unload it and all its assemblies.
AppDomain.Unload(ad);
}
catch (TypeLoadException e)
{
Console.WriteLine(e.Message);
}
}
}

class MarshalByRefType : MarshalByRefObject
{
//System.Runtime.Remoting.Activation.IActivator.
// This instance method can be called via a proxy.

public MarshalByRefType()
{
}
public void SomeMethod(String sourceAppDomain)
{
// Display the name of the calling AppDomain and my AppDomain.
// NOTE: The application's thread has transitioned between
AppDomains.
Console.WriteLine(
"Code from the '{0}' AppDomain\n" +
"called into the '{1}' AppDomain.",
sourceAppDomain,
Thread.GetDomain().FriendlyName);
}
}

Thanks,
GP.
 
M

Mujtaba Syed

Hi:
We are trying to create an object in secondary appdomain from default
appdomain using CreateInstanceAndUnwrap api, But this call seems to be
taking lot of time to return, I know the call involve loading of required

How much time?
You are using late binding and that too across AppDomains. It's definitely
not going to be as fast as a new ().

Mujtaba.
 
G

Gnanaprakash Rathinam

It is somewhere around 15-20 milliseconds on uniprocessor pentium 2.6GHz
with 1GB RAM
 
D

David Levine

Does it take that long each time you call CreateInstanceAndUnwrap or only
the 1st time you call it? It is a fairly heavy operation to create and
destroy an appdomain. When you destroy the appdomain all objects that are
rooted in it are garbage collected, and all finalizers of those objects run
to completion before the destroy call completes - this is time consuming.

Try running another timing test but don't include the appdomain
create/destroy in the test.
 
G

Gnanaprakash Rathinam

The timing include only the call to CreateInstanceAndUnwrap, but the second
call to CreateInstanceAndUnwrap takes very less time, close to 1
millisecond, so for first call this long time (~15ms) is required to load an
assembly into appdomain?
 
D

David Levine

Gnanaprakash Rathinam said:
The timing include only the call to CreateInstanceAndUnwrap, but the
second
call to CreateInstanceAndUnwrap takes very less time, close to 1
millisecond, so for first call this long time (~15ms) is required to load
an
assembly into appdomain?

That's probably the case. Loading an assembly is considered to be a "heavy"
operation, since it is a container for types, acts as a security boundary,
etc. There's a lot of overhead in loading the assembly, but once it is
loaded the results are cached.
 

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

No members online now.

Forum statistics

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

Latest Threads

Top