Golang pprof live and in action

arjun dhar
3 min readSep 15, 2021

--

Go pprof Live and in Action

pprof is a Go profiling tool.

There are a lot of Blogs on Go — pprof. Unfortunately, the code samples are pretty badly written or dont work off the bat. This code is a live working example of pprof in action.

You can find the code @ https://github.com/ArjunDhar/scribbles/tree/master/go-pprof-working

It show cases 2 modes.

  1. Via net/http using the Web server routes in net/http/pprof.Init() -- WORKS
  2. Using github.com/pkg/profile; this creates .out/mem.pprof file but it remains @ 0KB -- FAIL

Usage

Build & Run

go build -o sample.exe main.go
go run main.go

Access debug info

http://localhost:8082/debug/pprof/

Produce Memory Graph

Install Graphviz @ https://graphviz.org/download/ and ensure part of System PATH

go tool pprof -png http://localhost:8082/debug/pprof/heap > heap.png

Code notes

If you increase the ticker time to call clear(); you can crash the VM @ some point.

Sample Output

Following are generated by go tool command above

Sample CLI Out corresponding to above screens

D:\profiling-pprof\sample>go run main.go
[main]
[Profiler started]
2021/09/15 18:14:31 profile: memory profiling enabled (rate 4096), out\mem.pprof
[f()]
[net/http] WebServer started @ http://localhost:8082/debug/pprof/
[f->go] Timer fired : Clear slice[568187513] @ 2021-09-15 18:15:31.1290589 +0530 IST m=+60.008981301
[clear]
[pre-clear] len= 568294570
[post-clear] len= 0
[f->go] Timer fired : Clear slice[294187520] @ 2021-09-15 18:16:31.1291916 +0530 IST m=+120.009114001
[clear]
[pre-clear] len= 294187952
[post-clear] len= 0
[f->go] Timer fired : Clear slice[294187520] @ 2021-09-15 18:17:31.1301379 +0530 IST m=+180.010060301
[clear]
[pre-clear] len= 294188396
[post-clear] len= 0
[f->go] Timer fired : Clear slice[573005841] @ 2021-09-15 18:18:31.1291326 +0530 IST m=+240.009055001
[clear]
[pre-clear] len= 573227942
[post-clear] len= 0
[f->go] Timer fired : Clear slice[235350016] @ 2021-09-15 18:19:31.1301378 +0530 IST m=+300.010060201
[clear]
[pre-clear] len= 235351899
[post-clear] len= 0
[f->go] Timer fired : Clear slice[459668480] @ 2021-09-15 18:20:31.1298302 +0530 IST m=+360.009752601
[clear]
[pre-clear] len= 459669033
[post-clear] len= 0

Packages

References

These articles were the initial source, but they all have their own holes which are put together in my code

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

arjun dhar
arjun dhar

Written by arjun dhar

Software development enthusiast since I was 8 yrs old. Love communicating on anything regarding innovation, community development … ∞

No responses yet

Write a response