Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the disadvanges of calling Assembly.Load(AssemblyName) with the same assembly multiple times?

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.

like image 851
weilin8 Avatar asked Dec 28 '22 19:12

weilin8


1 Answers

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.

like image 147
Alois Kraus Avatar answered Jan 12 '23 09:01

Alois Kraus