memory leak in ASP.NET Core API RRS feed

  • Question

  • User227221424 posted


    I've created a small ASP.NET Core Web API project in VS 2017 from the template. Every time a request is made it appears to consume memory, even the following code does this.  This happens when running the service from VS 2017 and on a docker container both under Windows 10 and Linux. The GC does not appear to intercede at any point either even after 1000s of calls.

    public class ValuesController : ControllerBase
    // GET api/values
    public ActionResult<IEnumerable<string>> Get()
    return new string[] { "value1", "value2" };

    Friday, June 21, 2019 2:33 PM

All replies

  • User753101303 posted


    AFAIK it's not necessarily a problem. A GC paper updated for ASP.NET Core could help but the main criteria should be the remaining memory rather than just an allocation count (using 1000 time 10 bytes or one time 1 Gb is far from being the same thing). Try AddMemoryPressure (or really consume much more memory) and then you should start to see GC being triggered.

    In short don't expect to see a GC if you still have plenty of available memory.

    Friday, June 21, 2019 2:48 PM
  • User227221424 posted


    There was not however plenty of memory. In production the service co-exists with other services on the same boxes with around 16 GB memory, and these services handle millions of requests per day. The services just stopped working, and required a restart using Rancher. My additional issue with this is that even when the method code does literally nothing ( return null), it consumes memory without releasing it.  This does not seem to happen in .NET 4.X.

    Friday, June 21, 2019 4:20 PM
  • User753101303 posted


    Ah try maybe https://blog.markvincze.com/troubleshooting-high-memory-usage-with-asp-net-core-on-kubernetes/ (in short it takes also into account the number of CPUs).

    Unfortunately testing the GC behavior is a bit hard as the goal is precisely to minimize all memory operations (and even avoiding reclaiming memory from the OS too often). It seems ASP.NET Core 3 will expose some more info through the GC class.

    Friday, June 21, 2019 4:40 PM
  • User-474980206 posted
    The issue is that core 2.2 does not support the docker memory constraints, so it thinks there is lots of memory. As stated core 3.0 has support for docker memory constraints.
    Friday, June 21, 2019 4:59 PM
  • User753101303 posted

    If confirmed maybe https://docs.microsoft.com/en-us/dotnet/api/system.gc.addmemorypressure?view=netframework-4.8 could help for now ???

    Friday, June 21, 2019 5:13 PM
  • User-474980206 posted

    you might follow this tread about docker memory constraints:


    the current hack for 2.2 is to put the GC in workstation mode to make the GC more aggressive. in your .proj file


    again, this is fixed in 3.0 (RC scheduled for July)

    Friday, June 21, 2019 6:13 PM
  • User227221424 posted

    OK Thanks Bruce, 

    I'll try that and leave it open too for another day in case there someone knows of another solution that does not require 3.0.

    Monday, June 24, 2019 9:50 AM