Repository navigation
Description
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>- Run on CoreCLR:
dotnet run -c Release -p:PublishAot=false - Publish with NativeAOT and run the executable:
dotnet publish -c Release -o outthenout\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.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Oct 24, 2026
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.
jkotas commentedon Oct 24, 2026
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.UINT64and divides by three, producing an enormous pressure threshold.long. ItsheapOver3is 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:
10_000_000_000_000_000pressure 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.dotnet-policy-service commentedon Oct 24, 2026
Tagging subscribers to this area: @anicka-net, @dotnet/gc
See info in area-owners.md if you want to be subscribed.
jkotas commentedon Oct 24, 2026
It would be nice to de-duplicate GC.Add/RemoveMemoryPressure implementations as part of fixing this bug.
matyaskollert commentedon Oct 27, 2026
@jkotas would it be preferable to keep the native implementations since these functions are called by both the native and managed code?
jkotas commentedon Oct 27, 2026
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.
Share GC memory-pressure handling between CoreCLR and NativeAOT (#136644