dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.13k stars 4.7k forks source link

Memory Consumption Differences in Running .NET 6 WebAPI Linux Docker container (with DOTNET_GCHighMemPercent Configured) With and Without Docker memory Limit #105390

Open jedrp opened 2 months ago

jedrp commented 2 months ago

Description

Hosting Environment: I'm running multiple .NET Docker containers on the same Linux Docker host. My goal is to optimize memory usage over performance.

To achieve the goal mentioned above, I set DOTNET_GCHighMemPercent=0x5 and DOTNET_GCConserveMemory=9, assuming that the garbage collector would run more aggressively when memory usage exceeds 5% of total physical memory. However, I did not observe any difference until I imposed a memory limit on the container (docker run --memory with the value limit being roughly equivalent to the physical memory).

I don't want to set a static memory limit on the container because it needs to process large data sets for background jobs. Instead, when memory usage exceeds DOTNET_GCHighMemPercent, the garbage collector should run more aggressively to reduce memory consumption.

Configuration

.Net 6 OS version: Docker WSL 2 ubuntu Linux (on windows 11) Architecture: x64 Resource: 4 vCPUs and 8GBs Ram assigned Dockerfile build upon

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

Steps to reproduce?

Sniped code that increase memory image

Trigger the endpoints multiple times with the same input using the following commands:

  1. CMD1: _docker run --rm -p 8080:80 -e DOTNET_GCHighMemPercent=0x5 -e DOTNETGCConserveMemory=9 --name mem mem Memory usage image

  2. CMD2: _docker run --rm -p 8080:80 -e DOTNET_GCHighMemPercent=0x5 -e DOTNETGCConserveMemory=9 --memory=6500m --name mem mem Memory usage image

Concerns

Why there is a difference in memory consumption with and without a memory limit on the container when DOTNET_GCHighMemPercent is set?

In additional, Can I check if there's a way to configure the GC to release memory down to 80% of DOTNET_GCHighMemPercent when memory usage exceeds DOTNET_GCHighMemPercent?

dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @dotnet/gc See info in area-owners.md if you want to be subscribed.