Avoid Call To Gc

ID

go.avoid_call_to_gc

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Go

Tags

best-practice, reliability

Description

Reports an explicit call to runtime.GC(), which forces a full, stop-the-world garbage collection.

Rationale

A forced collection scans the entire heap while the program is paused, so it usually costs more in throughput and latency than it saves. The Go runtime already schedules collection adaptively. A manual call is typically a misguided attempt to reclaim memory immediately and tends to hide a real leak — a retained reference that should be released — instead of fixing it.

runtime.GC()  // FLAW — let the runtime manage collection

c.GC()        // OK — a method on a local type, not runtime.GC

Remediation

Remove the call and let the runtime manage collection. If memory keeps growing, profile the heap to find and release the references that are being retained, and tune GOGC rather than forcing collection in code.

References