Reputation: 4117
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?
Upvotes: 4
Views: 3335
Reputation: 7612
I think you are asking whether the code of the assembly is loaded once per process or shared between processes like native dlls. Answer is no mainly because the code is not native and it has to be JIT. Jitting happens per process. Pre-jitted assemblies (ngen) are able to share code.
Upvotes: 0
Reputation: 86729
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.
Upvotes: 4
Reputation: 5771
Tested this with two .net 4.0 projects, one class library and Process Explorer - by default Visual Studio copies referenced dlls to projects' bin
directory.
Process Explorer returns two different addresses for the referenced dll, while the same address for other, windows-specific libraries.
Conclusion: Yes, for two copies of the same library in different folders, loaded by two applications, the same library may be loaded twice.
Upvotes: 2
Reputation: 3440
If you want multiple processes to share the same assembly, you have two options:
GAC
or Global Assembly Cache
. You can do this by using GacUtil.Upvotes: 2
Reputation: 176
Yes, the DLL will be loaded once per AppDomain. However you you can share the DLL's across multiple AppDomain's to reduce the memory usage. As described in this code project article.
If an assembly is used by multiple domains in a process, the assembly's code (but not its data) can be shared by all domains referencing the assembly. This reduces the amount of memory used at run time.
Upvotes: 2