[ Memory consumption ] Use flat layout for IL-only assemblies
Hello.
We propose to reduce consumption of memory, which is used for "IL-only" (without native code) assembly images.
Currently, "IL-only" assemblies are being mapped to memory by using map per section.
First, the assemblies are tried to be mapped using MAPMapPEFile.
If the try is successful, then assembly is directly mapped to memory, and is registered through MappedImageLayout. This is good case from view point of memory consumption, because physical memory which is used for the assembly image, will be allocated only once, and then will be shared between processes. So, per-application memory consumption is optimal in this case.
Otherwise, if MAPMapPEFile fails, then ConvertedImageLayout is used to load the assembly.
This type of layout allocates anonymous memory, and copies contents of the sections to the allocated memory. This is bad case from view point of memory consumption, as physical memory is allocated separately for each process that loads the assembly.
One of the reason why MAPMapPEFile could fail, is alignment of assembly sections in file.
If the sections are aligned on a value, which is less than size of virtual page, then the function denies to perform direct mapping (this behaviour seems reasonable, because some platforms deny to directly map misaligned file content). The case might be quite frequent, so memory consumption is higher than possible, and can be reduced.
We propose to use FlatImageLayout for "IL-only" assemblies to reduce per-application memory consumption by sharing the memory, which is used for "IL-only" assembly images. The flat layout doesn't consider section alignment, so can directly map assembly to memory.
Actual improvement depends on sizes of "unaligned" non-precompiled assemblies, which are loaded to several processes at once.
Estimation on Tizen Xamarin GUI Application (specifically, Puzzle.Tizen - see tables at https://github.com/dotnet/coreclr/issues/10380#issuecomment-291146637) is about 0.7 megabytes (10%) savings of per-application memory.
@lemmaa @egavrin @Dmitri-Botcharnikov @gbalykov
We plan to open pull request for this change in two days.
Nice - thank you for identifying this issue!
@jkotas, Please, suggest, how is it better to implement tests for changes like these. What is the recommended way in such cases?
Is it a good idea to implement a separate unit test for this, or is it better to simply add a local assertion, like if image is for IL-only assembly without writeable sections, then layout is "flat" in code of PE loader?
Is it a good idea to implement a separate unit test for this, or is it better to simply add a local assertion, like
if image is for IL-only assembly without writeable sections, then layout is "flat"in code of PE loader?
I looked to the question from another view point. As interfaces still allow requirement of "Mapped" layout for IL-only assemblies, the assertion will not be valid.
So, maybe we need a performance-like test that just checks that memory consumption doesn't increase back after some change. Is it worthwhile to add such a test?
Agree. The performance test would be most appropriate since this is essentially a performance fix.
@valenis Do we have a good place to add working set performance tests on Linux?
@valenis, could you, please, suggest a good place to add a test for detecting regressions related to size of working set?
CC @mikem8361 who is working to validate the changes against Debugger/Profiler tests. We cannot merge this until that is complete.
CC @lt72 @janvorli
@mikem8361 FYI. MIke has been lookign at the changes and he is trying to run the debugger tests against this change. We are currently experiencing some issues but Mike may have more info later this week or early next week.
@lt72 @mikem8361 @ruben-ayrapetyan Few things about this:
- We need to get status of debugger signoff on this change. @lt72 @mikem8361 Where are we with it?
- This is a key feature change and would need to be vetted by shiproom, assuming (1) above is complete.
@ruben-ayrapetyan If this does not make 2.0 release but comes in a future release (e.g. say 2.1), will that work for you? If not, then you may want to consider having this change only in Tizen branch and merge it back into master branches once (1) is done and master is targeting our next release (2.1).
I finally got our debuggertests repo working on .NETCore 2.0 and now trying to verify ruben’s changes.
If this does not make 2.0 release but comes in a future release (e.g. say 2.1), will that work for you?
@gkhanna79, Yes, release 2.1 is fine for these changes.
Thanks for the confirmation @ruben-ayrapetyan - I have moved this issue to 2.1.
@gkhanna79, Thank you.
This has been verified against the debugger tests and the tests succeed.
This has been verified against the debugger tests and the tests succeed.
@mikem8361, thank you very much for verification.
@mikem8361 Just making sure ... have you run the debugger tests on Linux? This is Linux specific change.
Yes.
If this does not make 2.0 release but comes in a future release (e.g. say 2.1), will that work for you? If not, then you may want to consider having this change only in Tizen branch and merge it back into master branches once (1) is done and master is targeting our next release (2.1).
@jkotas, @gkhanna79, could you, please, check this, if master is now targetting the 2.1 release?
could you, please, check this, if master is now targetting the 2.1 release?
Yes, master is targeting the 2.1 release now.
Yes, master is targeting the 2.1 release now.
@jkotas, thank you for your answer. Is it now possible to merge the dotnet/coreclr#10772?