Sizing .NET 10 pods: requests versus limits, QoS classes, how the runtime reads cgroup limits (ProcessorCount, GC heap hard limit at 75%), Server GC and DATAS, CFS CPU throttling and its latency impact, GC heap limit settings, and a measurement-driven sizing method.
Requests are what the scheduler reserves and set the pod's QoS class; limits are enforced by cgroups: exceed the memory limit and the container is OOMKilled, exceed the CPU limit and it is throttled within each 100 ms CFS period.
The .NET runtime reads the cgroup limits: Environment.ProcessorCount follows the CPU limit (rounded up) and the GC's heap hard limit defaults to 75% of the memory limit. With no CPU limit, .NET sees every node core.
I set memory request equal to limit, sized from load tests with headroom for native memory. For CPU I set a realistic request and a generous limit or none, because throttling turns GC and JIT bursts into p99 latency spikes. DOTNET_GCHeapHardLimitPercent is for when native memory needs more room. The trade-off is density versus tail latency.
"First, requests and limits do different jobs. The request is a reservation: the scheduler only places the pod on a node with that much unreserved CPU and memory, and it decides the QoS class. The limit is a ceiling enforced by the kernel's cgroups. Memory is incompressible, so crossing the memory limit gets the container OOMKilled. CPU is compressible, so crossing the CPU limit gets the container throttled: the CFS scheduler gives it a quota per 100 ms period, and once that's spent, every thread in the container waits for the next period.
The .NET runtime is container-aware. It reads cgroup v2
cpu.maxandmemory.max.Environment.ProcessorCountbecomes the CPU limit rounded up, so a 1.5-core limit looks like 2 processors, and that drives Server GC heap count and thread pool sizing. If there's no CPU limit, .NET sees all the node's cores, even with a 250m request, because the runtime doesn't read requests; then I may setDOTNET_PROCESSOR_COUNT. On memory, the GC sets a heap hard limit of 75% of the container limit by default, leaving the rest for native memory: thread stacks, the JIT, SQL client buffers, gRPC and TLS. If an app uses a lot of native memory, I lowerDOTNET_GCHeapHardLimitPercent; I rarely set an absoluteDOTNET_GCHeapHardLimit. ASP.NET Core uses Server GC, and since .NET 9 DATAS is on by default, which adapts the number of heaps to load, so memory per pod is far lower than with classic Server GC.For example, at [company] our [service] had p99 latency spikes of [400 ms] every few seconds with average CPU at only [40%].
container_cpu_cfs_throttled_periods_totalshowed throttling in [30%] of periods: gen0 GCs on 4 Server GC heaps burned the 1-core quota in bursts. We raised the CPU limit to [2], kept the request at [1], and p99 dropped to [90 ms]. The mistake I see is setting tight CPU limits 'to be safe' and then blaming .NET for tail latency."
| Setting | Enforced by | Effect when exceeded |
|---|---|---|
| CPU request | Scheduler + CFS weights | Guaranteed share under contention |
| CPU limit | CFS quota (cpu.max) |
Throttled until next 100 ms period |
| Memory request | Scheduler | Eviction order under node pressure |
| Memory limit | cgroup memory.max |
OOMKilled (exit code 137) |
| QoS class | Rule | Eviction priority |
|---|---|---|
| Guaranteed | requests = limits for CPU and memory | Last |
| Burstable | some requests set | Middle |
| BestEffort | none set | First |
| Runtime behaviour | Source | Override |
|---|---|---|
Environment.ProcessorCount |
CPU limit, rounded up; node cores if no limit | DOTNET_PROCESSOR_COUNT |
| GC heap hard limit | 75% of memory limit (min 20 MB) | DOTNET_GCHeapHardLimitPercent, DOTNET_GCHeapHardLimit (hex) |
| Server GC heap count | ProcessorCount; adapted by DATAS | DOTNET_GCHeapCount, DOTNET_GCDynamicAdaptationMode=0 |
| Thread pool min threads | ProcessorCount | ThreadPool.SetMinThreads, System.Threading.ThreadPool.MinThreads |
After load testing an API at 800 requests/s, peak working set was 310 MB with a 180 MB managed heap. We set memory request and limit to 512 Mi (75% hard limit = 384 MB heap ceiling, plenty for native), CPU request 500m and limit 2. OOMKills went to zero and throttled periods stayed under 1%.
Checks that the candidate understands how Kubernetes resource settings interact with the .NET runtime and can size pods from data.
Green flags
Red flags
CPU limit = 1 core → quota 100 ms per 100 ms period
period 1: |██████████ 4 threads × 25 ms = 100 ms used at t=25ms|░░░░ throttled 75 ms ░░░░|
period 2: |██ normal ██|.............................................|
The quota is per container across all threads. A gen0 GC with 4 Server GC threads, or a burst of JIT at startup, can spend the whole quota in 25 ms of wall time, and then every request thread waits 75 ms. Average CPU looks fine; p99 doesn't. Watch container_cpu_cfs_throttled_periods_total / container_cpu_cfs_periods_total (in Azure Monitor Container Insights or Prometheus).
At start-up the runtime reads cgroup v1 or v2 files (/sys/fs/cgroup/cpu.max, memory.max). CPU: quota / period, rounded up, becomes the processor count (capped at node cores). Memory: the limit becomes TotalAvailableMemoryBytes, and when running in a container with a memory limit, the GC sets GCHeapHardLimit to 75% of it unless configured. When the heap approaches the hard limit, the GC collects more aggressively; if it still can't fit, you get OutOfMemoryException, which is different from an OOMKill by the kernel.
| OutOfMemoryException | OOMKilled | |
|---|---|---|
| Who | .NET GC hit the heap hard limit | Kernel: container total exceeded memory.max |
| Visible as | Exception in logs, maybe a crash | Exit code 137, Reason: OOMKilled, no managed logs |
| Usual cause | Managed leak or limit too small | Native memory growth, or heap limit + native > container limit |
If you see OOMKilled with a small managed heap, look at native memory (the related Phase 1 question covers working set versus GC heap in depth).
Server GC creates one heap and one GC thread per logical processor, which gives throughput but uses more memory. DATAS (Dynamic Adaptation To Application Sizes), on by default for Server GC since .NET 9, starts with one heap and grows the heap count with load, so a quiet pod uses a fraction of the memory. For tiny pods (under ~0.5 core and 256 MB) Workstation concurrent GC can be better: <ServerGarbageCollector>false</ServerGarbageCollector>.
dotnet-counters monitor System.Runtime), CPU and throttling.DOTNET_PROCESSOR_COUNT.limits.cpu: 500m and log Environment.ProcessorCount (expect 1).bombardier or k6 at CPU limits of 1 and 2; compare p99 and throttled periods.DOTNET_GCHeapHardLimitPercent=0x32 (50%, the value is hex) and watch GC.GetGCMemoryInfo().TotalAvailableMemoryBytes.DOTNET_GCDynamicAdaptationMode=0 and compare memory per pod at idle.Resource settings for a .NET 10 API, with the runtime knobs that matter:
containers:
- name: api
image: crordersprod.azurecr.io/orders-api:1.4.2
resources:
requests:
cpu: 500m # what the scheduler reserves; steady-state usage
memory: 512Mi
limits:
cpu: "2" # generous, so GC/JIT bursts aren't throttled
memory: 512Mi # = request: predictable, no eviction surprises
env:
# GC heap ceiling as a percentage of the container limit (hex 0x46 = 70)
# Lower than the 75% default because this app uses a lot of native memory (gRPC, SqlClient)
- name: DOTNET_GCHeapHardLimitPercent
value: "0x46"
A startup log line that makes the effective settings visible in Application Insights:
using System.Runtime;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var gc = GC.GetGCMemoryInfo();
app.Logger.LogInformation(
"Runtime limits: ProcessorCount={Cpu}, GcHeapLimitMB={Heap}, ServerGC={Server}, LatencyMode={Mode}",
Environment.ProcessorCount,
gc.TotalAvailableMemoryBytes / 1024 / 1024,
GCSettings.IsServerGC,
GCSettings.LatencyMode);
app.MapGet("/", () => "ok");
app.Run();
# Is the container being throttled? (cgroup v2, from a debug container sharing the pod)
cat /sys/fs/cgroup/cpu.stat # nr_periods, nr_throttled, throttled_usec
# Live runtime view
dotnet-counters monitor -p 1 System.Runtime --counters dotnet.gc.heap.total_allocated,dotnet.process.memory.working_set
DOTNET_PROCESSOR_COUNT or rely on DATAS and verify memory use.oom_score_adj (set from the QoS class) picks them as OOM victims. Instead: Memory request = limit for production APIs (Guaranteed or near it).DOTNET_GCHeapHardLimitPercent=50. Why it is wrong: GC config env vars are parsed as hex, so 50 means 80%. Instead: 0x32 for 50%, or use runtimeconfig.json (System.GC.HeapHardLimitPercent), which takes decimal.container_cpu_cfs_throttled_periods_total), working set as a percentage of limit, OOMKilled events and restart counts, alongside dotnet-counters / OpenTelemetry runtime metrics (GC heap size, GC pause time, thread pool queue length).CPU limits are a trade-off between noisy-neighbour protection and tail latency, and for latency-sensitive .NET APIs I usually favour latency: set accurate requests (so scheduling and fair share work) and a high or absent CPU limit, with a namespace LimitRange or ResourceQuota as the guardrail. Memory is the opposite: always set the limit, make request equal limit, and treat any OOMKill as a bug to investigate, not something to tune away with more memory.
| Approach | When it fits | Cost | When not to |
|---|---|---|---|
| Request = limit (Guaranteed) | Critical APIs, predictable latency | Lower density | Bursty batch work |
| Request < generous CPU limit | Most .NET APIs | Some overcommit | Strict multi-tenant fairness |
| No CPU limit | Platform allows, latency critical | Needs DOTNET_PROCESSOR_COUNT awareness |
Shared clusters without quotas |
| Workstation GC | Tiny pods, many replicas | Lower throughput | High-throughput APIs |
I measure: throttled-period ratio, OOMKilled count, p99 latency, and requested-versus-used CPU per namespace in Cost Management or Kubecost.
RAM is physical memory. Each process gets a virtual address space that the OS maps onto RAM page by page. The working set is what's resident in RAM right now; private (committed) memory is what the process owns. The GC heap is only one part: native allocations (native libraries, NativeMemory, unmanaged buffers), thread stacks, JIT code, and runtime structures add to it. In Kubernetes, the cgroup limit counts all of it, and going over gets the container OOMKilled (exit code 137), not an OutOfMemoryException. So if the GC heap is small but the pod dies, I compare GC heap size with working set in dotnet-counters. A big gap points to native memory or too many threads; a growing GC heap points to a managed leak I'd chase with dotnet-gcdump. Then I fix the cause and set limits and GC settings deliberately.
The scheduler keeps a queue of runnable threads per core and gives each a time slice. Scheduling is preemptive: when the slice ends, or a higher-priority thread becomes ready, the running thread is interrupted and another runs. Threads that wait for I/O, locks or timers are blocked and don't use CPU. Windows uses priority-based round-robin with temporary boosts; Linux uses a fair scheduler (CFS, and EEVDF from kernel 6.6). For a .NET service the practical points are: don't play with thread priorities; blocking threads wastes them; and in containers, a CPU limit is enforced as a quota per 100 ms period, so a burst of work can use the quota early and get throttled, causing latency spikes. Environment.ProcessorCount follows the limit and sizes the thread pool and GC.
How to tell it (STAR)
**S:** Our [payments] API on AKS had p99 spikes of [450 ms] against a [200 ms] SLO, yet average CPU was under half. **T:** I was asked to find out why before [the end-of-month peak]. **A:** I ruled out the database with Application Insights, then checked Container Insights and found [35%] of CFS periods throttled. The CPU limit equalled the request at [500m], so short bursts of GC and request work used the whole quota. I raised the limit to [2], set the request from load-test data, and added a throttling alert and a namespace default. **R:** p99 fell to [110 ms], we handled [the peak] with no SLO breach, and the CPU limit policy became a team standard.
Last updated 23 Nov 2026.