Troubleshooting guide
Goroutine leaks in Go: how to find and fix them
A goroutine that can never finish keeps its stack and everything it references. The count rises until memory runs out.
3 min read10 sectionsUpdated
On this page
// In main.go, on an internal-only port
import _ "net/http/pprof"
go http.ListenAndServe("127.0.0.1:6060", nil)
# Grouped stacks with counts
curl -s 'localhost:6060/debug/pprof/goroutine?debug=1' | head -60
# Compare two points in time
go tool pprof -base goroutine-1.pb.gz goroutine-2.pb.gz
# Interactive view
go tool pprof -http=:8081 'http://localhost:6060/debug/pprof/goroutine'Fixes
Use a buffered channel of size 1 when a worker may outlive its caller, so that its send cannot block forever. Use select with <-ctx.Done() in every blocking operation. Close response bodies using defer resp.Body.Close(). Call ticker.Stop(). Limit concurrency with errgroup and SetLimit. When a leak is found, write a test that reproduces it: start the operation, cancel it, and confirm that the goroutine count returns to where it was. This keeps the same bug from coming back in a later refactor.
The timeout trap
A common leak looks harmless. A function starts a goroutine to do work and send the result on an unbuffered channel, then uses select with a timeout. If the timeout wins, the function returns, and no one receives. The goroutine stays blocked on its send forever, so every timed-out request leaks one. It shows up precisely when a dependency slows down, which turns a brief slowdown into a slow memory leak. Giving the channel a buffer of one fixes it, because the send then always succeeds even with no reader.
Catch it automatically
Export runtime.NumGoroutine() and alert on growth over hours. Add go.uber.org/goleak to tests so that leaks fail the build. Go performance monitoring charts goroutines beside memory and latency.
Related reading
- How to find a Node.js memory leak: Detect a Node.js memory leak from heap metrics, compare heap snapshots to find what retains memory, fix the usual causes and alert before a crash.
- Prometheus high cardinality: how to find and fix it: Find and fix Prometheus high cardinality: use the TSDB status page and promtool, identify runaway labels and drop them with relabeling rules before ingestion.
- Python memory usage: find what uses memory: Measure and reduce Python memory usage: RSS vs allocated objects, tracemalloc snapshots, finding leaks in long-running workers.
- Java memory usage: heap, metaspace and leaks: Understand Java memory usage: heap, metaspace, threads and direct memory, why RSS exceeds -Xmx, how to size for containers.
- MongoDB slow queries: how to find and fix them: Find and fix slow MongoDB queries: use the profiler and explain, spot COLLSCAN and in-memory sorts, and follow the ESR rule when designing compound indexes.
- .NET OutOfMemoryException: causes and how to fix it: Fix .NET System.OutOfMemoryException: use dotnet-counters and dotnet-gcdump, spot Large Object Heap fragmentation and set the right GC limits in containers.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.