Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
We have identified an issue with Asp.Net core API controllers, specifically related to memory allocation which does not get cleared.
Our original use case was an API controller that reads a JSON file and returns data from it. We noted that memory use was growing every time we called the API controller.
We have generated a simplified example to reproduce this issue. Here, we use the following method to generate a 50 million character string and puts it into an object that is not used at all.
[HttpGet("[action]")]
public IActionResult Test1(int testNumber)
{
var result = new string((char)(new Random().NextInt64(1, 65535)), 50_000_000);
var mem = Process.GetCurrentProcess().PagedMemorySize64;
return Ok($"TEST #{testNumber} ; Mem: {mem:N0}");
}
However, the memory still seems to be allocated and retained for this object for a very long duration (GC is run but the memory doesn't seem to be released). Interestingly, at some point, this hits a peak memory allocation, which differs based on the size of the string created.
The problem is also true when deserializing JSON files (which is what we started with) and returning a subset of information from the result object. In that case, the memory allocation for the complete object doesn't seem to go away, causing a server to get overloaded.
In .Net 7, memory is never released, it seems. In .Net 6, some of the memory is released, some of the time, but we still don't understand why it's even stored in memory at all for any duration, since the object is not used for any purpose, and once the API call has completed, should be released.
You can test this issue with the attached solution. Once the project is run, monitor your Visual Studio's Diagnostic Tools window (Process Memory). The API calls also return some information about memory, but the graph represents the issue better IMHO.
Expected Behavior
Objects are released from memory after method completes execution and the API response is returned
Steps To Reproduce
Run the TestWebAppCore project. Click on the links. Watch the memory not get released, but hit a top limit at some point per test.
Exceptions (if any)
No response
.NET Version
.Net 7.0.100 and .Net 6.0.400