Repository navigation
Description
Description
We (@BenV and @andyblox) are experiencing what appears to be heap corruption when using GCLargePages in production.
This can be forced to happen by using GC.Collect with GCCollectionMode.Aggressive when GCLargePages
mode is enabled on Linux.
When DOTNET_GCLargePages=1 is enabled (with real kernel huge pages via hugetlbfs),
calling GC.Collect(2, GCCollectionMode.Aggressive, true, true) can cause GC
managed heap corruption. The corruption manifests as NullReferenceException and/or
AccessViolationException inside ConcurrentDictionary internals, with multiple
threads faulting simultaneously, which is characteristic of a GC heap corruption event.
Switching to GCCollectionMode.Forced (same generation, same blocking/compacting
parameters) works as expected with no corruption.
While GCCollectionMode.Aggressive does appear to make the heap corruption significantly more likely to happen
we are not using that in our application, so we would like assistance in tracking down the root cause.
Analysis
The GCCollectionMode.Aggressive documentation states it "requests that the garbage
collector decommit as much memory as possible." We believe the aggressive decommit
code path interacts incorrectly with the GC's large-page memory management:
When GCLargePages is enabled the garbage collector skips de-commits at the OS level
and also has special logic regarding clearing regions.
When an aggressive GC is induced, all regions are flagged for de-commit
and since de-commit success is always true for large pages the bookkeeping is updated even though
the de-commit never really happened, which could potentially cause corruption.
It is possible that the aggressive de-commit may need to be skipped when GCLargePages is active,
however it is also possible that this reproduction via forcing Aggressive GC is simply exposing
existing bugs within the de-commit logic that is leading to re-using pages which have not been
zeroed out by the OS.
We discovered this issue while trying to track down a problem we're hitting recently in production
when GCLargePages mode is active - we are occasionally getting NullReferenceExceptions related
to ConcurrentDictionary where it does not seem possible to throw a NullReferenceException:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Collections.Concurrent.ConcurrentDictionary`2.GrowTable(Tables tables, Boolean resizeDesired, Boolean forceRehashIfNonRandomized)
We created this example program as a way to try and reproduce that original issue, and while it is possible they are
related we do not believe we are either directly or indirectly calling GC.Collect with GCCollectionMode.Aggressive.
We would be very interested in hearing your thoughts on whether there could be any bugs lurking around
that could be causing the similar issues that we are encountering in production.
Reproduction
Minimal repro program
Option 1: Docker (requires --privileged for huge page setup)
# Triggers corruption, usually within seconds:
./run.sh aggressive
# Control, identical workload, runs clean:
./run.sh forced
# To test with a different .NET version:
DOTNET_VERSION_ARG=10.0 ./run.sh aggressiveOption 2: Run locally on Linux
Requires real kernel huge pages allocated ahead of time (at least 4000 pages = 8GB):
# Reserve huge pages (requires root):
echo 4000 | sudo tee /proc/sys/vm/nr_hugepages
# Build (env vars must NOT be set during build):
dotnet build -c Release
# Triggers corruption, usually within seconds:
DOTNET_GCLargePages=1 DOTNET_GCHeapHardLimit=0xC0000000 \
dotnet bin/Release/net8.0/GCLargePagesRepro.dll aggressive
# Control, identical workload, runs clean:
DOTNET_GCLargePages=1 DOTNET_GCHeapHardLimit=0xC0000000 \
dotnet bin/Release/net8.0/GCLargePagesRepro.dll forcedThe repro:
- Creates 4 writer threads writing to a shared
ConcurrentDictionary<string, byte[]> - A separate thread allocates temporary SOH/LOH arrays (creating GC region churn) and periodically calls
GC.Collect(2, mode, true, true) - With
GCCollectionMode.Aggressive: Corruption typically occurs within seconds - With
GCCollectionMode.Forced: runs clean for the full duration
Configuration that triggers the bug
DOTNET_GCLargePages=1
GC.Collect(2, GCCollectionMode.Aggressive, true, true)
Configurations that do NOT trigger the bug
DOTNET_GCLargePages=1
GC.Collect(2, GCCollectionMode.Forced, true, true)
DOTNET_GCLargePages=0
GC.Collect(2, GCCollectionMode.Aggressive, true, true)
Expected behavior
GC.Collect(2, GCCollectionMode.Aggressive, true, true) should not corrupt the
managed heap regardless of whether large pages are enabled.
Observed behavior
Heap corruption and access violations or null reference exceptions. Corruption typically
occurs within seconds of starting, before the first status line is printed:
=== GCLargePages + Aggressive GC: Heap Corruption Repro ===
GC collect mode: Aggressive
Server GC: True
GCLargePages: 1
Runtime: .NET 8.0.25
Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Collections.Concurrent.ConcurrentDictionary`2[[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryAddInternal(...)
at Program.WriterLoop(Int32)
at Program+<>c__DisplayClass3_1.<Main>b__0()
[11:24:19.849] *** NRE on thread 3 — HEAP CORRUPTION ***
[11:24:19.847] *** NRE on thread 1 — HEAP CORRUPTION ***
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at System.Collections.Concurrent.ConcurrentDictionary`2.GrowTable(Tables tables, Boolean resizeDesired, Boolean forceRehashIfNonRandomized)
at System.Collections.Concurrent.ConcurrentDictionary`2.TryAddInternal(...)
at Program.WriterLoop(Int32 tid) in /app/Program.cs
The corruption cascades rapidly and multiple threads fault at the same millisecond,
consistent with a single GC event corrupting a heap region that multiple threads
then read from.
Regression?
No response
Known Workarounds
No response
Configuration
Environment
- Versions: .NET 8.0/10.0 (for .NET 10, disabling DATAS helps reproduce)
- OS: Linux (tested on official Microsoft .NET Docker images)
- Arch: x86_64
- Server/Concurrent GC enabled
DOTNET_GCLargePages=1with real kernel huge pages
Other information
When GCLargePages is enabled the garbage collector skips de-commits at the OS level
and also has special logic regarding clearing regions.
When an aggressive GC is induced, all regions are flagged for de-commit
and since de-commit success is always true for large pages the bookkeeping is updated even though
the de-commit never really happened, which could potentially cause corruption.
It is possible that the aggressive de-commit may need to be skipped when GCLargePages is active,
however it is also possible that this reproduction via forcing Aggressive GC is simply exposing
existing bugs within the de-commit logic that is leading to re-using pages which have not been
zeroed out by the OS.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Apr 14, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/gc
See info in area-owners.md if you want to be subscribed.
mangod9 commentedon Apr 15, 2026
I can repro it and have a potential fix: #126929. Would you be able to try a private to ensure it works in your scenario?
BenV commentedon Apr 15, 2026
Wow thanks so much for the quick turnaround, the fix looks reasonable (and in line with what we were expecting) - we would be happy to help validate, although I'm unsure how difficult it would be to test with these changes in our production app.
mangod9 commentedon Apr 15, 2026
Based on what version your app is running on, I can build a standalone GC binary you can run in your test environment.
BenV commentedon Apr 15, 2026
That would be super helpful since we could just copy that library into our current image and conditionally load it on a few allocations rather than creating an entire new .NET base across the board. We are currently running on:
Please let us know if you need any more information, and thanks again for the assistance!
fix for largepages with agressive decommit logic (#126929)
mangod9 commentedon Apr 16, 2026
Activating so that we can port to 10
Port 126929 to 10.0
11 remaining items
janvorli commentedon Apr 28, 2026
Yes, I'd prefer the minimal change for 10.
Port 126929 to 10.0 (#126977)
mangod9 commentedon Apr 29, 2026
Closing since the fix has also been back ported to 10.
BenV commentedon May 6, 2026
@mangod9 Unfortunately we just ran into this issue again in production after running for over 2 weeks with the patched version of the GC. In case it matters, we are doing the following:
libclrgcexp.sointo our service's container imageDOTNET_GCPath=/app/lib/libclrgcexp.soalong with our normal env vars when running the serviceThis method does resolve the issue for the standalone reproduction case, but I'm wondering if there's any reason you can think of that using the "custom GC" approach would invalidate the production test. If not, then I fear there is another way of triggering this issue hiding somewhere (perhaps #126977 (comment)?)
Let me know what you think, thanks!
mangod9 commentedon May 6, 2026
ok interesting. Does the dump show the same stack trace for AV, or is it possible that this is a different issue?
BenV commentedon May 6, 2026
Yeah, stack trace and symptoms look the same - things were running normally, then we get several hundred NRE within a short period starting at ~18:09 on the charts below:
This seems to cause a sharp increase in memory usage metrics, for example LOH, GC Pause Time, and Allocation Rate:



Shortly after the instance gets killed for memory usage being above our 80% threshold and restarts.
One interesting thing is that if I look back a bit further I see memory usage start to climb before the really sharp increase at the time of the failure (the yellow line vs the red which is another healthy instance):

We can also see the thread count and thread queue increase around the time of failure:

Some more graphs comparing the unhealthy instance to healthy ones for the time leading up to the incident, in case they are helpful:


I'll dig around for some more clues tomorrow AM. Let me know if there's any other metrics etc you'd like me to dig up that might be helpful. The service in question runs in a container with a 60GB memory limit and has several large in-memory caches backed by ConcurrentDictionary. Maybe we have some app-level bug that triggers the memory growth we see leading up to the incident and this changes GC behavior somehow?
mangod9 commentedon May 6, 2026
This looks like a different problem I am guessing, possibly causing some leak but the NRE in ConcurrentDictionary do look suspicious and feel like a runtime problem. Is it feasible to share a dump when the failure occurs?
BenV commentedon May 6, 2026
I'm unfortunately not going to be able to keep running this version (with
GCLargePagesenabled) in production if it is still known to cause a crash since it is a critical service and leads to people getting paged, etc. I will try and investigate the leak behavior that seemed to lead to the NREs in hopes of finding another standalone reproduction and report back unless you have any other ideas (would it be worth attempting to use a build patched with #127290 instead?)BenV commentedon May 6, 2026
@mangod9 I believe we were able to come up with another reproduction case, I filed another ticket here: #127892
perf(gc): DATAS GC, calmer budget service, weak-ref track-state messe…
fix for largepages with agressive decommit logic (#126929)
Skip decommit for large pages and add fake large pages test mode (#12…