4

I am curious to know what the disadvantage is by calling Assembly.Load(AssemblyName) numerous times with the same version of assembly. Does the runtime know not to load the assembly again after the first call? If not, is there any way to detect what's already loaded?

Thanks in advance.

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
weilin8
  • 2,975
  • 3
  • 32
  • 36
  • possible duplicate of [Multiple Assembly.Load(Byte\[\]), same instance or leak ?](http://stackoverflow.com/questions/1554404/multiple-assembly-loadbyte-same-instance-or-leak) – Fosco Jun 03 '11 at 19:47
  • That phrase "Note that this method overload always creates a new Assembly object with its own mapping" is not included in the MSDN doc for this overload function. That's why I am curious to know if they behave different. – weilin8 Jun 03 '11 at 19:50
  • possible duplicate of: http://stackoverflow.com/questions/1554404/multiple-assembly-loadbyte-same-instance-or-leak – Akhil Jun 03 '11 at 19:45

1 Answers1

10

When you use this overload it will be loaded only once in memory. You can verify it with Process Explorer. Look at the loaded modules list. Every assembly is loaded up to .NET 3.5 with LoadLibrary. Additionally it is loaded as memory mapped file into the process.

Starting with .NET 4.0 an assembly is loaded only as memory mapped file and not via LoadLibrary anymore except if it is a precompiled ngenned assembly.

This breaking change in .NET 4 was done because MS found during the development of VS 2010 that their memory did deplete rather quickly. Somebody found that every loaded assembly cosumed twice its original size in virtual memory because it was loaded once via LoadLibrary and a second time as memory mapped file. This is not easy to find except if you look with VMMap into your process. Due to the massive amount of code in VS this was a major issue for VS2010 which is now mostly managed.

Alois Kraus
  • 13,229
  • 1
  • 38
  • 64