High Native Memory Usage Observed on Azure Linux 3.0 with .NET 8.0
Description
We have an application running in AKS with .NET 8 and our pods in the service with requests of 3GB and limit of 4GB are getting bloated with memory and reaching near peak limit causing pods crashing and restarting. This caused Out of Memory exceptions as well because pods were not getting initialized due to lack of enough memory
We analysed the dotnet dump, dotnet-counter and observed that there were 60+ Malloc arena created in the memory with ~64MB each. we observed the dotnet-counter that there were no signs of managed memory heap issue.
We tried with setting the MALLOC_ARENA_MAX = 2 in lower environment and observed lesser memory consumption with MALLOC_ARENA count reduced to 4. This has shown some improvement, but we want to be sure that is the right approach for production and what are the trade offs of setting this environment variable.
Few questions we have
- What could be the root cause of this high native memory footprint as this is impacting our production
- what could be the possible solutions to fix this without moving back to the older mariner2.0 docker image
- Is this approach of setting the MALLOC_ARENA_MAX env variable recommended and what is the ideal value that we should be using in the production environments
Reproduction Steps
- letting the service running for a couple of hours and we see a steadily growing memory footprint indicating increase in native memory while the heap size stays normal
Expected behavior
The total memory is expected to go up and down, but it should not increase steadily over time.
Actual behavior
Total memory constantly increasing, while .NET managed heap stays small.
Regression?
Suspected regression coinciding with base image/runtime changes and the major changes in the newer image azurelinux compared to the aspnet:8.0-cbl-mariner2.0
Known Workarounds
On 64‑bit Linux, glibc malloc uses multiple arenas to reduce lock contention. The default maximum arena count scales with CPUs. In containers, if the process can see many cores, glibc may create tens of arenas.
dotnet-dump and dotnet-counters show 60+ glibc malloc arenas, each roughly ~64 MB, driving substantial native footprint. Managed memory counters show no managed heap pressure—the issue appears native, not GC‑related. As an experiment, setting MALLOC_ARENA_MAX=2 reduced the number of arenas to ~4 and lowered overall memory consumption, improving stability.
Configuration
- Environment: AKS; Kubernetes pods with 3 GB request / 4 GB limit, Node SKU Standard_D4S_v3
- Container base image: sdk:8.0-azurelinux3.0
- letting the service running for a couple of hours and we see a steadily growing memory footprint indicating increase in native memory while the heap size stays normal.
Other information
I'm Microsoft FTE, Can you please help in expedite this investigation. Please let us know if you need any other information in triaging this
Tagging subscribers to this area: @dotnet/gc See info in area-owners.md if you want to be subscribed.
I'm a bot. Here are possible related and/or duplicate issues (I may be wrong):
- https://github.com/dotnet/runtime/issues/95922
- https://github.com/dotnet/runtime/issues/98524
You may be seeing malloc fragmentation due to native allocations in OpenSSL (https://github.com/dotnet/runtime/issues/92490#issuecomment-2457540297). @rzikm Do we have any additional insights about this?
What could be the root cause of this high native memory footprint as this is impacting our production
Azure Linux 3.0 has significantly different OpenSSL implementation.
we see a steadily growing memory footprint
If you see steadily growing memory footprint that never stabilizes, you may be seeing a memory leak. Setting MALLOC_ARENA_MAX won't really fix it.
The one big trigger we usually see for this is CRL checking with large CRLs, do you have CertificateRevocationChecking turned on in your application? If the memory usage goes down after turning it off then you are hitting https://github.com/dotnet/runtime/issues/108557
As jkotas said, if the memory never stabilizes then you may be seeing a leak, in which case tools like heaptrack may be used to track down the source of native leaks.
[setting MALLOC_ARENA_MAX] has shown some improvement, but we want to be sure that is the right approach for production and what are the trade offs of setting this environment variable.
MALLOC_ARENA_MAX allows you to make a trade-off between number of allocator arenas (= potentially more memory being used) and lock contention when allocating/freeing memory from multiple threads. in multithreaded environment, malloc needs to take a lock when accessing critical structures in the arena so with fewer arenas you may get some lock contention.
In .NET you may see significant churn around TLS (allocated in OpenSSL internals), since due to .NET async programming model, memory allocated by one thread may end up being freed on a different thread, possibly increasing a chance of lock contention on the affected arenas
Best way to measure is performing a stress test on the system to see if you can measure any degradation.
@rajatg64 - same issue reported here https://github.com/dotnet/runtime/issues/121827 Are we reporting the same issue?
@jkotas - is there any way we can pinpoint the issue to the OpenSSL library? Any tools you would recommend? Core dumps do not show anything significant. heaptrack was mentioned too; I was able to build the tool, as it cannot be installed with the tdnf package manager on azurelinux, but I have not yet been able to run it.
I would start with monitoring the high-level counters over time (see https://github.com/dotnet/runtime/issues/101552#issuecomment-2095925438) to confirm whether you are dealing with heap fragmentation or a memory leak.
heaptrack is the tool that can help you to track down a memory leak. It won't help you to figure out the fragmentation problem.
@jkotas - thanks for getting back to me. I am indeed capturing unmanaged memory counters as suggested in the post you linked, see more details here: https://github.com/dotnet/runtime/issues/121827 We are seeing values like these below after a few hours - the total space in the arena is large, but most of it seems to be free
Non-mmapped space allocated (bytes): 1,376,210,944 (arena)
Number of free chunks: 12,383 (ordblks)
Number of free fastbin blocks: 24,266 (smblks)
Number of mmapped regions: 16 (hblks)
Space allocated in mmapped regions (bytes): 1,785,856 (hblkhd)
usmblks: 0
Space in freed fastbin blocks (bytes): 1,082,064 (fsmblks)
Total allocated space (bytes): 38,468,416 (uordblks)
Total free space (bytes): 1,337,742,528 (fordblks)
Top-most, releasable space (bytes): 113,584 (keepcost)
Closing as duplicate of https://github.com/dotnet/runtime/issues/121827