Skip to content

Getting started with Go CPU and memory profiling

The Go ecosystem provides a very easy way to profile your applications.

I’ll explain profiling using a package by Dave Cheney which makes programs very easy to debug, by adding a one-liner to our main().

All you need to get started is follow these X easy steps.

CPU Profiling

Step #1: download github.com/pkg/profile

Can’t be simpler than running

go get github.com/pkg/profile

and you’re done.

Step #2: add profiling into the main() function of your command

package main

import (
    //...
	"github.com/pkg/profile"
)

func main() {
	// CPU profiling by default
	defer profile.Start().Stop()

    //...
}

Step #3: build and run your program

This will generate a *.pprof file in a temp folder, and tell you where it’s located (will be needed later)

2017/08/03 14:26:28 profile: cpu profiling enabled, /var/...../cpu.pprof

Step #4: install graphviz if you don’t have it installed yet

This is used to generate the graph on a pdf. On a Mac, it’s a simple brew install graphviz. Refer to https://www.graphviz.org for other platforms.

Step #5: run go tool pprof

Pass your binary location, and the location of the cpu.pprof file as returned when running your program.

You can generate the analysis in various formats. The PDF one is pretty amazing:

go tool pprof --pdf ~/go/bin/yourbinary /var/path/to/cpu.pprof > file.pdf

You can generate other kind of visualizations as well, e.g. txt:

go tool pprof --txt ~/go/bin/yourbinary /var/path/to/cpu.pprof > file.txt

Memory profiling

Memory profiling is essentially the same as CPU profiling, but instead of using the default configuration for profile.Start(), we pass a profile.MemProfile flag:

defer profile.Start(profile.MemProfile).Stop()

thus the code becomes

package main

import (
    //...
	"github.com/pkg/profile"
)

func main() {
	// Memory profiling
	defer profile.Start(profile.MemProfile).Stop()

    //...
}

and when running the program, it will generate a mem.pprof file instead of cpu.pprof.

Read more about profiling Go apps

This is just a start. Read more at:

→ Get my Go Handbook

I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter

JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list