Analyze stop-the-world phases and pacing
Analyze stop-the-world phases and pacing
In Go 1.23 the collector briefly stops application execution at phase transitions, including mark setup and mark termination. Most marking and sweeping can overlap application work. The pacer estimates how early to start marking based on allocation and scan work so the heap stays near its target. A low pause duration does not imply zero GC cost: concurrent workers consume CPU and assists can slow allocation. GC cycles and pauses are different counters.
package main
import ( "fmt" "runtime")
func main() { var a, b runtime.MemStats runtime.ReadMemStats(&a) runtime.GC() runtime.ReadMemStats(&b) fmt.Println(b.NumGC > a.NumGC, b.PauseTotalNs >= a.PauseTotalNs)}In this isolated process GC blocks until a requested collection finishes. The cumulative pause duration is nondecreasing. Do not divide a single pause by request count or equate runtime.GC duration to the stop-the-world time. For a local diagnostic run set GODEBUG=gctrace=1 on the compiled binary; the trace format can change across releases.
Expected output:
true trueRead the current heap goal
package main
import ( "fmt" "runtime/metrics")
func main() { s := []metrics.Sample{{Name: "/gc/heap/goal:bytes"}} metrics.Read(s) fmt.Println(s[0].Value.Kind() == metrics.KindUint64, s[0].Value.Uint64() > 0)}The goal is the heap size targeted by the collector for the end of the current cycle. The trigger occurs earlier, leaving space for allocations during marking. A goal is not a hard memory cap; runtime state, minimum goals and the memory limit affect it. Measure the live heap and allocation rate alongside the goal.
Expected output:
true trueTry it locally
Use two metric snapshots of total GC cycles around two explicit calls to runtime.GC; assert an increase of at least two, not an exact delta.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Garbage collection 03: Analyze stop-the-world phases and pacing
Check the lesson objective before moving on.
Which activity normally overlaps application work in Go 1.23?
Why can requests slow even with short global pauses?