02 / 05

What are common sources of memory leaks in Go services?

Difficulty: 7/10
goroutine leaks, slice retention, cgo/resource handling

The most common memory leaks in Go are goroutine leaks, unbounded caches, unclosed HTTP response bodies, and keeping references to large backing arrays via sub-slices.

Common memory leak sources
  1. 1

    Goroutine leaks: blocked goroutines hold their stack and all referenced heap objects alive forever

  2. 2

    Unbounded global maps or caches that only grow — add a size limit or TTL eviction

  3. 3

    time.After inside loops: each call creates a new channel and timer that leaks until it fires

  4. 4

    Unclosed HTTP response bodies: resp.Body.Close() must be called even when not reading the body

  5. 5

    Sub-slice keeping large array alive: a small slice of a large array prevents the whole array from being GC'd

  6. 6

    CGo retained pointers: C code holding Go pointers prevents GC collection

Common leak examples and fixes

Scenario Questions

0-2 years experience

  1. 1You added an HTTP handler that reads the request body into a byte slice but never lets the slice go out of scope. What effect could that have on memory over time?
  2. 2If you launch a new goroutine for each incoming request and never signal it to stop, how might that show up in the service's memory usage?
  3. 3What happens if you defer a file.Close() inside a tight loop that processes thousands of files?

2-5 years experience

  1. 1After deploying a feature that uses sync.Pool, the service's memory keeps growing. Walk me through how you'd investigate whether the pool is leaking.
  2. 2During a load test you see many large slices retaining old data. Explain how slice capacity can cause a leak and how you'd fix it.
  3. 3A goroutine got stuck waiting on a channel forever. How would that manifest as a memory leak and what steps would you take to prevent it?

5-8 years experience

  1. 1Design a monitoring strategy for a high‑traffic Go microservice to detect and alert on memory leaks caused by goroutine or resource leaks. Which metrics and tools would you include?
  2. 2When migrating a monolith to multiple Go services, you need to ensure that legacy C libraries used via cgo don’t leak memory. How would you audit and mitigate such leaks at scale?
  3. 3Explain the trade‑offs of using runtime.SetFinalizer versus explicit Close methods for managing external resources in a large Go codebase.

8+ years experience

  1. 1Our organization runs dozens of Go services and sees intermittent OOM crashes due to subtle leaks. Propose an organization‑wide policy and tooling pipeline to catch leaks early, covering code review, static analysis, CI, and production observability.
  2. 2We plan to refactor a critical service to use a custom memory allocator for performance gains. How would you evaluate the risk of introducing leaks, and what safeguards would you put in place across teams?

Follow-up Questions

  • What tooling would you use to confirm the leak is resolved?
  • How do you decide between adding explicit Close methods versus relying on finalizers?
  • Can you share an example where a leak caused an OOM and how you mitigated it?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.