What .NET 10 escape analysis stack-allocates, and what it doesn't

The same C# code allocates less on .NET 10 than on .NET 9 without a single source change. In our runs, a three-element int[] went from 40 heap bytes per call to 0, an array held in a local struct field went from 64 bytes to 0, and a capturing lambda went from 88 bytes to 24. Other cases did not move at all: an array passed to a method the JIT won’t inline, an array stored in a field, an object allocated inside a loop, and any array whose length isn’t a JIT-time constant.
The difference comes from the JIT’s escape analysis, which decides whether an allocation can outlive the method that creates it. If it can’t, the JIT places the object in the stack frame instead of on the garbage-collected heap. This post shows which patterns qualify as of .NET 10, the source-level rules behind each result, the cost of stack allocation that the allocation column hides, and how to check your own hot paths. It doesn’t cover the Native AOT or ReadyToRun compilers beyond one caveat, and it doesn’t cover the other .NET 10 JIT changes.
Escape analysis in one paragraph
An object escapes when a reference to it can be observed after the allocating method returns: stored in a field or static, returned, or passed to code the JIT can’t see. The object stack allocation design doc describes the analysis as intra-procedural. The JIT reasons about one method at a time, after inlining, so an argument passed to a call that wasn’t inlined counts as escaping. The .NET 10 runtime what’s-new page states the same rule: objects escape when assigned to non-local variables or passed to functions that aren’t inlined.
.NET 10 widened the set of allocations the analysis can track. Per the what’s-new page and Stephen Toub’s Performance Improvements in .NET 10, the JIT can now stack-allocate:
- small fixed-size arrays of value types and of reference types (dotnet/runtime#104906, #112250);
- objects referenced from fields of local structs, which includes
Span<T>wrapping an array (#113977, #116124); - delegates whose target method is known (#115172).
Closures are not on that list. The what’s-new page says the compiler-generated closure class stays on the heap and that closure stack allocation is planned for a future release.
How we measured: one source, two JITs
The test project targets net9.0 and net10.0 with <LangVersion>13</LangVersion> for both, so the C# compiler emits the same IL and only the JIT differs. It uses BenchmarkDotNet 0.15.8 with MemoryDiagnoser and DisassemblyDiagnoser. The first reports heap bytes per operation; the second dumps the Tier-1 machine code BenchmarkDotNet actually measured.
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net9.0;net10.0</TargetFrameworks>
<LangVersion>13</LangVersion>
<Optimize>true</Optimize>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>
Environment: Debian 13 (trixie), Linux kernel 6.12, an Intel Xeon at 2.40 GHz with 8 cores visible to the VM and 15 GB of RAM. .NET SDK 10.0.402, with runtimes .NET 10.0.14 and .NET 9.0.21, both X64 RyuJIT targeting x86-64-v4. BenchmarkDotNet ran its default job: it picks warm-up and iteration counts itself, runs each benchmark in a separate process, and reports the mean and standard deviation. The main run of 28 benchmarks took 26 minutes; a second run of 10 boundary benchmarks took 11 minutes.
Treat the allocation column as the primary evidence. It is deterministic: a case either calls the allocator or doesn’t. The timings are sub-nanosecond to tens of nanoseconds on a shared VM and move between runs; the same int[128] benchmark measured 6.33 ns in one run and 4.38 ns in the other. The project, the raw reports and the full disassembly are in the sample zip.
Results: allocated bytes per call on .NET 9 and .NET 10
| Case | .NET 9.0.21 | .NET 10.0.14 |
|---|---|---|
FixedIntArray: int[] numbers = { _seed, 2, 3 }, summed locally |
8.07 ± 0.87 ns, 40 B | 1.21 ± 0.04 ns, 0 B |
RefArrayInlinedCallee: new string[3] passed to an inlined helper |
10.75 ± 1.02 ns, 48 B | 1.19 ± 0.08 ns, 0 B |
ArrayInLocalStructField: new Holder { Arr = new int[10] } |
5.57 ± 0.52 ns, 64 B | 0.30 ± 0.04 ns, 0 B |
ArrayAsSpan: new int[] {...}.AsSpan(1, 2) |
7.03 ± 0.96 ns, 40 B | 0.28 ± 0.05 ns, 0 B |
CapturingLambda: Func<int,int> addY = x => x + y |
12.95 ± 0.98 ns, 88 B | 4.77 ± 0.51 ns, 24 B |
RefArrayNoInliningCallee: same array, [NoInlining] helper |
8.90 ± 1.06 ns, 48 B | 5.49 ± 0.55 ns, 48 B |
StoredInField: _field = new int[] {...} |
9.24 ± 1.06 ns, 40 B | 7.65 ± 0.79 ns, 40 B |
ObjectInLoop: new Point (sealed class) 16 times in a loop |
71.40 ± 6.04 ns, 384 B | 74.43 ± 5.27 ns, 384 B |
Time is mean ± standard deviation per call.
Heap bytes per call from BenchmarkDotNet’s MemoryDiagnoser, same IL on both runtimes.
The five cases that dropped are the three feature areas from the release notes. The three that didn’t drop each hit a specific rule, covered below.
What the generated code shows
The disassembly makes the change concrete. Here is ArrayInLocalStructField on .NET 9. The array comes from the allocation helper:
; .NET 9.0.21, EscapeCases.ArrayInLocalStructField()
push rbx
mov rbx,rdi
mov rdi,offset MT_System.Int32[]
mov esi,0A
call CORINFO_HELP_NEWARR_1_VC
mov ecx,[rbx+10]
; ...
; Total bytes of code 36
On .NET 10 the helper call is gone. The JIT reserves frame space, zeroes it, writes the array’s method table pointer and length (10) into the frame by hand, and uses a pointer to the frame as the array:
; .NET 10.0.14, EscapeCases.ArrayInLocalStructField()
sub rsp,38
vxorps xmm8,xmm8,xmm8
vmovdqu ymmword ptr [rsp],ymm8
vmovdqa xmmword ptr [rsp+20],xmm8
xor eax,eax
mov [rsp+30],rax
mov rax,offset MT_System.Int32[]
mov [rsp],rax
lea rax,[rsp]
mov dword ptr [rax+8],0A
; ...
; Total bytes of code 71
The code nearly doubles in size, from 36 to 71 bytes, and still runs faster: 0.30 ns against 5.57 ns. That pattern repeats across the array cases. Stack allocation trades a call into the allocator, and later GC work, for inline zeroing code.
The lambda case goes further. On .NET 9, CapturingLambda calls CORINFO_HELP_NEWSFAST twice, once for the closure class and once for the Func<int,int>, then a write barrier (CORINFO_HELP_ASSIGN_REF) to store the closure into the delegate, then an indirect call through the delegate: 124 bytes of code. On .NET 10, the delegate never materializes. Once it lives on the stack, the JIT knows its target and inlines the lambda body:
; .NET 10.0.14, EscapeCases.CapturingLambda()
push rbx
mov rbx,rdi
mov rdi,offset MT_EscapeCases+<>c__DisplayClass10_0
call CORINFO_HELP_NEWSFAST ; the closure, still on the heap
mov ecx,[rbx+10]
mov [rax+8],ecx
add ecx,2A ; x + y, with x = 42, inlined
mov eax,ecx
add eax,eax ; * 2 from Apply
pop rbx
ret
; Total bytes of code 34
The remaining 24 bytes are the closure object that holds y. This matches Toub’s own example in his .NET 10 post, which also went from 88 to 24 bytes with the closure left on the heap.
The rules behind the misses
The allocation-side logic lives in objectalloc.cpp on the release/10.0 branch. When the JIT keeps an allocation on the heap, it records a reason string, and several of our cases map directly onto those strings.
A simplified view of the checks. Other heap reasons exist, such as finalizers, ReadyToRun arrays and cloned allocations.
Non-inlined calls and field stores escape
RefArrayNoInliningCallee is the same code as RefArrayInlinedCallee except for [MethodImpl(MethodImplOptions.NoInlining)] on the helper. The array allocates 48 bytes on both runtimes, and the .NET 10 disassembly still calls CORINFO_HELP_NEWARR_1_PTR. The JIT can’t see what the callee does with the array, so it assumes the worst. The same applies to any callee too large to inline, virtual calls the JIT can’t devirtualize, and interface calls. Don’t read much into the 8.90 ns versus 5.49 ns timing difference for this case; the allocation is identical, and the variance between runs on this VM is of that order.
StoredInField is the simplest escape: once _field holds the array, the array outlives the call. No analysis can change that.
Allocations inside a loop stay on the heap
ObjectInLoop allocates a 24-byte Point 16 times per call, 384 bytes in total, on both runtimes. The source records [alloc in loop] for allocations in a basic block that is the target of a backward jump. The reason: a stack slot is reused across iterations, so the JIT would have to prove each iteration’s object is dead before the next one starts. The design doc lists loop allocations as a known restriction. If a hot loop allocates a small helper object, hoisting it out of the loop or switching to a struct is still on you.
The size limit: 528 bytes, header included
The default limit is the JitObjectStackAllocationSize config value, 528 bytes, defined in jitconfigvalues.h. For arrays, the size the JIT compares includes the 16-byte array header on x64, so the largest stack-allocatable int[] is 128 elements (16 + 512 = 528) and the largest long[] is 64. We measured both edges:
| Case | .NET 9.0.21 | .NET 10.0.14 |
|---|---|---|
new int[16] |
6.93 ± 0.38 ns, 88 B | 0.17 ± 0.02 ns, 0 B |
new int[64] |
14.01 ± 0.33 ns, 280 B | 1.79 ± 0.11 ns, 0 B |
new int[128] |
22.92 ± 0.88 ns, 536 B | 4.38 ± 0.27 ns, 0 B |
new int[129] |
23.65 ± 0.54 ns, 544 B | 23.45 ± 0.63 ns, 544 B |
new long[64] |
22.34 ± 1.64 ns, 536 B | 4.11 ± 0.09 ns, 0 B |
new long[65] |
24.13 ± 1.73 ns, 544 B | 22.15 ± 1.12 ns, 544 B |
new int[256] |
42.75 ± 1.48 ns, 1048 B | 39.12 ± 0.22 ns, 1048 B |
new int[_len], _len = 16 |
7.01 ± 0.66 ns, 88 B | 5.90 ± 0.33 ns, 88 B |
One element past the limit, the .NET 10 time jumps from about 4 ns back to about 23 ns, the same as .NET 9. The rows for 16, 64 and 256 elements come from the first run, the rest from the second.
The length must be a JIT-time constant
The last row is the same 16-element array as the first, except that the length comes from a field. It allocates 88 bytes on both runtimes. The source calls this [non-constant array size]: the JIT has to know the frame size when it compiles the method. If you know the bound, write it as a literal or a const.
Stack allocation is not free
The stack-allocated cases are not equally cheap. new int[16] costs 0.17 ns and new int[128] costs 4.4 to 6.3 ns depending on the run, because the JIT has to zero the frame space on every call, just as the GC allocator returns zeroed memory. A large stack-allocated buffer also makes the frame bigger, which matters in deep recursion. For a buffer you fill entirely before reading, stackalloc with [SkipLocalsInit] or an ArrayPool<T> rental remain the explicit tools.
Caveats
- Tier-1 code only. BenchmarkDotNet measures code after tiered compilation has finished. Tier-0 code, which runs first in every method, doesn’t do this analysis.
- ReadyToRun. The source has a
[R2R array not yet supported]reason, so array stack allocation doesn’t apply to precompiled ReadyToRun code. We didn’t test ReadyToRun or Native AOT. - Inlining decides most of it. Any change to inlining (callee size, a profile-guided decision) can move a case from 0 bytes to the heap without a visible code change at the allocation site.
- What we didn’t test: single class objects outside loops, boxing, and the conditional-escape cloning of enumerators that Toub describes (#117222). Those exist in .NET 10, but we have no measurements of our own to report.
- One machine. All timings come from one VM. The allocated bytes should reproduce anywhere on x64 with the same runtime versions; the nanoseconds won’t.
Takeaways
- Allocation numbers from .NET 9 benchmarks are stale for small, local arrays and delegates. Re-run
MemoryDiagnoseron .NET 10 before rewriting code to avoid an allocation the JIT may already remove. - “Does it escape?” usually means “does every use get inlined?”. A
[NoInlining]attribute, a large callee or an interface call keeps the object on the heap. - The rules are mechanical and visible in source: constant size of at most 528 bytes, no loop, no escape. Code that depends on them is fragile, so measure rather than assume.
- Closures still allocate. If a lambda in a hot path captures locals, the closure object is where the remaining bytes come from.
Check your own hot paths
Add [MemoryDiagnoser] and [DisassemblyDiagnoser(maxDepth: 1)] to a benchmark of the method you care about, run it with --runtimes net9.0 net10.0, and look for CORINFO_HELP_NEW* calls in the exported assembly. To confirm a 0-byte result comes from stack allocation, set DOTNET_JitObjectStackAllocation=0 in the benchmark’s environment and see the bytes return. The sample project has all 19 cases and the raw results; start from the case closest to your code.
