Assembly.Load performance impact

I am working on a component that executes a preliminary method of any interface registered in ioc, and the execution time depends on different triggers. It should be able to save the actions that should be performed in the database, so I save the method name, type and parameter list (with serialization in BLOB) to the database until it is needed.

When the trigger starts, I need to execute the method on the type instance. Since I use dependency injection, I have an interface name stored in the database (in the format "Namespace.IInterface, AssemblyName" )

To run the Resolve<IInterface>() method in the ioc container, I need an instance of its Type :

 Assembly assembly = System.Reflection.Assembly.Load(assemblyName); Type service = assembly.GetType(typeName); object instance = IOCContainer.Resolve(service); 

My questions:

  • Is there a better way to get an instance of a type on its behalf if I'm sure that the containing assembly is already loaded into the application domain? (I tried just Type.Load(typeName) but got zero)
  • If the assembly is already loaded, will the CLR optimize this process (use the already loaded one), or do I need to manually cache the list of assemblies to prevent the performance impact on reloading the same assembly again and again?
+4
source share
4 answers
  • If the typeName you use includes the assembly name (something like MyNamespace.MyType, MyAssembly version=1.0.0.0,publicKeyToken=12345etc ), then Type.Load(typeName) will get your type, but not null;
  • The CLR takes care of loading the assembly only once (once for the context, details here , in your case the context remains the same, so the answer is that you should relax and leave the caching to CLR :)).
+4
source

Is there a better way to get an instance of a type from its name if I'm sure that the containing assembly is already loaded into the application domain? (I tried just Type.Load (typeName) but got null)

No.

If the assembly is already loaded, the CLR optimizes this process (use the already loaded)

Yes.

Each assembly is downloaded only once.

+3
source

If the assembly in question is already loaded, the CLR will optimize this process (use is already loaded)

The answer only to this question is in step 2 of the MSDN article How to Runtime Locates Assemblies :

If the requested assembler also requested in previous calls , the common environment uses the already assembled assembly . This can have consequences when assigning the nodes that make up the application. For more information on naming assemblies, see Assembly Names .


Not directly related to the issue, but also useful:

If the previous assembly request failed, subsequent assembly requests immediately fail without trying to load the assembly. Starting with the .NET Framework version 2.0, assembly binding failures are cached, and cached information is used to determine whether to attempt to load the assembly.

+2
source

To check the performance impact, I tried loading the same assembly for n times. And found that shared distributed memory grows with every call.

To try the following:

 while (true) { Assembly assembly = Assembly.LoadFrom("abc.dll"); //monitor: AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize //memory growing with each call } 

==============

Then I did this to make sure I load the assembly only once.

 var typeName = "Namespace.ClassName, Namespace"; while (true) { var typeFound = Type.GetType(typeName); if (typeName == null) { Assembly assembly = Assembly.LoadFrom("abc.dll"); } //monitor: AppDomain.CurrentDomain.MonitoringTotalAllocatedMemorySize //memory will not grow after first call } 
0
source

Source: https://habr.com/ru/post/1432234/


All Articles