Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET assemblies memory usage

Suppose I have two total separate .NET apps, Foo.exe and Boo.exe, both of which are using the same library TestLib.dll which is located in the bin folder of Foo and Boo (two copies).

Will .NET load both of these dlls, wasting size_of_dll * 2 size of RAM or it will check strong assemblies names, sizes etc... and load and load only one of the two assemblies?

like image 743
Brans Ds Avatar asked Dec 26 '22 03:12

Brans Ds


1 Answers

You are correct that a feature of DLLs is that multiple processes both using that DLL can share certain sections in order to reduce memory usage (this is a Windows feature and is not specific to .Net), however I'm fairly certain that for this to work the two processes must load the same physical DLL on disk, in which case this won't be happening for the scenario you describe.

If you instead installed into a common location (e.g. the GAC) then Windows would be able to share certain portions of the DLL across multiple processes in order to save memory. In the case of .net assemblies you also need to NGen the assembly in order to take advantage of this

Note that the amount of memory "saved" is not size_of_dll as certain parts of that DLL image cannot be shared, namely any section of that DLL which may be modified. These sections are still duplicated across multiple processes to ensure that applications don't accidentally modify each others data.

like image 122
Justin Avatar answered Jan 04 '23 23:01

Justin