Using pprof to examine the heap and memory usage in Golang Programs

I had some trouble getting the info I needed to setup pprof in my program and the steps to get actionable data out of pprof, so here is my attempt to provide the minimum steps needed to use pprof.

Instrument your code

import (     
   _ "net/http/pprof"         
    ) 
func main() {     
   go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }() 
//Your program 
}

Make sure you have the above in your main.go file. This sets up a webserver that provides pprof data from

localhost:6060/debug/pprof/

Heap Memory Usage

In your shell run: go tool pprof http://localhost:6060/debug/pprof/heap This will open a cli program, enter top into the prompt.

go tool proof http://localhost:6060/debug/pprof/heap

This gives you the top 10 nodes of memory usage. It will truncate the results if most of the memory is in the top 3 nodes.

CPU time

To sample 30 seconds of cpu time, with 50ms of time ‘sampled’. go tool pprof http://localhost:6060/debug/pprof/profile

go tool proof http://localhost:6060/debug/pprof/profile

    pprof Godoc:   https://golang.org/pkg/net/http/pprof/

If you like my writing, please buy my book on Amazon.
The Sledgeworx Guide to Getting into Software