Back to results for “Memory Management”
.NET › Docker & Containerization

How do you configure a .NET container to respect memory and CPU limits set by the container orchestrator (like Kubernetes resource limits)?

Intermediate

Answer

Modern .NET automatically detects container memory and CPU limits (via cgroup information on Linux) and configures the garbage collector and thread pool accordingly by default -- the GC scales its heap size targets based on the container's memory limit rather than the host machine's total memory, and the thread pool sizes itself based on the container's CPU limit (not the host's core count), preventing the classic problem of a containerized app assuming it has access to the entire host's resources.
# Kubernetes resource limits automatically respected by the .NET runtime:
resources:
  limits:
    memory: "512Mi"
    cpu: "1"
# .NET's GC targets a heap size appropriate for 512Mi, not the node's full memory
Real-world example Before this automatic detection was reliable, a team had to manually set DOTNET_GCHeapHardLimit to prevent their containerized app's GC from assuming it had access to the entire host's 64GB of RAM, causing OOM-killed pods under memory pressure.

Common follow-ups: How do you override the automatic detection if it doesn't behave as expected?;What environment variables control GC heap limits explicitly?