runtime icon indicating copy to clipboard operation
runtime copied to clipboard

[ Memory consumption ] Use flat layout for IL-only assemblies

Open ruben-ayrapetyan opened this issue 9 years ago • 21 comments

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

ruben-ayrapetyan avatar Apr 04 '17 13:04 ruben-ayrapetyan

We plan to open pull request for this change in two days.

ruben-ayrapetyan avatar Apr 04 '17 15:04 ruben-ayrapetyan

Nice - thank you for identifying this issue!

jkotas avatar Apr 05 '17 07:04 jkotas

@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?

ruben-ayrapetyan avatar Apr 07 '17 11:04 ruben-ayrapetyan

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?

ruben-ayrapetyan avatar Apr 07 '17 14:04 ruben-ayrapetyan

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?

jkotas avatar Apr 07 '17 14:04 jkotas

@valenis, could you, please, suggest a good place to add a test for detecting regressions related to size of working set?

ruben-ayrapetyan avatar Apr 10 '17 11:04 ruben-ayrapetyan

CC @mikem8361 who is working to validate the changes against Debugger/Profiler tests. We cannot merge this until that is complete.

CC @lt72 @janvorli

gkhanna79 avatar Apr 27 '17 19:04 gkhanna79

@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 avatar Apr 27 '17 20:04 lt72

@lt72 @mikem8361 @ruben-ayrapetyan Few things about this:

  1. We need to get status of debugger signoff on this change. @lt72 @mikem8361 Where are we with it?
  2. 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).

gkhanna79 avatar May 05 '17 18:05 gkhanna79

I finally got our debuggertests repo working on .NETCore 2.0 and now trying to verify ruben’s changes.

mikem8361 avatar May 05 '17 18:05 mikem8361

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.

ruben-ayrapetyan avatar May 05 '17 19:05 ruben-ayrapetyan

Thanks for the confirmation @ruben-ayrapetyan - I have moved this issue to 2.1.

gkhanna79 avatar May 05 '17 19:05 gkhanna79

@gkhanna79, Thank you.

ruben-ayrapetyan avatar May 05 '17 19:05 ruben-ayrapetyan

This has been verified against the debugger tests and the tests succeed.

mikem8361 avatar May 06 '17 06:05 mikem8361

This has been verified against the debugger tests and the tests succeed.

@mikem8361, thank you very much for verification.

ruben-ayrapetyan avatar May 06 '17 13:05 ruben-ayrapetyan

@mikem8361 Just making sure ... have you run the debugger tests on Linux? This is Linux specific change.

jkotas avatar May 06 '17 13:05 jkotas

Yes.

mikem8361 avatar May 06 '17 16:05 mikem8361

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?

ruben-ayrapetyan avatar May 23 '17 12:05 ruben-ayrapetyan

could you, please, check this, if master is now targetting the 2.1 release?

Yes, master is targeting the 2.1 release now.

jkotas avatar May 23 '17 12:05 jkotas

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?

ruben-ayrapetyan avatar May 23 '17 12:05 ruben-ayrapetyan