Leaks In Goroutine And Debug Resource

Tram Ho

Simple Example

  • in the doWork func with the parameter being a chan string .
  • func will leak because string channel is a nil value. This means this goroutine does not clear resources, does not stop func.
  • The example above may be extremely short, but in reality the func will last for a lifetime and affect the program’s resources.

Debug Resource

Install packages

  • install package: apt-get install graphviz gv (debian) or brew install graphviz (mac)
  • install pprof: go get -u github.com/google/pprof
  • import pprof: import _ "net/http/pprof"
  • add server: pprof

source

  • in func main, call 3 leak() functions -> in lifetime there will be 3 leaked goroutines
  • start server with port 1234
  • after starting the server, open another terminal go tool pprof http://localhost:1234/debug/pprof/goroutine and enter png
  • there will be Generating report in profile001.png
  • The result in the picture using pprof will have 3 leaks.

Video https://www.youtube.com/watch?v=e1Aa4d90nzk

Source https://github.com/ducnpdev/open-dev/blob/master/concurrency/leak.go

Share the news now

Source : Viblo