Common Causes of Memory Leaks

Goroutines, Slices, Maps, Timers

Identify and fix Go memory leaks caused by blocked goroutines, unbounded slices/maps, and unstopped timers using pprof and proper resource cleanup.

Memory leaks in Go occur when goroutines block indefinitely, slices or maps hold references to large objects, or timers are created but never stopped. Use the go tool pprof command to identify the specific leak source in your binary.

go tool pprof -top http://localhost:6060/debug/pprof/heap

For goroutine leaks, check for channels that are never closed or select statements missing a default case. For slice and map leaks, ensure you are not appending to slices that are never truncated or adding keys to maps that are never deleted. For timer leaks, always call timer.Stop() when the timer is no longer needed, or use time.AfterFunc which handles cleanup automatically.