{"id":3579,"date":"2021-07-04T16:53:32","date_gmt":"2021-07-04T08:53:32","guid":{"rendered":"https:\/\/wyxxt.org.cn\/?p=3579"},"modified":"2023-12-04T15:18:51","modified_gmt":"2023-12-04T07:18:51","slug":"go%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/wyxxt.org.cn\/?p=3579","title":{"rendered":"go\u4f18\u5316"},"content":{"rendered":"<h1>\u9003\u9038\u5206\u6790<\/h1>\n<p>\u5728 C \u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>malloc<\/code> \u548c <code>free<\/code> \u624b\u52a8\u5728\u5806\u4e0a\u5206\u914d\u548c\u56de\u6536\u5185\u5b58\u3002Go \u8bed\u8a00\u4e2d\uff0c\u5806\u5185\u5b58\u662f\u901a\u8fc7\u5783\u573e\u56de\u6536\u673a\u5236\u81ea\u52a8\u7ba1\u7406\u7684\uff0c\u65e0\u9700\u5f00\u53d1\u8005\u6307\u5b9a\u3002\u90a3\u4e48\uff0cGo \u7f16\u8bd1\u5668\u600e\u4e48\u77e5\u9053\u67d0\u4e2a\u53d8\u91cf\u9700\u8981\u5206\u914d\u5728\u6808\u4e0a\uff0c\u8fd8\u662f\u5806\u4e0a\u5462\uff1f<strong>\u7f16\u8bd1\u5668\u51b3\u5b9a\u5185\u5b58\u5206\u914d\u4f4d\u7f6e\u7684\u65b9\u5f0f\uff0c\u5c31\u79f0\u4e4b\u4e3a\u9003\u9038\u5206\u6790(escape analysis)<\/strong>\u3002\u9003\u9038\u5206\u6790\u7531\u7f16\u8bd1\u5668\u5b8c\u6210\uff0c\u4f5c\u7528\u4e8e\u7f16\u8bd1\u9636\u6bb5\u3002<\/p>\n<pre><code class=\"language-shell line-numbers\">go run -gcflags \"-m -l\" main.go \n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/17\/image-20210823164853750.png\" alt=\"image-20210823164853750\" \/><\/p>\n<ol>\n<li>\u6307\u9488\u9003\u9038\n<p>\u6307\u9488\u9003\u9038\u5e94\u8be5\u662f\u6700\u5bb9\u6613\u7406\u89e3\u7684\u4e00\u79cd\u60c5\u51b5\u4e86\uff0c\u5373\u5728\u51fd\u6570\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd4\u56de\u4e86\u8fd9\u4e2a\u5bf9\u8c61\u7684\u6307\u9488\u3002\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u51fd\u6570\u867d\u7136\u9000\u51fa\u4e86\uff0c\u4f46\u662f\u56e0\u4e3a\u6307\u9488\u7684\u5b58\u5728\uff0c\u5bf9\u8c61\u7684\u5185\u5b58\u4e0d\u80fd\u968f\u7740\u51fd\u6570\u7ed3\u675f\u800c\u56de\u6536\uff0c\u56e0\u6b64\u53ea\u80fd\u5206\u914d\u5728\u5806\u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p>interface{}\u52a8\u6001\u7c7b\u578b\u9003\u9038<\/p>\n<p>\u5728 Go \u8bed\u8a00\u4e2d\uff0c\u7a7a\u63a5\u53e3\u5373 <code>interface{}<\/code> \u53ef\u4ee5\u8868\u793a\u4efb\u610f\u7684\u7c7b\u578b\uff0c\u5982\u679c\u51fd\u6570\u53c2\u6570\u4e3a <code>interface{}<\/code>\uff0c\u7f16\u8bd1\u671f\u95f4\u5f88\u96be\u786e\u5b9a\u5176\u53c2\u6570\u7684\u5177\u4f53\u7c7b\u578b\uff0c\u4e5f\u4f1a\u53d1\u751f\u9003\u9038\u3002<\/p>\n<\/li>\n<li>\n<p>\u6808\u7a7a\u95f4\u4e0d\u8db3<\/p>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u5bf9\u5185\u6838\u7ebf\u7a0b\u4f7f\u7528\u7684\u6808\u7a7a\u95f4\u662f\u6709\u5927\u5c0f\u9650\u5236\u7684\uff0c64 \u4f4d\u7cfb\u7edf\u4e0a\u901a\u5e38\u662f 8 MB\u3002\u53ef\u4ee5\u4f7f\u7528 <code>ulimit -a<\/code> \u547d\u4ee4\u67e5\u770b\u673a\u5668\u4e0a\u6808\u5141\u8bb8\u5360\u7528\u7684\u5185\u5b58\u7684\u5927\u5c0f\u3002<\/p>\n<p>\u5bf9\u4e8e Go \u8bed\u8a00\u6765\u8bf4\uff0c\u8fd0\u884c\u65f6(runtime) \u5c1d\u8bd5\u5728 goroutine \u9700\u8981\u7684\u65f6\u5019\u52a8\u6001\u5730\u5206\u914d\u6808\u7a7a\u95f4\uff0cgoroutine \u7684\u521d\u59cb\u6808\u5927\u5c0f\u4e3a 2 KB\u3002\u5f53 goroutine \u88ab\u8c03\u5ea6\u65f6\uff0c\u4f1a\u7ed1\u5b9a\u5185\u6838\u7ebf\u7a0b\u6267\u884c\uff0c\u6808\u7a7a\u95f4\u5927\u5c0f\u4e5f\u4e0d\u4f1a\u8d85\u8fc7\u64cd\u4f5c\u7cfb\u7edf\u7684\u9650\u5236\u3002<\/p>\n<p>\u5bf9 Go \u7f16\u8bd1\u5668\u800c\u8a00\uff0c\u8d85\u8fc7\u4e00\u5b9a\u5927\u5c0f\u7684\u5c40\u90e8\u53d8\u91cf\u5c06\u9003\u9038\u5230\u5806\u4e0a\uff0c\u4e0d\u540c\u7684 Go \u7248\u672c\u7684\u5927\u5c0f\u9650\u5236\u53ef\u80fd\u4e0d\u4e00\u6837\u3002<\/p>\n<\/li>\n<li>\n<p>\u95ed\u5305<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"language-go line-numbers\">func Increase() func() int {\n    n := 0\n    return func() int {\n        n++\n        return n\n    }\n}\n\nfunc main() {\n    in := Increase()\n    fmt.Println(in()) \/\/ 1\n    fmt.Println(in()) \/\/ 2\n}\n<\/code><\/pre>\n<p><code>Increase()<\/code> \u8fd4\u56de\u503c\u662f\u4e00\u4e2a\u95ed\u5305\u51fd\u6570\uff0c\u8be5\u95ed\u5305\u51fd\u6570\u8bbf\u95ee\u4e86\u5916\u90e8\u53d8\u91cf n\uff0c\u90a3\u53d8\u91cf n \u5c06\u4f1a\u4e00\u76f4\u5b58\u5728\uff0c\u76f4\u5230 <code>in<\/code> \u88ab\u9500\u6bc1\u3002\u5f88\u663e\u7136\uff0c\u53d8\u91cf n \u5360\u7528\u7684\u5185\u5b58\u4e0d\u80fd\u968f\u7740\u51fd\u6570 <code>Increase()<\/code> \u7684\u9000\u51fa\u800c\u56de\u6536\uff0c\u56e0\u6b64\u5c06\u4f1a\u9003\u9038\u5230\u5806\u4e0a\u3002<\/p>\n<h1>GC\u4f18\u5316<\/h1>\n<h2>\u4f20\u503cvs\u4f20\u6307\u9488<\/h2>\n<p>\u4f20\u503c\u4f1a\u62f7\u8d1d\u6574\u4e2a\u5bf9\u8c61\uff0c\u800c\u4f20\u6307\u9488\u53ea\u4f1a\u62f7\u8d1d\u6307\u9488\u5730\u5740\uff0c\u6307\u5411\u7684\u5bf9\u8c61\u662f\u540c\u4e00\u4e2a\u3002\u4f20\u6307\u9488\u53ef\u4ee5\u51cf\u5c11\u503c\u7684\u62f7\u8d1d\uff0c\u4f46\u662f\u4f1a\u5bfc\u81f4\u5185\u5b58\u5206\u914d\u9003\u9038\u5230\u5806\u4e2d\uff0c\u589e\u52a0\u5783\u573e\u56de\u6536(GC)\u7684\u8d1f\u62c5\u3002\u5728\u5bf9\u8c61\u9891\u7e41\u521b\u5efa\u548c\u5220\u9664\u7684\u573a\u666f\u4e0b\uff0c\u4f20\u9012\u6307\u9488\u5bfc\u81f4\u7684 GC \u5f00\u9500\u53ef\u80fd\u4f1a\u4e25\u91cd\u5f71\u54cd\u6027\u80fd\u3002<\/p>\n<p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u5bf9\u4e8e\u9700\u8981\u4fee\u6539\u539f\u5bf9\u8c61\u503c\uff0c\u6216\u5360\u7528\u5185\u5b58\u6bd4\u8f83\u5927\u7684\u7ed3\u6784\u4f53\uff0c\u9009\u62e9\u4f20\u6307\u9488\u3002\u5bf9\u4e8e\u53ea\u8bfb\u7684\u5360\u7528\u5185\u5b58\u8f83\u5c0f\u7684\u7ed3\u6784\u4f53\uff0c\u76f4\u63a5\u4f20\u503c\u80fd\u591f\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<h2>slice \u9884\u5148\u5206\u914d\u5185\u5b58<\/h2>\n<p>\u5f53<code>slice<\/code>\u7684\u5bb9\u91cf\u5c0f\u4e8e1024\u65f6\uff0c\u5bb9\u91cf\u662f\u6309\u71672\u500d\u5927\u5c0f\u589e\u957f\u7684\u3002\u5f53\u5bb9\u91cf\u5927\u4e8e1024\uff0c\u589e\u957f\u7684\u5bb9\u91cf\u662f\u539f\u6765\u76841.25\u500d<\/p>\n<pre><code class=\"language-go line-numbers\">\/**\n * slice.go\n *\/\npackage demo\n\nfunc appendOne(num int) []int {\n    var res []int\n    for i := 0; i &lt; num; i++ {\n        res = append(res, i)\n    }\n    return res\n}\n\nfunc appendMany(num int) []int {\n    res := make([]int, 0, num)\n    for i := 0; i &lt; num; i++ {\n        res = append(res, i)\n    }\n    return res\n}\n\n<\/code><\/pre>\n<pre><code class=\"language-go line-numbers\">\/**\n * slice_test.go\n *\/\npackage demo\n\nimport \"testing\"\n\nfunc BenchmarkAppendOne(b *testing.B) {\n    num := 100000\n    for i := 0; i &lt; b.N; i++ {\n        _ = appendOne(num)\n    }\n}\n\nfunc BenchmarkAppendMany(b *testing.B) {\n    num := 100000\n    for i := 0; i &lt; b.N; i++ {\n        _ = appendMany(num)\n    }\n}\n\n<\/code><\/pre>\n<pre><code class=\"language-shell line-numbers\">go test -bench=. -benchmem\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/17\/image-20210823171232783.png\" alt=\"image-20210823171232783\" \/><\/p>\n<h2>map\u4e2d\u4fdd\u5b58\u503c\uff0c\u800c\u4e0d\u662f\u6307\u9488<\/h2>\n<h2>\u4f7f\u7528\u5206\u6bb5map<\/h2>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4f7f\u7528\u5206\u6bb5\u7684\uff0c\u4fdd\u5b58\u503c\u7684map\u7684GC\u8017\u65f6\u6700\u5c0f<\/p>\n<h2>\u4f7f\u7528struct{}\u4f18\u5316<\/h2>\n<p><code>struct{}<\/code>\u7ecf\u8fc7\u7f16\u8bd1\u5668\u7279\u6b8a\u4f18\u5316\uff0c\u6307\u5411\u540c\u4e00\u4e2a\u5185\u5b58\u5730\u5740(runtime.zerobase)\uff0c\u4e0d\u5360\u7528\u7a7a\u95f4\u3002<\/p>\n<h2>GC\u5206\u6790\u5de5\u5177<\/h2>\n<pre><code class=\"language-shell line-numbers\">go tool pprfo\ngo tool trace\ngo run -gcflags \"-m -l\" main.go \nGODEBUG=\"gctrace=1\"\n<\/code><\/pre>\n<h1>\u6c60\u5316 sync.pool<\/h1>\n<p>\u4fdd\u5b58\u548c\u590d\u7528\u4e34\u65f6\u5bf9\u8c61\uff0c\u51cf\u5c11\u5185\u5b58\u5206\u914d\uff0c\u964d\u4f4e GC \u538b\u529b<\/p>\n<p>json \u7684\u53cd\u5e8f\u5217\u5316\u5728\u6587\u672c\u89e3\u6790\u548c\u7f51\u7edc\u901a\u4fe1\u8fc7\u7a0b\u4e2d\u975e\u5e38\u5e38\u89c1\uff0c\u5f53\u7a0b\u5e8f\u5e76\u53d1\u5ea6\u975e\u5e38\u9ad8\u7684\u60c5\u51b5\u4e0b\uff0c\u77ed\u65f6\u95f4\u5185\u9700\u8981\u521b\u5efa\u5927\u91cf\u7684\u4e34\u65f6\u5bf9\u8c61\u3002\u800c\u8fd9\u4e9b\u5bf9\u8c61\u662f\u90fd\u662f\u5206\u914d\u5728\u5806\u4e0a\u7684\uff0c\u4f1a\u7ed9 GC \u9020\u6210\u5f88\u5927\u538b\u529b\uff0c\u4e25\u91cd\u5f71\u54cd\u7a0b\u5e8f\u7684\u6027\u80fd\u3002<\/p>\n<p>Go \u8bed\u8a00\u4ece 1.3 \u7248\u672c\u5f00\u59cb\u63d0\u4f9b\u4e86\u5bf9\u8c61\u91cd\u7528\u7684\u673a\u5236\uff0c\u5373 <code>sync.Pool<\/code>\u3002<code>sync.Pool<\/code> \u662f\u53ef\u4f38\u7f29\u7684\uff0c\u540c\u65f6\u4e5f\u662f\u5e76\u53d1\u5b89\u5168\u7684\uff0c\u5176\u5927\u5c0f\u4ec5\u53d7\u9650\u4e8e\u5185\u5b58\u7684\u5927\u5c0f\u3002<code>sync.Pool<\/code> \u7528\u4e8e\u5b58\u50a8\u90a3\u4e9b\u88ab\u5206\u914d\u4e86\u4f46\u662f\u6ca1\u6709\u88ab\u4f7f\u7528\uff0c\u800c\u672a\u6765\u53ef\u80fd\u4f1a\u4f7f\u7528\u7684\u503c\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e0d\u7528\u518d\u6b21\u7ecf\u8fc7\u5185\u5b58\u5206\u914d\uff0c\u53ef\u76f4\u63a5\u590d\u7528\u5df2\u6709\u5bf9\u8c61\uff0c\u51cf\u8f7b GC \u7684\u538b\u529b\uff0c\u4ece\u800c\u63d0\u5347\u7cfb\u7edf\u7684\u6027\u80fd\u3002<\/p>\n<p><code>sync.Pool<\/code> \u7684\u5927\u5c0f\u662f\u53ef\u4f38\u7f29\u7684\uff0c\u9ad8\u8d1f\u8f7d\u65f6\u4f1a\u52a8\u6001\u6269\u5bb9\uff0c\u5b58\u653e\u5728\u6c60\u4e2d\u7684\u5bf9\u8c61\u5982\u679c\u4e0d\u6d3b\u8dc3\u4e86\u4f1a\u88ab\u81ea\u52a8\u6e05\u7406\u3002<\/p>\n<h2>\u4f7f\u7528<\/h2>\n<pre><code class=\"language-go line-numbers\">package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"sync\"\n)\n\ntype Student struct {\n    Name   string\n    Age    int32\n    Remark [1024]byte\n}\n\nvar studentPool = sync.Pool{New: func() interface{} {\n    return new(Student)\n}}\n\nfunc main() {\n    var buf, _ = json.Marshal(Student{Name: \"Geektutu\", Age: 25})\n    stu := studentPool.Get().(*Student)\n    json.Unmarshal(buf, stu)\n    studentPool.Put(stu)\n    fmt.Println(stu)\n}\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/17\/image-20210823173830618.png\" alt=\"image-20210823173830618\" \/><\/p>\n<h1>\u5185\u5b58\u5206\u914d\u6a21\u578b<\/h1>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/17\/image-20210823174139187.png\" alt=\"image-20210823174139187\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/oss.wyxxt.org.cn\/images\/2021\/09\/17\/image-20210701111051915.png\" alt=\"image-20210701111051915\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9003\u9038\u5206\u6790 \u5728 C \u8bed\u8a00\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 malloc \u548c free \u624b\u52a8\u5728\u5806\u4e0a\u5206\u914d\u548c\u56de\u6536\u5185\u5b58\u3002Go \u8bed\u8a00\u4e2d\uff0c\u5806\u5185\u5b58 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[15],"tags":[398],"class_list":["post-3579","post","type-post","status-publish","format-standard","hentry","category-15","tag-go"],"_links":{"self":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3579"}],"version-history":[{"count":5,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3579\/revisions"}],"predecessor-version":[{"id":3631,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=\/wp\/v2\/posts\/3579\/revisions\/3631"}],"wp:attachment":[{"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wyxxt.org.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}