0

Despite this is a .Net question about dlls this is a Linux question as I am deploying to Linux.

I deploy dozens of different .Net applications to a single Linux computer.

Each of the separate applications have several .dll files that are identical between the applications.

Here are my questions: Does .Net/Mono optimize memory usage automatically by recognizing that despite being separate files they are identical?

What can I do to move those files to a central location so they would be loaded into shared memory and reduce memory usage?

My questions are based on my experience with how .so files are handled in various UNIX based systems.

For reference, here is a dump of the .dlls in one application:

docroot@machine:~/Deployment/PrimaryApp_2023-07-12_01-16-59$ file *.dll */*.dll
AppConfigSdk.dll:                                      PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
AppLoginSdk.dll:                                       PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Dapper.Transaction.dll:                                PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Dapper.dll:                                            PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
DataAccess.dll:                                        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Microsoft.AspNetCore.Mvc.Razor.Extensions.dll:         PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.dll: PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Microsoft.AspNetCore.Razor.Language.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Microsoft.CodeAnalysis.CSharp.dll:                     PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Microsoft.CodeAnalysis.Razor.dll:                      PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Microsoft.CodeAnalysis.dll:                            PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Microsoft.Extensions.DependencyModel.dll:              PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
NLog.Extensions.Logging.dll:                           PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
NLog.Web.AspNetCore.dll:                               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
NLog.dll:                                              PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
Newtonsoft.Json.dll:                                   PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
System.Data.SqlClient.dll:                             PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
PrimaryApp.dll:                                        PE32 executable (console) Intel 80386 Mono/.Net assembly, for MS Windows
cs/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
cs/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
de/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
de/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
es/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
es/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
fr/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
fr/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
it/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
it/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
ja/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
ja/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
ko/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
ko/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
pl/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
pl/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
pt-BR/Microsoft.CodeAnalysis.CSharp.resources.dll:     PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
pt-BR/Microsoft.CodeAnalysis.resources.dll:            PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
ru/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
ru/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
tr/Microsoft.CodeAnalysis.CSharp.resources.dll:        PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
tr/Microsoft.CodeAnalysis.resources.dll:               PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
zh-Hans/Microsoft.CodeAnalysis.CSharp.resources.dll:   PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
zh-Hans/Microsoft.CodeAnalysis.resources.dll:          PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
zh-Hant/Microsoft.CodeAnalysis.CSharp.resources.dll:   PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows
zh-Hant/Microsoft.CodeAnalysis.resources.dll:          PE32 executable (DLL) (console) Intel 80386 Mono/.Net assembly, for MS Windows

[EDIT] To clarify... Two processes on AIX that load the same .so file will share the physical memory of the readonly code block from that .so. That is, the first process loads the .so from the disk, but the second process fixes up a page table pointing to the same physical memory. I am guessing Linux has a similar optimization, but I am not sure.

1 Answer 1

1

What can I do to move those files to a central location so they would be loaded into shared memory and reduce memory usage?

That's not how shared memory works/what it's used for. Memory between different processes can only be shared by means of the operating system; mono is not very involved at all. Under Linux, KSM can do that.

For that, mono has to annotate the pages it uses as MADV_MERGEABLE, and you need to enable KSM (see the linked page). Also note that KSM weakens the boundaries between processes, so on some systems it might be disabled for security reasons.

I don't know whether mono advises the kernel that assemblies loaded can be shared (my guess is that it doesn't). I quickly checked the source code for any indications it knows about MADV_MERGEABLE, but can't find any. It might be an easy and effective addition to the core, so I guess the maintainers would welcome a patch should you decide to improve that.

1
  • Thank you, your answer gives me some good research points. Commented Aug 24, 2023 at 14:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.