You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an ASP.NET app running on Docker (image: microsoft/dotnet:1.1.1-sdk). When we start the application it allocates ~11G of virtual memory. I have run the application directly on the host, i.e. without Docker, both on Windows and Ubuntu. I have noticed that in Windows is consumes 30MB of Memory (private working set) and about the same for Memory (shared working set), but on Ubuntu it still consumes 11G.
So, I have the following questions:
What's the cause for this?
Do we need to do something to avoid this?
Here are some data from running ps aux from Ubuntu.
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 8832 0.4 1.3 11896404 66396 ? SLl 11:41 0:01 dotnet ...
@ipinak Please note that this is just a reservation of virtual address space, not a physical memory. Since each process (on x64) has 128TB of virtual address space for its private use, this should not be a problem. This virtual memory is mostly an address space reserved for GC heaps and executable code.
Also note that on Windows "working set" means something like the amount of committed memory that the OS thinks the application needs. It's not all the memory that the application has committed and it's certainly not reserved virtual address space which is very large on Windows too. For example, I have a running .NET application where VMMap shows 17GB of reserved virtual address space, 400MB of commited memory and a 185MB working set.
Thank you both @janvorli and @mikedn for the explanation. Is there any way to reduce that? Since I'm running on Docker, I'm thinking of using cgroups, do you have any opinion on that?
@janvorli The real problem related to huge vm usage is that the coredumps created from dotnet processes are too huge too. Real example: I'm running console app which uses less than 100MB of memory, but virtual memory usage is always near 10GB. When I create coredump of this app under Linux, the size of this coredump is 10GB too. When I create process dump of the same app under Windows, the size of this dump is near 100MB.
Ok, that's the default. Hmm, I wonder if we can do anything with the size of the core dump so that it doesn't include pages that were just reserved (allocated by mmap with PROT_NONE option). I can see that madvise can be used with MADV_DONTDUMP to prevent a range of memory from being included in the core dump, so maybe it could be used in some way.
To test your changes, the simplest way that I often use is to publish your testing application for linux-x64 rid (dotnet publish -r linux-x64). After the publishing, it shows you the path to the publish folder that contains all the runtime files. Then I get the commit number from which the libcoreclr.so in that folder was built (strings libcoreclr.so | grep "@(#)"). Then I check out the coreclr repo at that commit, make my changes there, build it (just run the build.sh script in the root, that will get you the debug build with symbols and no optimizations so that you can easily debug your change). After the build completes, it will tell you where it put the resulting binaries (bin/Product/Linux.x64.Debug). Just copy all the files from there into the publish folder, overwriting the existing ones. Then you can debug and test your changes.
I've made the changes you described to test if core dump will be smaller, but size of core dump is the same :(
I'm not sure if I understand you correctly. This is what I did:
diff --git a/src/pal/src/map/virtual.cpp b/src/pal/src/map/virtual.cpp
index a5610ef..55c210d 100644
--- a/src/pal/src/map/virtual.cpp+++ b/src/pal/src/map/virtual.cpp@@ -1016,6 +1016,20 @@ static LPVOID ReserveVirtualMemory(
return nullptr;
}
+ // Do not include reserved virtual memory in core dumps+ // TODO: only on linux, because MADV_DONTDUMP is Linux-specific+ int madviseResult = madvise(pRetVal,+ MemSize,+ MADV_DONTDUMP);+ if (madviseResult != 0)+ {+ //TODO: should we fail and exit here?+ ERROR("madvise failed!\n");+ pthrCurrent->SetLastError(ERROR_INVALID_ADDRESS);+ munmap(pRetVal, MemSize);+ return nullptr;+ }+
#if MMAP_ANON_IGNORES_PROTECTION
if (mprotect(pRetVal, MemSize, PROT_NONE) != 0)
{
@@ -1169,6 +1183,15 @@ VIRTUALCommitMemory(
goto error;
}
+ // Include committed memory in core dump+ // TODO: Linux-specific?+ if (madvise((void *) StartBoundary, MemSize, MADV_DODUMP) != 0)+ {+ //TODO: is it safe to continue here?+ ERROR("madvise() failed! Error(%d)=%s\n", errno, strerror(errno));+ goto error;+ }+
VIRTUALSetAllocState(MEM_COMMIT, runStart, runLength, pInformation);
if (nProtect == (PROT_WRITE | PROT_READ))
@@ -1706,6 +1729,17 @@ VirtualProtect(
{
*lpflOldProtect = PAGE_EXECUTE_READWRITE;
}
++ //TODO: Linux-specific?+ //TODO: Is protection check correct?+ int advise = flNewProtect == PAGE_NOACCESS ? MADV_DONTDUMP : MADV_DODUMP;+ if (madvise((LPVOID)StartBoundary, MemSize, advise) != 0)+ {+ //TODO: Is it safe to continue here?+ SetLastError( ERROR_INVALID_ADDRESS );+ goto ExitVirtualProtect;+ }+
bRetVal = TRUE;
}
else
You've made the changes I would expect. So maybe the memory is committed but not mapped to physical memory yet.
Is it possible to share your testing app so that I can debug it locally?
VM utilization in that app is not that much as in original app, but problem is reproduced:
mem usage: ~150MB
vm usage: ~3GB
createdump -u produces ~3GB core dump
createdump (minidump with heap) produces ~200MB
This issue has re-appeared for me. Attempting to use dotnet-trace to see if my code is causing this, but I'm still not following why this is happening where virtual memory allocation is requested to go above what a 32-bit system can provide.
Can you get this to reproduce reliably? You may want to set a breakpoint on GCToOSInterface::VirtualReserve to see why the GC is reserving memory.
I can reproduce this easily with my setup. I'm working around this by setting DOTNET_DefaultStackSize=500000. this keeps VM down under 3GB for me.
How do I set a break point there?
Are you using aot at all? If not, the AOT issue is irrelevant.
If you can use your own build to ensure the optimization are turned off to make debugging easier, otherwise functions could be inlined and you might lose it. The standalone GC feature allow you to replace only the GC so you can keep the rest untouched.
Here are some instruction on how to build the CoreCLR.
Here are some instructions on how to debug the runtime.
I think I'm about to enter a rabbit hold. The AOT issue indicates it also resolved issues with non-AOT code, but I linked it as they indicate using GDB as well for the said function. I will see if I can build dotnet with our internal SDK for the target platform.
@jbatt33 if setting the stack size helps you, then maybe your app is using too many threads. GC cannot take the threads stack size into account when creating the heap, since it doesn't know how many of them your app will be using. By default, it takes the physical memory limit and starting from dotnet 9 even the virtual memory limit as input to its decision on how much virtual address space to reserve for the GC heap (unless you specify the limits yourself)
@janvorli that's understandable. Checking the number of tasks under the process (including those of .NET) it is showing 531 tasks. My concern was that the default values when running the same application under mono 6.12 did not show this much usage, but that is a totally different runtime.
With that all said, it does seem weird that setting a valid of 5MB allows the program to work as expected when I though the default was a lower value. Based on my silly calculations, it looks like the thread stack size is defaulting to 8MB, which would put it over the limit allowed by arm32. Getting an OOM exception is to be expected.
I was actually wrong when I've said that the default stack size is 1.5MB. On Linux, it is actually 8MB (and it reflects what the ulimit -s reports / sets)
Except for MUSL based distros where the secondary thread stack size is really 1.5MB, we override the tiny default size set by MUSL to what Windows has set as a default.
There are cases where this matters. In particular, if you are running on Linux and have overcommit disabled then the committed VM contributes to the VM limit for the process. The PROT_NONE reservation by the GC is not considered committed and therefore doesn't contribute, but stack memory is mapped PROT_READ, which counts.
We'll likely need to add some configuration switch to enable apps which run without overcommit to limit the amount of thread VM requested by the runtime.
There is large allocation and 300GB of virtual memory being reported per process. Yeah it doesn't seem to cause any negative things for the process except maybe keeping track of that much space, but it's messing with monitoring. Virtual memory is a great metric for average memory usage over time. With mono this would grow proportional to the real memory usage, but remain reserved if the actual usage went down again. Meaning over time you would see if the memory usage spiked and the relation between virt mem and actual memory also gives indication of how heavy certain processes actually. More virt mem allocation usually means you have heave routines that reserve a lot of memory just in case, whether they actually consume it or not.
Frankly the monitoring part is the most annoying, cause an application that reports 300GB virt memory looks like it just exploded or GC has failed so it'll usually spit out a warning. collectd graphs made useless too as they stack the memory graphs so actual memory line is practically invisible now. Sure, personal problem and I'm fully happy to give processes a couple GB reserved, ten times their actual usage, but 300GB per... that's a big excessive when the real memory is 200MB.
Reacted by seriouz, kpietraszko, Tom Fifield and Gavin Lambert
Activity
janvorli commentedon Mar 28, 2017
@ipinak Please note that this is just a reservation of virtual address space, not a physical memory. Since each process (on x64) has 128TB of virtual address space for its private use, this should not be a problem. This virtual memory is mostly an address space reserved for GC heaps and executable code.
mikedn commentedon Mar 28, 2017
Also note that on Windows "working set" means something like the amount of committed memory that the OS thinks the application needs. It's not all the memory that the application has committed and it's certainly not reserved virtual address space which is very large on Windows too. For example, I have a running .NET application where VMMap shows 17GB of reserved virtual address space, 400MB of commited memory and a 185MB working set.
ipinak commentedon Mar 28, 2017
Thank you both @janvorli and @mikedn for the explanation. Is there any way to reduce that? Since I'm running on Docker, I'm thinking of using cgroups, do you have any opinion on that?
janvorli commentedon Mar 28, 2017
@ipinak why would you want to reduce that? Are you hitting some problem related to that?
ipinak commentedon Mar 29, 2017
I haven't faced any issue, it's for provisioning planning reasons.
6opuc commentedon Mar 14, 2019
@janvorli The real problem related to huge vm usage is that the coredumps created from dotnet processes are too huge too. Real example: I'm running console app which uses less than 100MB of memory, but virtual memory usage is always near 10GB. When I create coredump of this app under Linux, the size of this coredump is 10GB too. When I create process dump of the same app under Windows, the size of this dump is near 100MB.
janvorli commentedon Mar 14, 2019
I wonder what is your setting in
/proc/self/coredump_filter.6opuc commentedon Mar 14, 2019
cat /proc/self/coredump_filter
00000033
janvorli commentedon Mar 14, 2019
Ok, that's the default. Hmm, I wonder if we can do anything with the size of the core dump so that it doesn't include pages that were just reserved (allocated by mmap with PROT_NONE option). I can see that madvise can be used with MADV_DONTDUMP to prevent a range of memory from being included in the core dump, so maybe it could be used in some way.
6opuc commentedon Mar 14, 2019
How can I help to implement/test this modification? Maybe some directions in source code so I can test it on my side?
janvorli commentedon Mar 14, 2019
@6opuc that would be awesome! I think that we could add call to madvise MADV_DONTDUMP in this function for the range we've reserved:
https://github.com/dotnet/coreclr/blob/6958ede8e835048b9d1ee9843d7587cacf527101/src/pal/src/map/virtual.cpp#L958
Then add call to madvise with MADV_DODUMP for the commited range in
https://github.com/dotnet/coreclr/blob/6958ede8e835048b9d1ee9843d7587cacf527101/src/pal/src/map/virtual.cpp#L1041
And it seems we would need to add madvise with MADV_DONTDUMP / MADV_DODUMP (based on the protection) in
https://github.com/dotnet/coreclr/blob/6958ede8e835048b9d1ee9843d7587cacf527101/src/pal/src/map/virtual.cpp#L1621
I think that should be sufficient.
To test your changes, the simplest way that I often use is to publish your testing application for
linux-x64rid (dotnet publish -r linux-x64). After the publishing, it shows you the path to the publish folder that contains all the runtime files. Then I get the commit number from which the libcoreclr.so in that folder was built (strings libcoreclr.so | grep "@(#)"). Then I check out the coreclr repo at that commit, make my changes there, build it (just run the build.sh script in the root, that will get you the debug build with symbols and no optimizations so that you can easily debug your change). After the build completes, it will tell you where it put the resulting binaries (bin/Product/Linux.x64.Debug). Just copy all the files from there into the publish folder, overwriting the existing ones. Then you can debug and test your changes.6opuc commentedon Mar 14, 2019
Thanks a lot! I'll try that today and let you know about the results
6opuc commentedon Mar 15, 2019
I've made the changes you described to test if core dump will be smaller, but size of core dump is the same :(
I'm not sure if I understand you correctly. This is what I did:
janvorli commentedon Mar 15, 2019
You've made the changes I would expect. So maybe the memory is committed but not mapped to physical memory yet.
Is it possible to share your testing app so that I can debug it locally?
6opuc commentedon Mar 15, 2019
I've created simple app for this test:
https://github.com/6opuc/MemoryTest/blob/master/MemoryTest/Program.cs
VM utilization in that app is not that much as in original app, but problem is reproduced:
mem usage: ~150MB
vm usage: ~3GB
createdump -u produces ~3GB core dump
createdump (minidump with heap) produces ~200MB
72 remaining items
cshung commentedon Mar 19, 2024
Can you get this to reproduce reliably? You may want to set a breakpoint on
GCToOSInterface::VirtualReserveto see why the GC is reserving memory.jbatt33 commentedon Mar 19, 2024
I can reproduce this easily with my setup. I'm working around this by setting DOTNET_DefaultStackSize=500000. this keeps VM down under 3GB for me.
How do I set a break point there?
jbatt33 commentedon Mar 19, 2024
I see the following issue also discusses a breakpoint there, but I'm using the release build of dotnet, does it have GDB symbols?
#95257
cshung commentedon Mar 19, 2024
Are you using aot at all? If not, the AOT issue is irrelevant.
If you can use your own build to ensure the optimization are turned off to make debugging easier, otherwise functions could be inlined and you might lose it. The standalone GC feature allow you to replace only the GC so you can keep the rest untouched.
Here are some instruction on how to build the CoreCLR.
Here are some instructions on how to debug the runtime.
jbatt33 commentedon Mar 19, 2024
I think I'm about to enter a rabbit hold. The AOT issue indicates it also resolved issues with non-AOT code, but I linked it as they indicate using GDB as well for the said function. I will see if I can build dotnet with our internal SDK for the target platform.
janvorli commentedon Mar 19, 2024
@jbatt33 if setting the stack size helps you, then maybe your app is using too many threads. GC cannot take the threads stack size into account when creating the heap, since it doesn't know how many of them your app will be using. By default, it takes the physical memory limit and starting from dotnet 9 even the virtual memory limit as input to its decision on how much virtual address space to reserve for the GC heap (unless you specify the limits yourself)
jbatt33 commentedon Mar 20, 2024
@janvorli that's understandable. Checking the number of tasks under the process (including those of .NET) it is showing 531 tasks. My concern was that the default values when running the same application under mono 6.12 did not show this much usage, but that is a totally different runtime.
With that all said, it does seem weird that setting a valid of 5MB allows the program to work as expected when I though the default was a lower value. Based on my silly calculations, it looks like the thread stack size is defaulting to 8MB, which would put it over the limit allowed by arm32. Getting an OOM exception is to be expected.
janvorli commentedon Mar 20, 2024
I was actually wrong when I've said that the default stack size is 1.5MB. On Linux, it is actually 8MB (and it reflects what the ulimit -s reports / sets)
janvorli commentedon Mar 20, 2024
Except for MUSL based distros where the secondary thread stack size is really 1.5MB, we override the tiny default size set by MUSL to what Windows has set as a default.
agocke commentedon Aug 21, 2024
Hit this in an unrelated situation the other day.
There are cases where this matters. In particular, if you are running on Linux and have overcommit disabled then the committed VM contributes to the VM limit for the process. The PROT_NONE reservation by the GC is not considered committed and therefore doesn't contribute, but stack memory is mapped PROT_READ, which counts.
We'll likely need to add some configuration switch to enable apps which run without overcommit to limit the amount of thread VM requested by the runtime.
Tampa commentedon Nov 3, 2024
There is large allocation and 300GB of virtual memory being reported per process. Yeah it doesn't seem to cause any negative things for the process except maybe keeping track of that much space, but it's messing with monitoring. Virtual memory is a great metric for average memory usage over time. With mono this would grow proportional to the real memory usage, but remain reserved if the actual usage went down again. Meaning over time you would see if the memory usage spiked and the relation between virt mem and actual memory also gives indication of how heavy certain processes actually. More virt mem allocation usually means you have heave routines that reserve a lot of memory just in case, whether they actually consume it or not.
Frankly the monitoring part is the most annoying, cause an application that reports 300GB virt memory looks like it just exploded or GC has failed so it'll usually spit out a warning. collectd graphs made useless too as they stack the memory graphs so actual memory line is practically invisible now. Sure, personal problem and I'm fully happy to give processes a couple GB reserved, ten times their actual usage, but 300GB per... that's a big excessive when the real memory is 200MB.
dotnet-policy-service commentedon Apr 9, 2026
Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.