23

If one calls Assembly.Load multiple times does it cause any side effects?

e.g.

for (int i = 0; i < N; i++) 
{
   Assembly.Load(assemblyStrongName);
   // .......
}

This loads the assembly one time doesn't it? I've checked with AppDomain.CurrentDomain.GetAssemblies() before and after and it seems it's loaded one time (as it should) but does it have side effects?

In a long running server application (runs for months/years with no restart) does the above cause any issues?

davioooh
  • 23,742
  • 39
  • 159
  • 250
JohnDoDo
  • 4,720
  • 8
  • 31
  • 47
  • 2
    If you have a system that, the loading of assemblies is the critical part of it staying active for months/years, I'm impressed. – Damien_The_Unbeliever Feb 16 '12 at 18:07
  • 1
    Corollary - if you're chasing a memory leak, use appropriate tools to identify the actual objects being leaked, rather than looking at your source code and randomly guessing at would "could" be the leak. – Damien_The_Unbeliever Feb 16 '12 at 18:14
  • 1
    @Damien_The_Unbeliever: I'm not chasing memory leaks. I've never manually loaded an assembly 'till now so It was just (programmer) curiosity :). – JohnDoDo Feb 17 '12 at 08:59

1 Answers1

25

This loads the assembly one time doesn't it?

Yes. The assembly gets loaded into the current AppDomain, and will only be loaded once into that AppDomain. Calling this multiple times just returns the existing assembly.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 3
    @JohnDoDo No - it should just return the existing Assembly. It's impossible to load an assembly >1 time in an AppDomain, and once loaded, it'll never unload (until the AppDomain is shut down) – Reed Copsey Feb 17 '12 at 17:16
  • 5
    If Assembly.Load(byte[] rawbytes) is used (bytes instead of a filename as a parameter), then the assembly will be added to the collection (AppDomain.GetAssemblies()) on every call. – J. Ouwehand Jun 22 '18 at 18:25