aspnetcore: Memory grows with each request, PinnedBlockMemoryPool never release memory
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Here is code sample:
[Route("api/[controller]")]
[ApiController]
public class TestsController : ControllerBase
{
static readonly byte[] Data = new byte[1_000_000_000]; //1gb
[HttpGet(nameof(Download))]
public ActionResult<byte[]> Download()
{
GC.Collect();
var memory = System.Diagnostics.Process.GetCurrentProcess().PrivateMemorySize64;
Console.WriteLine($"{memory / 1_000_000} MB");
return Data;
}
}
I am calling this method via curl as following 10 times:
curl --insecure https://localhost:5000/api/Tests/Download > NUL
And here is output of the sample:
67 MB
5216 MB
6626 MB
8014 MB
9400 MB
9464 MB
12195 MB
13592 MB
14975 MB
16356 MB
So memory grows over and over, despite the fact that Data is static variable and GC.Collect() called each time. Please someone explain what is going on? And how to reduce memory consumption?
Expected Behavior
Much less memory consumption
Steps To Reproduce
No response
Exceptions (if any)
No response
.NET Version
8.0.204
Anything else?
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 6
- Comments: 15 (6 by maintainers)
I’ve made code sample to demonstrate the problem and way to reduce memory consumption: https://github.com/depler/DotnetMemoryTest
It uses Harmony (https://github.com/pardeike/Harmony) to override original logic of
PinnedBlockMemoryPool. See methodMemoryPatch.Install();Test steps are following:
Tracking memorytext in consoledownload_loop.batfile for about 1 minute, then kill itMemory consumption without patch:
Memory consumption with patch:
So this problem is definitely related with incorrect implementation of
PinnedBlockMemoryPool. It is not memory leak by nature, but it leads to accumulation huge amount of almost-not-used byte arrays forever, while process alive. At the very bad scenario - it will lead toOutOfMemoryExceptionduring heavy load.Can you still repro with this change?
ProcessimplementsIDisposableand you aren’t disposing of it so there’s a possibility you’re just observing native resources associated with that process handle leaking.So, memory is growing up dramatically because of this: https://github.com/dotnet/aspnetcore/blob/8486d31e24f30e3fa1809a95699a0adc16f448d7/src/Shared/Buffers.MemoryPool/PinnedBlockMemoryPool.cs#L35
And this: https://github.com/dotnet/aspnetcore/blob/v8.0.4/src/Shared/Buffers.MemoryPool/MemoryPoolBlock.cs
You see? Starting from some response length (megabytes and more) Kestrel uses memory pool to store this response. The problem is that PinnedBlockMemoryPool never release memory - it can only grows up. As far as I know there is no way to disable this behavior, and Microsoft has been promising for 3 years to fix this 🙁
We’re looking into addressing this in .NET 10, dont’ expect magic though, the examples in this thread will still perform poorly. The solution is often to stream data and avoiding large, reallocated buffers especially on a per request basis.
I’ve running it in docker on
mcr.microsoft.com/dotnet/aspnet:8.0. Looks like ok, I can’t reproduce it.Code:
So, I think:
ConcurrencyLimiterin your application’s configuration to make this rule clear.