{"id":974,"date":"2025-05-07T19:54:28","date_gmt":"2025-05-07T11:54:28","guid":{"rendered":"https:\/\/www.ddosidc.com\/wp\/?p=974"},"modified":"2025-05-07T19:54:28","modified_gmt":"2025-05-07T11:54:28","slug":"golang-%e5%86%85%e5%ad%98%e4%b8%80%e7%9b%b4%e6%b6%a8-pprof-%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5%e4%b8%8e%e4%bc%98%e5%8c%96%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"https:\/\/www.ddosidc.com\/wp\/974.html","title":{"rendered":"Golang \u5185\u5b58\u4e00\u76f4\u6da8 pprof \u7684\u6700\u4f73\u5b9e\u8df5\u4e0e\u4f18\u5316\u7b56\u7565"},"content":{"rendered":"<p><h2>\u89c2\u5bdf\u548c\u5206\u6790 Go \u7a0b\u5e8f\u5185\u5b58\u589e\u957f\u95ee\u9898<\/h2>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.ddosidc.com\/wp\/wp-content\/uploads\/2025\/05\/5UFbV.jpg\" alt=\"Golang \u5185\u5b58\u4e00\u76f4\u6da8 pprof \u7684\u6700\u4f73\u5b9e\u8df5\u4e0e\u4f18\u5316\u7b56\u7565\" title=\"Golang \u5185\u5b58\u4e00\u76f4\u6da8 pprof \u7684\u6700\u4f73\u5b9e\u8df5\u4e0e\u4f18\u5316\u7b56\u7565\"><\/p>\n<p><h3>\u5f15\u8a00<\/h3>\n<\/p>\n<p>\u5728\u5f00\u53d1 Golang \u5e94\u7528\u65f6\uff0c\u5185\u5b58\u4f7f\u7528\u91cf\u7684\u4e0d\u65ad\u589e\u52a0\u901a\u5e38\u662f\u4e00\u4e2a\u8b66\u793a\u4fe1\u53f7\uff0c\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u6216\u7cfb\u7edf\u5d29\u6e83\u3002\u4f7f\u7528 Go \u7684\u5185\u7f6e\u6027\u80fd\u5206\u6790\u5de5\u5177 pprof\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u65f6\u76d1\u63a7\u548c\u5206\u6790\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 pprof \u6765\u68c0\u6d4b\u548c\u4f18\u5316 Go \u5e94\u7528\u4e2d\u7684\u5185\u5b58\u95ee\u9898\uff0c\u5305\u62ec\u8be6\u7ec6\u7684\u64cd\u4f5c\u6b65\u9aa4\u548c\u5b9e\u7528\u6280\u5de7\u3002<\/p>\n<p><h2>\u51c6\u5907\u5de5\u4f5c<\/h2>\n<\/p>\n<p><h3>\u5b89\u88c5 Golang<\/h3>\n<\/p>\n<p>\u786e\u4fdd\u4f60\u7684\u5f00\u53d1\u73af\u5883\u4e2d\u5df2\u7ecf\u5b89\u88c5\u4e86 Golang\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5\u7248\u672c\uff1a<\/p>\n<p><pre><code>go version<\/code><\/pre>\n<\/p>\n<p>\u5982\u679c\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u8bbf\u95ee <strong>https:\/\/golang.org\/dl\/<\/strong> \u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u3002<\/p>\n<p><h3>\u5b89\u88c5 pprof \u5de5\u5177<\/h3>\n<\/p>\n<p>pprof \u5de5\u5177\u662f Go \u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u901a\u5e38\u5728\u5b89\u88c5 Go \u7684\u540c\u65f6\u4f1a\u81ea\u52a8\u5b89\u88c5\u3002\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u547d\u4ee4\u68c0\u67e5 pprof \u662f\u5426\u53ef\u7528\uff1a<\/p>\n<p><pre><code>go tool pprof --version<\/code><\/pre>\n<\/p>\n<p>\u5982\u679c\u663e\u793a\u5de5\u5177\u7248\u672c\uff0c\u5219\u8bbe\u5907\u5df2\u6210\u529f\u5b89\u88c5\u3002<\/p>\n<p><h2>\u5f00\u542f pprof \u670d\u52a1<\/h2>\n<\/p>\n<p>\u5728\u4f60\u7684 Go \u7a0b\u5e8f\u4e2d\u5f15\u5165 pprof \u5305\uff0c\u5e76\u542f\u52a8\u4e00\u4e2a HTTP \u670d\u52a1\u5668\uff0c\u5c06\u5176\u4e0e pprof \u7ed3\u5408\u4f7f\u7528\uff1a<\/p>\n<p><pre><code>package main<\/p>\r\n\r\n<p>import (<\/p>\r\n<p>\t\"net\/http\"<\/p>\r\n<p>\t_ \"net\/http\/pprof\" \/\/ \u5f15\u5165 pprof \u652f\u6301<\/p>\r\n<p>)<\/p>\r\n\r\n<p>func main() {<\/p>\r\n<p>\tgo func() {<\/p>\r\n<p>\t\tlog.Println(http.ListenAndServe(\"localhost:6060\", nil)) \/\/ \u542f\u52a8 pprof server<\/p>\r\n<p>\t}()<\/p>\r\n\r\n<p>\t\/\/ \u5176\u4ed6\u903b\u8f91<\/p>\r\n<p>\tselect {} \/\/ \u963b\u585e\u4e3b goroutine<\/p>\r\n<p>}<\/code><\/pre>\n<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5728\u4f60\u7684 Go \u5e94\u7528\u4e2d\u542f\u52a8 pprof HTTP \u670d\u52a1\u5668\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7 <strong>localhost:6060<\/strong> \u8bbf\u95ee pprof \u63d0\u4f9b\u7684\u9875\u9762\u3002<\/p>\n<p><h2>\u6536\u96c6\u5185\u5b58\u6570\u636e<\/h2>\n<\/p>\n<p><h3>\u8fd0\u884c\u5e94\u7528\u5e76\u6536\u96c6\u6570\u636e<\/h3>\n<\/p>\n<p>\u5728\u6d4b\u8bd5\u9636\u6bb5\uff0c\u8fd0\u884c\u4f60\u7684 Go \u5e94\u7528\u3002\u8fd9\u65f6\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u8bbf\u95ee <strong>http:\/\/localhost:6060\/debug\/pprof\/<\/strong> \u67e5\u770b\u53ef\u7528\u6027\u80fd\u5206\u6790\u6570\u636e\u3002<\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5806\u5185\u5b58\u4fe1\u606f\u751f\u6210\u5230\u6587\u4ef6\u4e2d\uff1a<\/p>\n<p><pre><code>go tool pprof http:\/\/localhost:6060\/debug\/pprof\/heap<\/code><\/pre>\n<\/p>\n<p>\u8be5\u547d\u4ee4\u5c06\u4ece pprof HTTP \u670d\u52a1\u5668\u83b7\u53d6\u5806\u6808\u8ddf\u8e2a\u6570\u636e\u5e76\u52a0\u8f7d\u5230 pprof \u5de5\u5177\u4e2d\u3002<\/p>\n<p><h3>\u4f7f\u7528 pprof \u8fdb\u884c\u4ea4\u4e92\u5206\u6790<\/h3>\n<\/p>\n<p>\u5728\u547d\u4ee4\u884c\u4e2d\u83b7\u5f97 pprof \u5b9e\u4f8b\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6307\u4ee4\u6765\u67e5\u770b\u548c\u5206\u6790\u5185\u5b58\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<p><pre><code>(pprof) top<\/code><\/pre>\n<\/p>\n<p>\u8fd9\u5c06\u663e\u793a\u5185\u5b58\u4f7f\u7528\u6700\u591a\u7684\u51e0\u4e2a\u51fd\u6570\uff0c\u901a\u5e38\u662f\u4f60\u9700\u8981\u5173\u6ce8\u7684\u91cd\u70b9\u3002<\/p>\n<p>\u4f7f\u7528 <strong>web<\/strong> \u547d\u4ee4\u751f\u6210\u53ef\u89c6\u5316\u56fe\u8868\uff1a<\/p>\n<p><pre><code>(pprof) web<\/code><\/pre>\n<\/p>\n<p>\u8fd9\u5c06\u4f7f\u7528 Graphviz \u751f\u6210\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00\u56fe\u8868\u3002<\/p>\n<p><h2>\u5206\u6790\u5185\u5b58\u6cc4\u6f0f<\/h2>\n<\/p>\n<p><h3>\u8bc6\u522b\u6cc4\u6f0f<\/h3>\n<\/p>\n<p>\u5728\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u7559\u610f\u90a3\u4e9b\u5185\u5b58\u5360\u7528\u6301\u7eed\u589e\u957f\u7684\u90e8\u5206\uff0c\u5c24\u5176\u662f\u957f\u5468\u671f\u8fd0\u884c\u7684\u670d\u52a1\u3002\u901a\u8fc7\u89c2\u5bdf\u5185\u5b58\u5360\u7528\u7684\u53d8\u5316\u8d8b\u52bf\uff0c\u8bc6\u522b\u51fa\u9ad8\u5185\u5b58\u5360\u7528\u7684\u51fd\u6570\u3002<\/p>\n<p>\u5728\u5206\u6790\u4e2d\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u67e5\u770b\u7279\u5b9a\u51fd\u6570\u7684\u5206\u914d\u60c5\u51b5\uff1a<\/p>\n<p><pre><code>(pprof) list [function_name]<\/code><\/pre>\n<\/p>\n<p>\u6b64\u547d\u4ee4\u5c06\u5c55\u793a\u6307\u5b9a\u51fd\u6570\u7684\u6e90\u4ee3\u7801\u53ca\u5176\u5185\u5b58\u5206\u914d\u60c5\u51b5\uff0c\u5e2e\u52a9\u786e\u8ba4\u662f\u5426\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p><h3>\u9010\u6b65\u8c03\u8bd5<\/h3>\n<\/p>\n<p>\u5982\u679c\u53d1\u73b0\u5185\u5b58\u6cc4\u6f0f\uff0c\u5e94\u8be5\u4f7f\u7528\u9010\u6b65\u8c03\u8bd5\u7684\u65b9\u6cd5\u5b9a\u4f4d\u95ee\u9898\u3002\u63a8\u8350\u901a\u8fc7 <strong>golangci-lint<\/strong> \u7b49\u9759\u6001\u5206\u6790\u5de5\u5177\u68c0\u67e5\u4ee3\u7801\u4e2d\u7684\u6f5c\u5728 \u043f\u0440\u043e\u0431\u043b\u0435\u043c. \u9010\u6b65\u5206\u6790\u6d89\u53ca\u5230\u7684\u51fd\u6570\u548c\u6570\u636e\u7ed3\u6784\uff0c\u786e\u4fdd\u6240\u6709\u5206\u914d\u7684\u5185\u5b58\u90fd\u6709\u5bf9\u5e94\u7684\u91ca\u653e\u3002<\/p>\n<p><h2>\u4f18\u5316 memory usage<\/h2>\n<\/p>\n<p><h3>\u91ca\u653e\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61<\/h3>\n<\/p>\n<p>\u5728\u4f7f\u7528 Goroutines \u65f6\uff0c\u786e\u4fdd\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\u53ef\u4ee5\u88ab\u5783\u573e\u56de\u6536\u3002\u4f8b\u5982\uff0c\u5728\u4f7f\u7528\u5927\u6570\u7ec4\u6216\u7ed3\u6784\u4f53\u65f6\uff0c\u8981\u663e\u5f0f\u5c06\u5176\u6307\u9488\u8bbe\u4e3a nil\uff1a<\/p>\n<p><pre><code>func someFunc() {<\/p>\r\n<p>\tdata := make([]int, 1000000)<\/p>\r\n<p>\t\/\/ \u5904\u7406 data<\/p>\r\n<p>\tdata = nil \/\/ \u4f7f\u6570\u636e\u53ef\u88ab\u56de\u6536<\/p>\r\n<p>}<\/code><\/pre>\n<\/p>\n<p>\u8fd9\u5c06\u4f7f\u5f97 GC \u80fd\u591f\u66f4\u5feb\u5730\u8bc6\u522b\u4e0d\u518d\u9700\u8981\u7684\u5bf9\u8c61\uff0c\u907f\u514d\u5185\u5b58\u6301\u7eed\u589e\u957f\u3002<\/p>\n<p><h3>\u4f7f\u7528 sync.Pool \u8fdb\u884c\u7f13\u5b58<\/h3>\n<\/p>\n<p>\u5bf9\u4e8e\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5bf9\u8c61\uff0c\u4f7f\u7528 <strong>sync.Pool<\/strong> \u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u5185\u5b58\u5206\u914d\u548c GC \u7684\u538b\u529b\uff1a<\/p>\n<p><pre><code>var pool = sync.Pool{<\/p>\r\n<p>\tNew: func() interface{} {<\/p>\r\n<p>\t\treturn new(MyType)<\/p>\r\n<p>\t},<\/p>\r\n<p>}<\/p>\r\n\r\n<p>func getObject() *MyType {<\/p>\r\n<p>\treturn pool.Get().(*MyType)<\/p>\r\n<p>}<\/p>\r\n\r\n<p>func putObject(obj *MyType) {<\/p>\r\n<p>\tpool.Put(obj)<\/p>\r\n<p>}<\/code><\/pre>\n<\/p>\n<p>\u6b64\u4ee3\u7801\u5c55\u793a\u4e86\u4f7f\u7528 <strong>sync.Pool<\/strong> \u6c60\u6765\u91cd\u7528\u5bf9\u8c61\uff0c\u663e\u8457\u63d0\u9ad8\u6027\u80fd\u5e76\u964d\u4f4e\u95f4\u6b47\u6027 GC \u65f6\u95f4\u3002<\/p>\n<p><h2>\u6ce8\u610f\u4e8b\u9879\u548c\u5b9e\u7528\u6280\u5de7<\/h2>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u5728\u591a\u6b21\u8bf7\u6c42\u4e2d\u590d\u7528 pprof \u5b9e\u4f8b\uff0c\u907f\u514d\u591a\u6b21\u521b\u5efa\u670d\u52a1\u5e26\u6765\u7684\u6027\u80fd\u5f00\u9500\u3002<\/li>\n<li>\u5728\u6d4b\u8bd5\u548c\u5f00\u53d1\u73af\u5883\u4e2d\u4f7f\u7528 pprof\uff0c\u751f\u4ea7\u73af\u5883\u4e2d\u4e0d\u5efa\u8bae\u5f00\u542f pprof \u670d\u52a1\u3002<\/li>\n<li>\u5728\u5206\u6790\u5185\u5b58\u4e4b\u524d\uff0c\u5173\u95ed HTTP \u670d\u52a1\u5668\u4ee5\u9632\u6b62\u5e72\u6270\u5185\u5b58\u5206\u6790\u7ed3\u679c\u3002<\/li>\n<li>\u5b9a\u671f\u68c0\u67e5\u548c\u66f4\u65b0 Go \u7684\u7248\u672c\uff0c\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u548c bug \u4fee\u590d\u3002<\/li>\n<li>\u7ed3\u5408\u5176\u4ed6\u5de5\u5177\uff08\u5982 <strong>go tool trace<\/strong>\uff09\u6765\u83b7\u5f97\u66f4\u5168\u9762\u7684\u6027\u80fd\u5206\u6790\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5185\u5bb9\u5c31\u662f\u5173\u4e8e golang \u5185\u5b58\u4f7f\u7528\u76d1\u63a7\u7684\u6307\u5357\uff0c\u76f4\u63a5\u4f7f\u7528 pprof \u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u548c\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002\u901a\u8fc7\u6301\u7eed\u76d1\u63a7\u548c\u4f18\u5316\uff0c\u80fd\u591f\u6709\u6548\u51cf\u5c11\u5185\u5b58\u6cc4\u6f0f\u5e26\u6765\u7684\u8d1f\u9762\u5f71\u54cd\uff0c\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u89c2\u5bdf\u548c\u5206\u6790 Go \u7a0b\u5e8f\u5185\u5b58\u589e\u957f\u95ee\u9898 \u5f15\u8a00 \u5728\u5f00\u53d1 Golang \u5e94\u7528\u65f6\uff0c\u5185\u5b58\u4f7f\u7528\u91cf\u7684\u4e0d\u65ad\u589e\u52a0\u901a\u5e38\u662f\u4e00\u4e2a\u8b66\u793a\u4fe1\u53f7\uff0c\u53ef\u80fd\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u6216\u7cfb\u7edf\u5d29\u6e83\u3002\u4f7f\u7528 Go \u7684\u5185\u7f6e\u6027\u80fd\u5206\u6790\u5de5\u5177 pprof\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5b9e\u65f6\u76d1\u63a7\u548c\u5206\u6790\u7a0b\u5e8f\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002\u672c\u6587\u5c06\u8be6&#8230;<\/p>\n","protected":false},"author":1,"featured_media":975,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-974","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-baike"],"_links":{"self":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts\/974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/comments?post=974"}],"version-history":[{"count":1,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts\/974\/revisions"}],"predecessor-version":[{"id":977,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/posts\/974\/revisions\/977"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/media\/975"}],"wp:attachment":[{"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/media?parent=974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/categories?post=974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/tags?post=974"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.ddosidc.com\/wp\/wp-json\/wp\/v2\/topic?post=974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}