Golang & Concurrency (50 Qs)•Part 4: Production Compilation, Tooling & Senior Engineering (39 - 50)
Memory Leak Patterns in Go: Subslice Retentions and Finalizers.
senior LevelMemory Management
In Simple Words:
- Subslice Leak: Slicing a tiny 5-byte header from a 100MB buffer keeps the entire 100MB array pinned in memory! Fix:
copy()the small slice. - Unclosed Response Bodies: Forgetting
resp.Body.Close()leaks the underlying TCP connection and socket buffers. - Goroutine Leaks: Blocked channel reads keep all variables referenced in that stack frame alive.