Go Memory Issues in Production
Go doesn't throw "MemoryError" like Python — instead, the runtime panics with runtime: out of memory or the OS kills the process. Here's how to diagnose and fix it.
Enable pprof
Add the profiling endpoint to your service:
import _ "net/http/pprof"
func main() {
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()
// ... your main server
}Then capture a heap profile:
go tool pprof http://localhost:6060/debug/pprof/heap
(pprof) top 10Goroutine Leaks
The most common Go memory issue — goroutines that never exit:
// LEAK: goroutine blocked forever if ctx is never cancelled
func startWorker(ch chan Data) {
go func() {
for data := range ch {
process(data)
}
}()
// If ch is never closed, this goroutine lives forever
}
// FIX: use context for cancellation
func startWorker(ctx context.Context, ch chan Data) {
go func() {
for {
select {
case data, ok := <-ch:
if !ok { return }
process(data)
case <-ctx.Done():
return
}
}
}()
}Large Allocations
// BAD — allocates entire file in memory
data, _ := os.ReadFile("huge.csv")
// GOOD — stream it
file, _ := os.Open("huge.csv")
scanner := bufio.NewScanner(file)
for scanner.Scan() {
processLine(scanner.Text())
}Check goroutine counts in production:
curl http://localhost:6060/debug/pprof/goroutine?debug=1 | head -5Bugsly captures Go panics including runtime: out of memory with goroutine dumps, making it easy to see which goroutines were running when memory ran out.
Try Bugsly Free
AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.
Get Started FreeRelated Articles
Next.js Error Tracking: The Complete Guide
A complete guide to setting up error tracking in Next.js applications, covering App Router, Server Components, API routes, and middleware.
Read moreHow to Fix DNS Resolution Error in R
Learn how to fix the DNS Resolution Error in R. Step-by-step guide with code examples.
Read moreHow to Fix DatabaseError in Vue
Learn how to fix the DatabaseError in Vue. Step-by-step guide with code examples.
Read moreHow to Fix Rangeerror in Go When Deploying
Learn how to diagnose and fix the rangeerror in Go when deploying. Includes code examples and prevention tips.
Read more