Skip to content

GC heap corruption with GCLargePages #126903

Description

@neiljohari

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

gc-largepages-repro.zip

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 aggressive

Option 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 forced

The repro:

  1. Creates 4 writer threads writing to a shared ConcurrentDictionary<string, byte[]>
  2. A separate thread allocates temporary SOH/LOH arrays (creating GC region churn) and periodically calls GC.Collect(2, mode, true, true)
  3. With GCCollectionMode.Aggressive: Corruption typically occurs within seconds
  4. 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=1 with 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.

Activity

  1. added
    untriaged
    on Apr 14, 2026
  2. dotnet-policy-service commented on Apr 14, 2026

    @dotnet-policy-service
    Contributor

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

  3. mangod9 commented on Apr 15, 2026

    @mangod9
    Member

    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?

  4. BenV commented on Apr 15, 2026

    @BenV

    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.

  5. mangod9 commented on Apr 15, 2026

    @mangod9
    Member

    Based on what version your app is running on, I can build a standalone GC binary you can run in your test environment.

  6. BenV commented on Apr 15, 2026

    @BenV

    Based on what version your app is running on, I can build a standalone GC binary you can run in your test environment.

    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:

    • .NET 10.0.5 (although we eagerly update so if it isn't too much trouble maybe do a build for 10.0.6 as well?)
    • Ubuntu 22.04/kernel 5.15.0
    • x86_64

    Please let us know if you need any more information, and thanks again for the assistance!

  7. removed
    untriaged
    on Apr 16, 2026
  8. mangod9 commented on Apr 16, 2026

    @mangod9
    Member

    Activating so that we can port to 10

  9. added
    untriaged
    on Apr 16, 2026
  10. added a commit that references this issue on Apr 16, 2026
  11. 11 remaining items

  12. janvorli commented on Apr 28, 2026

    @janvorli
    Member

    Yes, I'd prefer the minimal change for 10.

  13. added a commit that references this issue on Apr 29, 2026
  14. mangod9 commented on Apr 29, 2026

    @mangod9
    Member

    Closing since the fix has also been back ported to 10.

  15. removed
    untriaged
    on Apr 29, 2026
  16. BenV commented on May 6, 2026

    @BenV

    @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:

    • Pulling the 10.x source and applying 5713889 on top of it
    • Building a release build and copying libclrgcexp.so into our service's container image
    • Setting DOTNET_GCPath=/app/lib/libclrgcexp.so along with our normal env vars when running the service

    This 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!

  17. mangod9 commented on May 6, 2026

    @mangod9
    Member

    ok interesting. Does the dump show the same stack trace for AV, or is it possible that this is a different issue?

  18. BenV commented on May 6, 2026

    @BenV

    ok interesting. Does the dump show the same stack trace for AV, or is it possible that this is a different issue?

    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:

    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)
    

    This seems to cause a sharp increase in memory usage metrics, for example LOH, GC Pause Time, and Allocation Rate:
    Image
    Image
    Image

    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):
    Image

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

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

    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?

  19. mangod9 commented on May 6, 2026

    @mangod9
    Member

    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?

  20. BenV commented on May 6, 2026

    @BenV

    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?

    I'm unfortunately not going to be able to keep running this version (with GCLargePages enabled) 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?)

  21. BenV commented on May 6, 2026

    @BenV

    @mangod9 I believe we were able to come up with another reproduction case, I filed another ticket here: #127892

  22. locked and limited conversation to collaborators on Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions