Profiling memory usage of a Go app

net/http/pprof is a powerful package for profiling. This video introduces how to use it to do memory profiling. Let me write down the steps.

go tool pprof --base ~/Downloads/base.heap ~/go/bin/yourapp ~/Downloads/current.heap

You can show the number of objects by using this option -inuse_objects

go tool pprof -inuse_objects --base ~/Downloads/base.heap ~/go/bin/yourapp ~/Downloads/current.heap

6. Now you enter the interactive mode of pprof.

Use top to view the top entries (memory usage).

(pprof) top
13356 of -19412 total ( -69%)
flat flat% sum% cum cum%
10923 -56% -56% 10923 -56% runtime.deferproc.func1
1365 -7% -63% 1365 -7% runtime.malg
910 -4.7% -68% 910 -4.7% math/big.nat.make
158 -0.81% -69% 158 -0.81% crypto/tls.(*Conn).readHandshake
0 -0% -69% 0 -0% bufio.(*Reader).Peek
0 -0% -69% 0 -0% bufio.(*Reader).Read
0 -0% -69% 0 -0% bufio.(*Reader).fill
0 -0% -69% 0 -0% bufio.(*Writer).Write
0 -0% -69% 0 -0% bufio.(*Writer).flush
0 -0% -69% 0 -0% bytes.(*Buffer).ReadFrom

Use web to visualize the graph though the web browser. Actually it generates a .svg file. You need to set your browser as the default application to open .svg file first.

You can view the source code! Use list command for functions shown in top. For example, list runtime.deferproc.func1

(pprof) list runtime.deferproc.func1
Total: -19412
ROUTINE ======================== runtime.deferproc.func1 in /usr/local/go/src/runtime/panic.go
10923 10923 (flat, cum) -56% of Total
. . 67: sp := getcallersp(unsafe.Pointer(&siz))
. . 68: argp := uintptr(unsafe.Pointer(&fn)) + unsafe.Sizeof(fn)
. . 69: callerpc := getcallerpc(unsafe.Pointer(&siz))
. . 70:
. . 71: systemstack(func() {
10923 10923 72: d := newdefer(siz)
. . 73: if d._panic != nil {
. . 74: throw("deferproc: d.panic != nil after newdefer")
. . 75: }
. . 76: d.fn = fn
. . 77: d.pc = callerpc

More resources:

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

https://blog.golang.org/profiling-go-programs

Originally published at vinceyuan.github.io on November 20, 2015.

--

--

iOS/Android/Web developer. UX engineering lead at Grab. Singapore/Hong Kong/Shanghai

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aijin Yuan (Vince)

Aijin Yuan (Vince)

iOS/Android/Web developer. UX engineering lead at Grab. Singapore/Hong Kong/Shanghai