LMSouq
server-admin Open

Dotnet Core Docker Container Leaks RAM on Linux and causes OOM

PE
Peter Mills
1 month ago
3 views
Problem Description
I am running Dotnet Core 2.2 in a Linux container in Docker. I've tried many different configuration/environment options - but I keep coming back to the same problem of running out of memory ('docker events' reports an OOM). In production I'm hosting on Ubuntu. For Development, I'm using a Linux container (MobyLinux) on Docker in Windows. I've gone back to running the Web API template project, rather than my actual app. I am literally returning a string and doing nothing else. If I call it about 1,000 times from curl, the container will die. The garbage collector does not appear to be working at all. Tried setting the following environment variables in the docker-compose: DOTNET_RUNNING_IN_CONTAINER=true DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=true ASPNETCORE_preventHostingStartup=true Also tried the following in the docker-compose: mem_reservation: 128m mem_limit: 256m memswap_limit: 256m (these only make it die faster) Tried setting the following to true or false, no difference: ServerGarbageCollection I have tried instead running as a Windows container, this doesn't OOM - but it does not seem to respect the memory limits either. I have already ruled out use of HttpClient and EF Core - as I'm not even using them in my example. I have read a bit about listening on port 443 as a problem - as I can leave the container running idle all day long, if I check at the end of the day - it's used up some more memory (not a massive amount, but it grows). Example of what's in my API: // GET api/values/5 [HttpGet("{id}")] public ActionResult<string> Get(int id) { return "You said: " + id; } Calling with Curl example: curl -X GET "https://localhost:44329/api/values/7" -H "accept: text/plain" --insecure (repeated 1,000 or so times) Expected: RAM usage to remain low for a very primitive request Actual: RAM usage continues to grow until failure Full Dockerfile: FROM microsoft/dotnet:2.2-aspnetcore-runtime AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM microsoft/dotnet:2.2-sdk AS build WORKDIR /src COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"] RUN dotnet restore "WebApplication1/WebApplication1.csproj" COPY . . WORKDIR "/src/WebApplication1" RUN dotnet build "WebApplication1.csproj" -c Release -o /app FROM build AS publish RUN dotnet publish "WebApplication1.csproj" -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "WebApplication1.dll"] docker-compose.yml version: '2.3' services: webapplication1: image: ${DOCKER_REGISTRY-}webapplication1 mem_r...

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base