discrepancy between goal heap size and NextGC

122 views
Skip to first unread message

Michael Mitchell

unread,
Jan 29, 2024, 7:12:53 AM1/29/24
to golang-nuts
In the twelfth garbage collection of a short program I ran, GC trace reports the goal heap size as 735 MB (see below for gc11, gc12 and gc13 from GC trace)  I also checked runtime Memstats (see attached image), and it says the nextGC target after 12 GCs have run is 838103616. 

Should goal Heap Size of GC Trace and NextGC of runtime.Memstats not be the same? 


gc 11 @22.009s 0%: 0.004+12+0.034 ms clock, 0.019+0.13/11/0.94+0.13 ms cpu, 562->562->367 MB, 964 MB goal, 4 P

gc 12 @32.925s 0%: 0.003+16+0.024 ms clock, 0.014+0/3.7/13+0.099 ms cpu, 410->410->399 MB, 735 MB goal, 4 P (forced)

gc 13 @32.964s 0%: 0.019+21+0.036 ms clock, 0.079+0/21/0.56+0.14 ms cpu, 400->400->399 MB, 799 MB goal, 4 P (forced)

nextgc.png

Michael Mitchell

unread,
Jan 30, 2024, 10:48:26 AM1/30/24
to golang-nuts
Might it have something to do with the comment in mgc.go about how the increment of memstats.numgc differs from the increment of GC cycles for gctrace? memstats.numgc seems to get incremented earlier (after mark termination) whereas gctrace waits until the sweep is done.

// cycles is the number of completed GC cycles, where a GC
// cycle is sweep termination, mark, mark termination, and
// sweep. This differs from memstats.numgc, which is
// incremented at mark termination.
cycles uint32

Michael Knyszek

unread,
Jan 30, 2024, 11:51:51 PM1/30/24
to golang-nuts
838103616 bytes is 799 MiB (Mebibytes, the power-of-two-based prefix), which matches GC 13. (Note that the computation for the heap goal just divides by 1<<20, or 1 MiB.)

The discrepancy in GC count is probably due to the fact that the GC count value reported by the GC trace is *after* the increment in mark termination, but it reports the previous cycle's heap goal. This makes sense because the GC trace isn't reporting a count of completed GCs, but reporting on the Nth GC, which just finished. (This is why the GC trace count starts at 1 instead of 0.)

More concretely, you'll observe that 799 MiB heap goal after 12 GCs have completed (your read of numgc), but you'll be reading that heap goal *during* the 13th GC cycle, which is what the GC trace is reporting on.
Reply all
Reply to author
Forward
0 new messages