question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Asp.Net Core API controller memory leak

See original GitHub issue

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

TestSolution.zip

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

Anything else?

MemoryLeak

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:25 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
davidfowlcommented, Nov 16, 2022

Show me the code snippet you use today and I’ll rewrite it for you.

4reactions
davidfowlcommented, Nov 15, 2022

By using streams. When you use the result types with objects, it doesn’t first buffer 50Mb into memory because they would be horrible for performance. Instead, it writes your 50Mb in 16K (configurable) chunks asynchronously to the response stream.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Debug a memory leak in .NET Core
A memory leak may happen when your app references objects that it no longer needs to perform the desired task. Referencing said objects...
Read more >
Memory management and patterns in ASP.NET Core
If the Task Manager memory value increases indefinitely and never flattens out, the app has a memory leak. The following sections demonstrate ...
Read more >
Best Practices to Avoid Memory Leaks in ASP.NET
A memory leak happens when an application cannot correctly manage memory allocations and deallocations. Once a memory leak has occurred, the ...
Read more >
c# - ASP.NET Core Web API returning classes resulting in ...
ASP.NET Core Web API returning classes resulting in memory leak? ... Upon request, an ASP.NET Core Web API was created, in such a...
Read more >
Fixing a memory leak in .Net Core - A Method to Madness
The memory leak becomes quite obvious : the memory consumption keeps on growing, until it reaches a threshold matching the server's specs. We ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found