You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I audited the code and the internal GC total memory calculations are done per-heap using size_t instead of ssize_t/int64, so it makes sense that if we somehow got a very large calculated size, it would be negative. Getting a size that big should be impossible though, so that indicates some sort of rare bug in the underlying calculations. I couldn't spot any obvious cause, so it would be great if someone could offer more details on the reproduction scenario, even if you can't share an application with us.
i.e.:
How big are your heaps when this isn't failing?
Is your application heavily multithreaded?
Is your application under load when you call this?
In my case, this problem occurs in a small terminal app based on Terminal.Gui. Its typical memory footprint is about 1-2 MB. The app has several threads: Terminal.Gui threads + some background tasks in the user code for handling long-running operations, but it does not involve heavy multithreading. The app performs very basic tasks with a small number of allocations.
usingSystem.Threading;usingSystem.Collections.Generic;boolRunning=true;// See https://aka.ms/new-console-template for more informationConsole.WriteLine("Running...");varthreads=newList<Thread>();for(inti=0;i<8;i++){varthread=newThread(ThreadMain);thread.Start();threads.Add(thread);}while(true){vartm=GC.GetTotalMemory(false);if(tm<0)thrownewException($"Got total memory of {tm}");Thread.Yield();}voidThreadMain(){while(Running){vars="x";for(inti=0;i<1024;i++)s+=i.ToString();Thread.Yield();}}
Stepping through one iteration at a time shows that it fails after only a couple dozen steps. It looks like the result going negative might correlate with a GC, but Diagnostic Tools doesn't show a marker for the GC.
I tried calling GetGCMemoryInfo before each iteration and it appears multiple GCs have happened so this might be some sort of race condition with the GC.
EDIT: Further testing shows that this regressed sometime between NET6.0 and NET7.0. It is very hard to repro on 6 (it does repro eventually) but very easy in 7, 8 and 9.
Calling GetTotalMemory again will return a negative value even if you sleep first, so what's happening is that the GC heap is getting into a bad state of some sort. However, if you keep allocating eventually the GC heap will "recover" and become positive again. For example if you leave the threads going:
Got total memory of -3665464
Got total memory of 6809168
Got total memory of 1422672 after sleeping 10ms
Got total memory of 486960 after GetTotalMemory(true)
However if you change the repro test to be single threaded:
Running...
Got total memory of -525416
Got total memory of -517192
Got total memory of -517192 after sleeping 10ms
Got total memory of 421176 after GetTotalMemory(true)
You can see that the forced GC from GetTotalMemory(true) corrected the state of the heap. Between the two GetTotalMemory calls the negative value moved towards zero slightly, probably from some sort of allocation that happened during the writeline call.
Here is the single threaded repro. It takes longer to reproduce but does work:
usingSystem.Threading;usingSystem.Collections.Generic;Console.WriteLine("Running...");while(true){vars="x";for(inti=0;i<1024;i++)s+=i.ToString();Thread.Yield();vartm=GC.GetTotalMemory(false);if(tm<0){Console.WriteLine($"Got total memory of {tm}");tm=GC.GetTotalMemory(false);Console.WriteLine($"Got total memory of {tm}");tm=GC.GetTotalMemory(true);Console.WriteLine($"Got total memory of {tm} after GetTotalMemory(true)");return;}}
This does not reproduce using a debug or release mode build of corerun from latest main. If any of the reporters can test using one of the .NET 10 preview builds and see if it reproduces for you, I'd really appreciate it! It may be fixed already.
EDIT: I was mistaken. The multithreaded repro does fail on net10, only the single-threaded one is fixed.
Activity
dotnet-policy-service commentedon Aug 20, 2024
Tagging subscribers to this area: @dotnet/gc
See info in area-owners.md if you want to be subscribed.
firerain-fd commentedon Jun 27, 2025
I have the same problem.
kg commentedon Jul 17, 2025
Is this while using NativeAOT or regular execution mode?
kg commentedon Jul 17, 2025
I audited the code and the internal GC total memory calculations are done per-heap using size_t instead of ssize_t/int64, so it makes sense that if we somehow got a very large calculated size, it would be negative. Getting a size that big should be impossible though, so that indicates some sort of rare bug in the underlying calculations. I couldn't spot any obvious cause, so it would be great if someone could offer more details on the reproduction scenario, even if you can't share an application with us.
i.e.:
AndreyAkinshin commentedon Jul 21, 2025
In my case, this problem occurs in a small terminal app based on Terminal.Gui. Its typical memory footprint is about 1-2 MB. The app has several threads: Terminal.Gui threads + some background tasks in the user code for handling long-running operations, but it does not involve heavy multithreading. The app performs very basic tasks with a small number of allocations.
kg commentedon Jul 21, 2025
Test case I constructed fails instantly on NET9.
kg commentedon Jul 21, 2025
Stepping through one iteration at a time shows that it fails after only a couple dozen steps. It looks like the result going negative might correlate with a GC, but Diagnostic Tools doesn't show a marker for the GC.
I tried calling GetGCMemoryInfo before each iteration and it appears multiple GCs have happened so this might be some sort of race condition with the GC.

EDIT: Further testing shows that this regressed sometime between NET6.0 and NET7.0. It is very hard to repro on 6 (it does repro eventually) but very easy in 7, 8 and 9.
kg commentedon Jul 22, 2025
Calling GetTotalMemory again will return a negative value even if you sleep first, so what's happening is that the GC heap is getting into a bad state of some sort. However, if you keep allocating eventually the GC heap will "recover" and become positive again. For example if you leave the threads going:
However if you change the repro test to be single threaded:
You can see that the forced GC from
GetTotalMemory(true)corrected the state of the heap. Between the two GetTotalMemory calls the negative value moved towards zero slightly, probably from some sort of allocation that happened during the writeline call.Here is the single threaded repro. It takes longer to reproduce but does work:
kg commentedon Jul 22, 2025
This does not reproduce using a debug or release mode build of corerun from latest main. If any of the reporters can test using one of the .NET 10 preview builds and see if it reproduces for you, I'd really appreciate it! It may be fixed already.
EDIT: I was mistaken. The multithreaded repro does fail on net10, only the single-threaded one is fixed.
mrsharm commentedon Aug 7, 2025
Thank you, @kg for the repro and thanks @AndreyAkinshin for reporting this.
Here is what I have found:
GCHeap::ApproxTotalBytesInUseis where the total memory is computed and the reason why we are observing negative total size is because the gen0 fragmentation size is much higher than the gen0 size; for gen0, the total size is estimated as gen0 size - the fragmentation on gen0 to remove the bytes not in use (https://github.com/dotnet/runtime/blob/d104e3e236dcd069ba56fffe2a10ba9d6cc647c9/src/coreclr/gc/gc.cpp#L51800)The printed lines from one such run from the multithreaded repro above before the failure is:
Looks like this code was introduced around the time we enabled regions: https://github.com/dotnet/runtime/pull/59283/files CC: @mangod9 -- the real question is why the gen0 fragmentation is that high; in this computation it's based on the sum of the free list space and free object space - both of which are substantially higher than the gen0 size. This is a regions specific issue btw - running this with segments is fine.
Published a branch with these printfs: https://github.com/mrsharm/runtime/tree/musharm/Gen0FragPrint
Fix MemoryUsed metric failing due to .NET runtime bug
Fix MemoryUsed metric failing due to .NET runtime bug
Fix GC.GetTotalMemory returning negative under regions GC (#130888)