Skip to content

GC.GetTotalMemory looks like it counts gen1/gen2 twice on the segments GC (macOS) #134755

Closed

Description

@akirayamamoto

Description

On macOS, GC.GetTotalMemory returns much more than the live heap. In the repro below the small objects seem to be counted twice, while the LOH array is counted once. We noticed it because a memory test reads about 2x on macOS compared to Linux for the same allocations.

With DOTNET_GCName=libclrgcexp.dylib (regions) on the same Mac the numbers match, and with libclrgc.dylib (segments) they don't, so I think it's specific to the segments GC.

Reproduction steps

var keep = new object[1_000_000];
for (var i = 0; i < keep.Length; i++)
    keep[i] = new object();

var total = GC.GetTotalMemory(forceFullCollection: true);
var info = GC.GetGCMemoryInfo();
Console.WriteLine($"GC.GetTotalMemory:               {total / 1048576.0:F1} MiB");
Console.WriteLine($"HeapSizeBytes - FragmentedBytes: {(info.HeapSizeBytes - info.FragmentedBytes) / 1048576.0:F1} MiB");
GC.KeepAlive(keep);

Expected behavior

Both numbers roughly match, about 30.5 MiB (1M objects x 24 bytes is ~22.9 MiB, plus the ~7.6 MiB array on the LOH).

Actual behavior

macOS arm64, workstation GC, .NET 8.0.30 and 10.0.11 (server GC gives the same):

GC.GetTotalMemory:               53.6 MiB
HeapSizeBytes - FragmentedBytes: 30.6 MiB

53.6 is about 2 x 22.9 + 7.6.

Regression?

I think so, since .NET 7. It looks like it came from the ApproxTotalBytesInUse rewrite in #59283 (Enable GC regions by default). On the segments path gen0 is measured from the start of the ephemeral segment:

gen0_size = current_alloc_allocated - heap_segment_mem (current_eph_seg);

In a small heap gen1 and gen2 live on that same segment, so they're already included, and then the loop adds generation_size (1) and generation_size (2) on top. The .NET 6 version counted the ephemeral segment once and only added the other gen2 segments. I haven't run the repro on .NET 6 though, this is from reading the code.

Other information

#125416 (Enable GC regions on macOS) should make this go away on macOS in .NET 11, but the segments code path is unchanged in main, so I'd guess other segments builds (standalone clrgc, 32-bit) still have it. Since macOS stays on segments for all of .NET 10, could a fix be considered for .NET 10 servicing?

Thanks!

Activity

  1. added
    untriaged
    on Sep 27, 2026
  2. dotnet-policy-service commented on Sep 27, 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.

  3. self-assigned this
    on Sep 28, 2026
  4. removed
    untriaged
    on Sep 28, 2026
  5. added a commit that references this issue on Oct 1, 2026
    b9bafb9
  6. locked and limited conversation to collaborators on Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions