Forum Discussion
SamXion
Nov 12, 2021Copper Contributor
Why is my ASP.Net Web API piling up memory?
Hi everyone,
I have a very simple MVC web api with a single controller that currently does nothing but returning a value. I am publishing it in VS2022 Preview 1 into a single file, so I get an .exe as an output. So far so good. That all seems to work fine.
I have a C++ application that is communicating with that Web API and is starting it in an own process. Basically my test is calling an endpoint of the API every 250ms and what I noticed is that this fills up my memory (slowly but constantly). Starting with 16MB, after an hour the processs has swallowed around 90MB of RAM.
Can anybody please tell me why is that? I am lost.
Here is the complete code of the Web API:
// My Controller
[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
// Calling this endpoint every 250ms from my C++ application via http
[HttpGet("buttonstate")]
public int GetButtonState([FromQuery] string path)
{
return 0;
}
}
// My startup class
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
// My program
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
- If you're calling it ever 250ms, it's likely not quiet enough for the Garbage collector to fire up. And with 90mb, it likely isn't pushing enough memory pressure (that's still tiny) to force the Garbage collector.
- CodenamejackCopper ContributorHave you tried launching it without debugger? Maybe with dotnet run command. Is the behaviour same even then?
- SamXionCopper ContributorThe VS publishing function is always compiling in Release mode so that cannot be it. Also, actually when running the API in debug mode, this memory piling does NOT occur. Oo
- shawnwildermuthCopper ContributorIf you're calling it ever 250ms, it's likely not quiet enough for the Garbage collector to fire up. And with 90mb, it likely isn't pushing enough memory pressure (that's still tiny) to force the Garbage collector.
- SamXionCopper ContributorOk, I have to apologize and row back here. Even though I called GC.Collect(), I just noticed that I was running an older build that did NOT call the garbage collection. Running the latest build does indeed get rid of the memory piling.
So thanks for the help! This resolves my issue. 🙂