Go Academy · Lesson

Heap and Allocation Profiling

Finding memory hotspots with heap profiles

Lesson 3 of 413 steps

Heap and Allocation Profiling is a free Go Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

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/heap

File-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/heap

go 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 heap

Reducing 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
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Heap and Allocation Profiling” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Heap and Allocation Profiling”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

  1. Writing Benchmarks with testing.B
  2. CPU Profiling with pprof
  3. Heap and Allocation Profiling
  4. Execution Tracing
← Back to Go Academy