Skip to content

JIT: optimizing method blows up memory with OOM one 2.8KB-IL method with full ops takes 12s and 7.9GB of mem #133147

Closed

Description

@DomenPigeon

Description

On some of our pipelines the runners went OOM after a detailed investigation we have found out that it's a JIT issue. We found that the issue was related and only occurred after we used the eziriz dotnet reactor obfucation tool. As this is proprietary software we cannot really include it, but I have managed to create the smallest possible reproduction case with a bot. Here are the results:

RyuJIT's full-optimization compilation of one method goes superlinear: 10 s of CPU on one core and ~7.9 GB of JIT arena memory for a 2 415-byte-IL method. The same method compiles in 2 ms / 31 MB with DOTNET_JitNoInline=1. The generated code is correct — only JIT throughput and JIT memory are affected.

The method is the C# compiler's async state machine MoveNext for an ordinary 76-line method (an await using, two nested foreach loops, an inner loop writing through a MemoryStream, one awaited call). Compiled straight from that C# it is 1 007 bytes of IL and compiles in 3 ms. The attached IL is the same method after control-flow flattening was applied to it: dispatcher switch loops around the body, opaque predicates reading static fields, and backward branches inside finally handlers. Because of the backward branch in a handler the method is not OSR-eligible, so on its first call the JIT reports Tier-0 switched to FullOpts and runs the full optimizer synchronously on the calling thread.

The repro is a plain .il file — readable, editable, and buildable with nothing but the .NET SDK.

Reproduction Steps

Attached repro.zip. ./run.sh (or run.cmd) builds everything and prints the whole table below; manually it is:

dotnet build ReproIL/ReproIL.ilproj -c Release      # ilasm via Microsoft.NET.Sdk.IL
dotnet build Runner/Runner.csproj   -c Release

DOTNET_TieredCompilation=0 dotnet Runner/bin/Release/net10.0/Runner.dll ReproIL/bin/Release/net10.0/Repro.dll 300

Runner calls RuntimeHelpers.PrepareMethod on every method of the assembly and prints the compile time per method plus peak working set. Output (linux-x64, clean mcr.microsoft.com/dotnet/sdk:10.0 container):

Repro.dll: methods=40 slowest=10102 ms peakWorkingSet=7855 MB
    10102 ms  IL= 2415  Repro.Store+<WriteChanges>d__0::MoveNext

win-x64: 10089 ms / 7850 MB. With default tiering the first call behaves the same: 10614 ms, and DOTNET_JitDisasmSummary=1 shows [Tier-0 switched to FullOpts, IL size=2415, code size=4201].

Source/Store.cs is the C# this IL came from; built normally its MoveNext is 1 007 B of IL and compiles in 3 ms.

repro.zip

Expected behavior

A 2.4 KB-IL method compiles in milliseconds and tens of MB, as it does with inlining disabled.

Actual behavior

setting compile peak working set
default FullOpts 10 102 ms 7 855 MB
DOTNET_JitNoInline=1 2 ms 31 MB
DOTNET_JitInlineBudget=1 3 ms 25 MB
DOTNET_JITMinOpts=1 0 ms 23 MB
DOTNET_JitInlineMethodsWithEH=0 7 926 ms 6 412 MB
DOTNET_JitEnableGuardedDevirtualization=0 8 241 ms 6 412 MB

Only the inlining knobs collapse the cost. JitInlineMethodsWithEH=0 and JitEnableGuardedDevirtualization=0 take ~20 % off — both reduce how much gets inlined — but neither fixes it.

No measurable effect: JitEnablePhysicalPromotion=0, JitEnableInductionVariableOpts=0, JitEnableStrengthReduction=0, JitEnableCrossBlockLocalAssertionProp=0, JitEnablePostorderLocalAssertionProp=0, JitEnableOptRepeat=0, JitObjectStackAllocation=0, JitCloneLoopsSizeLimit=0, JitLoopInversionSizeLimit=0, JitVNMapSelBudget=1, JitEnregStructLocals=0, EnableEHWriteThru=0, JitMinOptsBbCount=200. So the cost is in inlining, or in something inlining feeds.

The memory is the JIT's own arena, not the managed heap — see below.

Regression?

Unknown — only tested on 10.0.11 (the IL targets net10.0). Can try on a net8.0/net9.0 variant if that would help.

Known Workarounds

DOTNET_JitNoInline=1 / DOTNET_JitInlineBudget=1 / DOTNET_JITMinOpts=1, all process-wide and unacceptable in production. On our side we turned the obfuscator's control-flow flattening off.

Configuration

  • .NET 10.0.11 (Microsoft.NETCore.App 10.0.11), SDK 10.0.400
  • win-x64 (Windows 11) and linux-x64 (mcr.microsoft.com/dotnet/sdk:10.0) — same numbers on both
  • Reproduces with Server and Workstation GC alike

Other information

Some more information about how this was found, maybe it helps:

Symptom. A CI job started failing intermittently with BadGateway on the first POST /api/import/asset of a fresh tenant. The proxy logged HttpIOException: The response ended prematurely, the server log simply stopped mid-request, and sudo dmesg on the host showed Memory cgroup out of memory: Killed process (dotnet). The first HTTP request that reached one particular method blocked for 11 s while the process grew from 0.9 GB to 9.2 GB and was OOM-killed at a 4 GiB cgroup limit. Second and later calls to the same method: 4 ms.

Why it did not look like the JIT. At 5 GB RSS a full dump reported Total bytes consumed by CLR: 112 MB (eeheap), the finalizer queue held 204 KB, and dotnet-counters showed zero GC collections with a flat 48 MB GC committed size while RSS climbed at 850 MB/s — so it was clearly not the managed heap. dotnet.jit.compiled_il.size and dotnet.jit.compilation.time were also flat, which seemed to rule out the JIT; in fact those counters only tick when a compile finishes, and the compile in question had not finished yet. Server GC, glibc arena tuning (MALLOC_ARENA_MAX), exception storms and response compression were each ruled out by direct A/B.

What identified it. top -H showed one thread at 100 % CPU; gdb -p <tid> -batch -ex "info registers rip" put the instruction pointer inside libclrjit.so, in a tight loop. DOTNET_JitDisasmSummary=1 with DOTNET_JitStdOutFile then made it obvious: the summary file froze for the entire ramp, and the first line written when RSS collapsed named the method, with Tier-0 switched to FullOpts. DOTNET_JITMinOpts=1 took the request from 11 215 ms / 9.2 GB to 101 ms / 0.9 GB.

Where the IL comes from. The application's release build is processed by a commercial obfuscator (.NET Reactor) with control-flow obfuscation enabled; that is what flattens the state machine and puts backward branches into the finally handlers. Its output is not deterministic — obfuscating identical input repeatedly produces different layouts, most of which compile in ~35 ms, and roughly 1 in 300 hits this pathology (1 in 8 for the larger production method). That is why the problem appeared to come and go between releases with no relevant source change.

Minimising. Because of that non-determinism, each candidate reduction had to be re-obfuscated and re-timed 300–600 times before "no repro" meant anything. Starting from a 110-line synthetic source shaped like the production method and reducing to 76 lines kept the pathology (12 s / 7.9 GB at its worst); removing the MemoryStream calls from the inner loop (Write, ToArray, Position = 0, SetLength(0)) removed it entirely — 600 rolls, worst 47 ms. A user-written try/finally turned out not to be needed. What remains as ingredients: await using lowering (try/catch(object) + ExceptionDispatchInfo), two nested foreach try/finally regions, and an inner loop that inlines several framework methods — all inside the flattened dispatcher loops.

Removing the obfuscator from the repro. The attached Repro.il was produced by ildasm on one pathological build and then reduced by hand: anti-tamper call sites and injected static constructors removed, the string-decryption helper calls replaced with ldstr, unreferenced types dropped, remaining helper type names renamed to OpaquePredicates. Nothing from the obfuscator's runtime is left, and no obfuscator is involved in building or running the repro. HOWTO.md in the zip documents each step. I verified the untouched obfuscated assembly gives the same behaviour (12 s / 7.9 GB) so the hand-reduction did not create it — that assembly and the roll script are available on request,

Activity

  1. added
    untriaged
    on Sep 3, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 3, 2026
  3. dotnet-policy-service commented on Sep 3, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. EgorBo commented on Sep 3, 2026

    @EgorBo
    Member

    Looks fixed in .NET 11 via #125156

  5. self-assigned this
    on Sep 3, 2026
  6. added this to the 11.0.0 milestone on Sep 3, 2026
  7. removed
    untriaged
    on Sep 3, 2026
  8. EgorBo commented on Sep 3, 2026

    @EgorBo
    Member

    Backporting the fix to 10.0 via #133161

  9. EgorBo commented on Sep 10, 2026

    @EgorBo
    Member

    @DomenPigeon is there an option for you to update to .NET 11.0 (rc1 is out, GA in 2 month)? or is there a workaround for this issue for you? (Presumably, you can put MethodImpl(NoOptimization) on the problematic method.

    We're trying to figure out if the fix meets the bar for .NET 10.0 backport.

  10. RoboNET commented on Sep 10, 2026

    @RoboNET

    We have the same problem. Upgrading to .NET 11 is not an option: company policy requires the use of only LTS versions and official releases. .NET 8 did not have this issue.

  11. JulieLeeMSFT commented on Sep 10, 2026

    @JulieLeeMSFT
    Member

    Backported to .NET 10. Closing.

  12. DomenPigeon commented on Sep 18, 2026

    @DomenPigeon
    Author

    @DomenPigeon is there an option for you to update to .NET 11.0 (rc1 is out, GA in 2 month)? or is there a workaround for this issue for you? (Presumably, you can put MethodImpl(NoOptimization) on the problematic method.

    We're trying to figure out if the fix meets the bar for .NET 10.0 backport.

    Sorry for late reply, was on vacations.
    Backport confirmed on my side against the original repro.

    runtime MoveNext compile peak WS
    10.0.11 11 903 ms 7 852 MB
    10.0.13-servicing.26466.121 (nightly) 9 ms 27 MB
    11.0.0-rc.1.26425.128 8 ms 27 MB
  13. locked and limited conversation to collaborators on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions