Memory Usage in .NET Assemblies

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

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

+4
source share
5 answers

You are right that the function of the DLL is that several processes using this DLL can share certain sections to reduce memory usage (this is a Windows function and does not apply to .Net), but Iโ€™m pretty sure that these two processes work you must load the same physical DLL onto disk, in which case this will not happen for the scenario you are describing.

If instead you install it in a normal place (for example, GAC), then Windows will be able to share certain parts of the DLL for several processes to save memory. For .net builds, you also need an NGen build to take advantage of this .

Please note that the amount of stored memory is not size_of_dll , since some parts of this DLL image cannot be shared, namely any section of this DLL that can be modified. These sections are still duplicated in several processes to ensure that applications do not accidentally modify each other's data.

+4
source

Yes, the DLL will load once in AppDomain. However, you can share the DLL through several AppDomain applications to reduce memory usage. As described in this code draft article .

If the assembly is used by several domains in the process, the assembly code (but not its data) can be used by all domains referring to the assembly. This reduces the amount of memory used at runtime.

+2
source

If you want several processes to share the same assembly, you have two options:

  • You can put the assembly in the GAC or Global Assembly Cache . You can do this using GacUtil .
  • You can use the configuration file and the codeBase element to share a private deployed assembly among a small group of applications.
+2
source

Tested with two .net 4.0 projects, one class library and Process Explorer. By default, Visual Studio copies links to dlls libraries to bin projects.

Process Explorer returns two different addresses for the referenced DLL, and 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 can be downloaded twice.

+2
source

I think you are asking if assembly code is loaded once per process or shared between processes such as native dll. The answer is not mainly because the code is not native, and it must be JIT. Jinting takes place in a process. Prebuilt assemblies (ngen) can share code.

0
source

Source: https://habr.com/ru/post/1481794/


All Articles