.NET › Docker & Containerization
How does .NET's runtime automatically size the ThreadPool and GC based on container CPU limits, and what issue can arise with fractional CPU limits?
AdvancedAnswer
The runtime reads the container's CPU quota (from cgroup cpu.cfs_quota_us/cpu.cfs_period_us on Linux) to determine the effective number of CPUs available, using this to size the ThreadPool's minimum thread count and influence Server GC's heap count -- a known historical issue arose with fractional CPU limits (like 0.5 or 1.5 CPUs, common in Kubernetes resource requests), where the runtime would sometimes round in ways that didn't perfectly match the intended allocation, leading to under- or over-provisioned thread pools; this has been progressively improved across .NET versions.
# Kubernetes deployment with a fractional CPU limit
resources:
limits:
cpu: "1.5" # .NET runtime detects this and sizes ThreadPool/GC accordingly
# Can be explicitly overridden if detection seems off:
# DOTNET_PROCESSOR_COUNT=2Real-world example A team troubleshooting inconsistent performance across pods with a 0.5 CPU limit discovers via dotnet-counters that ThreadPool sizing didn't match expectations, working around it with an explicit DOTNET_PROCESSOR_COUNT override while also upgrading to a newer .NET version with improved fractional CPU handling.