Use Go tool pprof to analyze memory leaks, CPU consumption

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go provides the PPROF package for code performance monitoring in two locations with packages:

    • Net/http/pprof
    • Runtime/pprof

In fact, net/http/pprof only use the RUNTIME/PPROF package to encapsulate a bit, and exposed on the HTTP port.

Performance monitoring of Web servers using Net/http/pprof

If your go program is a Web server that starts with an HTTP package, you want to see the status of your Web server. This is the time to choose Net/http/pprof.

Import _ "Net/http/pprof"
You can then use http://localhost:port/debug/pprof/directly in your browser to see the status of the current Web service, including CPU usage and memory use. Of course, non-web can also start the web in the following way. In the main method, add
Func main () {go func () {http. Listenandserve ("localhost:6060", Nil)} ()
is to visit this URL once:

CPU Consumption analysis

Application performance monitoring using Runtime/pprof

Key code:

Import "Runtime/pprof" Func Main () {
F, err: = OS. OpenFile ("./tmp/cpu.prof", Os. O_rdwr|os. O_create, 0644)
If err! = Nil {
Log. Fatal (ERR)
}
Defer F.close ()
Pprof. Startcpuprofile (f)
Defer pprof. Stopcpuprofile ()//note, sometimes defer f.close (), defer pprof. Stopcpuprofile () will not execute, this time we will see the prof file is empty, we need to add the following two lines in the place where our code exits, make sure to write the contents of the file. Pprof. Stopcpuprofile () F.close ()}

Analysis of the resulting file:

We can use the Go tool pprof (application) (application's Prof file) method to analyze this prof file.

$ go Tool pprof Huarongdao./tmp/cpu.prof
Entering interactive mode (type ' help ' for commands)
(PPROF)

Some common pprof commands:

Top

By default, the top command outputs a list of local sampling counts in order. We can call this list the local sample count ranking list.

(pprof) Top
2700ms of 3200ms Total (84.38%)
Dropped nodes (cum <= 16ms)
Showing top nodes out of 111 (cum >= 80ms)
Flat flat% sum% cum cum%
670ms 20.94% 20.94% 670ms 20.94% runtime.mach_semaphore_signal
580ms 18.12% 39.06% 590ms 18.44% runtime.cgocall
370ms 11.56% 50.62% 370ms 11.56% runtime.mach_semaphore_wait
360ms 11.25% 61.88% 360ms 11.25% runtime.memmove
210ms 6.56% 68.44% 580ms 18.12% golang.org/x/mobile/gl. (*context). DoWork
120ms 3.75% 72.19% 120ms 3.75% runtime.usleep
110ms 3.44% 75.62% 110ms 3.44% image/png.filterpaeth
100ms 3.12% 78.75% 160ms 5.00% compress/flate. (*decompressor). Huffsym
100ms 3.12% 81.88% 100ms 3.12% image/draw.drawnrgbasrc
80ms 2.50% 84.38% 80ms 2.50% runtime.memclr
(PPROF)

Reference: HTTPS://GITHUB.COM/HYPER-CARROT/GO_COMMAND_TUTORIAL/BLOB/MASTER/0.12.MD

The top command lists the top 10 items by default. But if you follow a number immediately after the top command, the number of items listed will be the same as that number.

Web

Similar to the GV command, Web commands graphically display the profile. But the difference is that the Web command is displayed in a Web browser. If your Web browser is started, it will be displayed very quickly. If you want to change the Web browser you are using, you can set the symbolic link/etc/alternatives/gnome-www-browser or/etc/alternatives/x-www-browser under Linux, or the OS X to change the associated finder for SVG files.

Under Mac, modify the default open mode: Right-click a file you want to work with, press the ALT key (Lion) to appear always open with, then open the whole process, right-click, and then press ALT until you open it.

Resources:

Go Tool pprof
Https://github.com/hyper-carrot/go_command_tutorial/blob/master/0.12.md

Pprof Use of Go
Http://www.cnblogs.com/yjf512/archive/2012/12/27/2835331.html

Profiling Go Programs
Https://blog.golang.org/profiling-go-programs

Memory leak or consumption analysis

Key code

FM, ERR: = OS. OpenFile ("./tmp/mem.out", Os. O_rdwr|os. O_create, 0644)
If err! = Nil {
Log. Fatal (ERR)
}
Pprof. WRITEHEAPPROFILE (FM)
Fm. Close ()

Just execute the above code when we need to generate the memory at that time.

The triggering conditions can be passed through an interface of HTTP, or when exiting, or receiving a particular signal, the logic needs to be implemented by itself.

Analysis method, consistent with the analysis method on the CPU.

Resources:

[Golang] Memory growing Bytes.makeslice
http://studygolang.com/articles/2763

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.