Skip to content

GC.AddMemoryPressure ends an active NoGC region on NativeAOT (but not on CoreCLR) #136426

Open

Description

@Dev-Taehui

Description

On NativeAOT, calling GC.AddMemoryPressure while inside a NoGC region (GC.TryStartNoGCRegion) eventually triggers a gen2 collection, which ends the NoGC region (GCSettings.LatencyMode goes back to Interactive). On CoreCLR the same code keeps the region intact.

This matters in practice because CsWinRT's WinRT.IObjectReference constructor calls GC.AddMemoryPressure(1000) for every object reference, so any WinUI/Win2D app that renders inside a NoGC region loses the region within moments when published with NativeAOT.

Reproduction Steps

Program.cs:

using System;
using System.Runtime;

Console.WriteLine($"Start: {GC.TryStartNoGCRegion(4000L * 1000 * 1000)}");
for (var i = 1; i <= 2000; ++i)
{
    GC.AddMemoryPressure(100_000);
    GC.RemoveMemoryPressure(100_000);
    if (GCSettings.LatencyMode != GCLatencyMode.NoGCRegion)
    {
        Console.WriteLine($"NoGC region ended at iteration {i}, gen2={GC.CollectionCount(2)}");
        return;
    }
}
Console.WriteLine("NoGC region still active");

Project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
    <PublishAot>true</PublishAot>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>
</Project>
  1. Run on CoreCLR: dotnet run -c Release -p:PublishAot=false
  2. Publish with NativeAOT and run the executable: dotnet publish -c Release -o out then out\repro.exe

Expected behavior

Same as CoreCLR — the NoGC region stays active:

Start: True
NoGC region still active

Actual behavior

NativeAOT ends the NoGC region after a few calls:

Start: True
NoGC region ended at iteration 42, gen2=2

Regression?

Unknown.

Known Workarounds

Stub out the memory pressure APIs with an ILC substitution (<IlcArg Include="--substitution:Substitutions.xml" />):

<linker>
  <assembly fullname="System.Private.CoreLib">
    <type fullname="System.GC">
      <method signature="System.Void AddMemoryPressure(System.Int64)" body="stub" />
      <method signature="System.Void RemoveMemoryPressure(System.Int64)" body="stub" />
    </type>
  </assembly>
</linker>

With this, the NoGC region survives 1,000,000 AddMemoryPressure(1000)/RemoveMemoryPressure(1000) calls on NativeAOT.

Configuration

  • .NET SDK 11.0.100-rc.2.26504.105, ILCompiler 11.0.0-rc.2.26504.105
  • Windows 11 x64
  • Workstation GC, concurrent (default)

Other information

Plain allocations inside the region (multi-threaded, LOH, new threads, idle) behave identically on both runtimes; only memory pressure differs. Outside a NoGC region both runtimes induce a similar number of gen2 GCs from memory pressure (1M × 1000 B add/remove: CoreCLR 71, NativeAOT 54).

It looks like CoreCLR's memory-pressure-triggered collection is a non-forced collection that the GC skips while a NoGC region is active, whereas NativeAOT's managed GC.AddMemoryPressure (GC.NativeAot.cs) collects via RhCollect in a way that ends the region.

Activity

  1. added
    untriaged
    on Oct 24, 2026
  2. dotnet-policy-service commented on Oct 24, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
    See info in area-owners.md if you want to be subscribed.

  3. jkotas commented on Oct 24, 2026

    @jkotas
    Member

    The current implementation of GC.AddMemoryPressure/GC.RemoveMemoryPressure does not guarantee that it won't trigger Gen2 GC on either CoreCLR or NativeAOT.

    The behavior you are seeing is actually a bug in the CoreCLR implementation. Copilot description of the bug:

    Both runtimes request a nonblocking gen2 collection. Neither path inherently preserves a NoGC region: explicitly calling GC.Collect(2, GCCollectionMode.Default, blocking: false) ends it on both.

    The divergence occurs earlier, in the pressure-budget calculation:

    • get_current_allocated() subtracts the remaining allocation budget from the desired allocation. Starting a large NoGC region increases the remaining budget, so this calculation can become negative and wrap in the unsigned heap-size result.
    • CoreCLR treats that result as UINT64 and divides by three, producing an enormous pressure threshold.
    • NativeAOT receives it as signed long. Its heapOver3 is negative, leaving the minimum 4 MiB pressure threshold in effect. That explains collection at iteration 42: 42 × 100,000 exceeds 4 MiB.

    Here is an example that will trigger Gen2 GC on CoreCLR as well:

    using System;
    using System.Runtime;
    
    Console.WriteLine($"Start: {GC.TryStartNoGCRegion(4000L * 1000 * 1000)}");
    for (var i = 1; i <= 2000; ++i)
    {
        GC.AddMemoryPressure(10_000_000_000_000_000);
        GC.RemoveMemoryPressure(10_000_000_000_000_000);
        if (GCSettings.LatencyMode != GCLatencyMode.NoGCRegion)
        {
            Console.WriteLine($"NoGC region ended at iteration {i}, gen2={GC.CollectionCount(2)}");
            return;
        }
    }
    Console.WriteLine("NoGC region still active");
    

    10_000_000_000_000_000 pressure is not realistic. It is just an easy to demonstrate that the problem can reproduce on CoreCLR too. It is possible to create more complicated more realistic repros.

  4. dotnet-policy-service commented on Oct 24, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @anicka-net, @dotnet/gc
    See info in area-owners.md if you want to be subscribed.

  5. jkotas commented on Oct 24, 2026

    @jkotas
    Member

    It would be nice to de-duplicate GC.Add/RemoveMemoryPressure implementations as part of fixing this bug.

  6. matyaskollert commented on Oct 27, 2026

    @matyaskollert
    Member

    @jkotas would it be preferable to keep the native implementations since these functions are called by both the native and managed code?

  7. jkotas commented on Oct 27, 2026

    @jkotas
    Member

    these functions are called by both the native and managed code

    That is not a fundamental problem. We have hundreds of callbacks from C++ code back to C# - look for UnmanagedCallersOnlyCaller. I do not see a problem using this scheme here.

    In general, we prefer C# as the implementation language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-GC-coreclruntriaged

Type

No type

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions