GC Assist Triggers
The Go runtime enters a goroutine into GC assist state when that goroutine's allocations would cause the heap to exceed the GC heap goal. This goal is calculated as:
heap_goal = live_heap * (GOGC / 100)
When HeapAlloc > heap_goal, any allocation triggers an assist. The assist work required is determined by the assist ratio:
assist_ratio = heap_before_gc / heap_after_gc
This ratio determines how much marking work each allocating P must perform. Higher allocation rates increase the ratio, causing more aggressive assists.
Confirmed Behavior
The assist mechanism operates as follows:
- Trigger condition: Allocation would exceed heap goal (based on GOGC setting)
- Work calculation: Marking work = assist_ratio * allocation_size
- Execution: Allocation blocks until sufficient marking work is completed
- Non-preemptive: No runtime mechanism to interrupt or bound assist work
Bounding Assist Latency
There is no built-in runtime API to cap assist latency per goroutine without global GOGC adjustment. The available strategies are:
- Reduce GOGC: Lower GOGC (e.g., 50) keeps heap smaller, reducing assist probability
- Pre-allocate: Use sync.Pool or object pooling to reduce allocation frequency
- Batch allocations: Minimize allocations in hot paths to reduce assist triggers
- GODEBUG=gcstoptheworld=1: Disables concurrent GC entirely (not recommended for production)
Verification Steps
Run these commands to diagnose assist behavior in your workload:
# Enable GC tracing to observe assist timing
GODEBUG=gctrace=1 ./your-app
# Monitor heap and GC stats programmatically
runtime.ReadMemStats(&memStats)
fmt.Printf("HeapAlloc: %d, HeapGoal: %d\n", memStats.HeapAlloc, memStats.HeapGoal)
# Use pprof to identify allocation hot paths
go tool pprof -http=:8080 http://localhost:6060/debug/pprof/heap
Missing Diagnostic Detail
To provide a more targeted recommendation, I need to know: What is your current GOGC setting and allocation rate (allocs/sec) during the latency spikes? This will determine whether the issue stems from GOGC tuning or allocation pattern optimization.