Heap and Allocation Profiling
Finding memory hotspots with heap profiles
Heap and Allocation Profiling is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why heap profiling?
Excessive memory allocations slow Go programs through GC pressure. Heap profiling shows which code paths allocate the most memory so you can reduce allocations.
Capturing a heap profile
Via HTTP endpoint (requires net/http/pprof):
go tool pprof http://localhost:6060/debug/pprof/heapFile-based heap profile
Write a heap profile at any point using runtime/pprof:
f, _ := os.Create("mem.prof")
pprof.WriteHeapProfile(f)
f.Close()Inuse vs alloc
The heap profile has two views: inuse_objects/inuse_space (currently live) and alloc_objects/alloc_space (total since start). Use alloc_space to find hot allocation paths.
go tool pprof -alloc_space http://localhost:6060/debug/pprof/heapgo test -memprofile
Capture memory profile during benchmarks:
go test -memprofile mem.prof -benchmem -bench=BenchmarkProcess ./...MemStats
Read runtime memory stats programmatically for live monitoring:
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
fmt.Printf("Alloc: %v MiB\n", ms.Alloc/1024/1024)
fmt.Printf("NumGC: %v\n", ms.NumGC)Escape analysis
Variables that escape to the heap cause allocations. Run go build -gcflags=-m to see which variables escape and why.
go build -gcflags="-m -m" ./...
// main.go:12:15: &User{...} escapes to heapReducing allocations: pre-allocate
Pre-allocate slices with make([]T, 0, n) when the final length is known, avoiding repeated reallocation as the slice grows.
results := make([]Result, 0, len(input))sync.Pool for reuse
Pool reusable objects (buffers, scratch slices) with sync.Pool to amortise allocation cost:
var pool = sync.Pool{
New: func() any { return new(bytes.Buffer) },
}
buf := pool.Get().(*bytes.Buffer)
buf.Reset()
// use buf...
pool.Put(buf)Value vs pointer receivers
Large structs passed by value are copied on each call. Pass by pointer to avoid copying, but be aware that pointers may cause the value to escape to the heap.
String vs []byte
Converting between string and []byte allocates. Use []byte throughout hot paths and convert to string only at the boundary where a string is required.
Quick Check
What does -alloc_space show in a pprof heap profile?
Recap: Heap Profiling
Key points:
- -alloc_space shows total allocations; inuse_space shows live
- go build -gcflags=-m shows escape analysis
- Pre-allocate slices; use sync.Pool for frequent reuse
- runtime.ReadMemStats for programmatic monitoring
Frequently asked questions
Is the “Heap and Allocation Profiling” lesson free?
Yes — the full text of “Heap and Allocation Profiling” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Heap and Allocation Profiling”?
Finding memory hotspots with heap profiles You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Heap and Allocation Profiling” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing Benchmarks with testing.B
- CPU Profiling with pprof
- Heap and Allocation Profiling
- Execution Tracing