{"id":18685,"date":"2023-02-27T21:58:20","date_gmt":"2023-02-27T13:58:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=18685"},"modified":"2023-07-30T22:59:51","modified_gmt":"2023-07-30T14:59:51","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e4%b8%bb%e6%b5%81gc%e8%af%ad%e8%a8%80%e4%b8%8d%e6%94%af%e6%8c%81gc%e5%92%8c%e6%89%8b%e5%8a%a8%e7%ae%a1%e7%90%86%e5%86%85%e5%ad%98%e6%b7%b7%e5%90%88%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/18685.html","title":{"rendered":"\u4e3a\u4ec0\u4e48\u4e3b\u6d41GC\u8bed\u8a00\u4e0d\u652f\u6301GC\u548c\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u6df7\u5408\u4f7f\u7528\u7684\u65b9\u5f0f\u5462"},"content":{"rendered":"<blockquote class=\"wp-block-quote\">\n<p>\u5176\u5b9e\u4e3b\u6d41GC\u8bed\u8a00\u652f\u6301GC\u548c\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u6df7\u5408\u4f7f\u7528\u7684\u65b9\u5f0f\u3002\u4efb\u4f55\u8bed\u8a00\u90fd\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u7cfb\u7edf\u51fd\u6570\u7533\u8bf7\u4e00\u5757\u865a\u62df\u5185\u5b58\uff0c\u6216\u8005\u53bb\u8c03\u7528c\/c++\u5305\u88c5\u7684malloc free\u51fd\u6570\u7684dll\u3002\u4e00\u822c\u90fd\u63d0\u4f9b\u4e86\uff0c\u53ea\u4e0d\u8fc7\u4f60\u6ca1\u627e\u5230\uff0c\u6bd4\u5982c# java\u90fd\u6709\u3002<\/p>\n<\/blockquote>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" alt=\"\" class=\"wp-image-18686\" decoding=\"async\" height=\"421\" sizes=\"(max-width: 680px) 100vw, 680px\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/02\/\u6dfb\u52a0\u6807\u9898-4-18.png?x-oss-process=image\/auto-orient,1\/format,webp\" srcset=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/02\/\u6dfb\u52a0\u6807\u9898-4-18.png?x-oss-process=image\/auto-orient,1\/format,webp 680w, https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/02\/\u6dfb\u52a0\u6807\u9898-4-18-300x186.png 300w\" width=\"680\"\/><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\"><strong>\u4e00\u3001\u4e3b\u6d41GC\u8bed\u8a00\u652f\u6301GC\u548c\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u6df7\u5408\u4f7f\u7528\u7684\u65b9\u5f0f<\/strong><\/h2>\n<p>\u4efb\u4f55\u8bed\u8a00\u90fd\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u7cfb\u7edf\u51fd\u6570\u7533\u8bf7\u4e00\u5757\u865a\u62df\u5185\u5b58\uff0c\u6216\u8005\u53bb\u8c03\u7528c\/c++\u5305\u88c5\u7684malloc free\u51fd\u6570\u7684dll\u3002\u201c\u5982\u679c\u6ca1\u6709\uff0c\u4e3a\u4f55\u4e3b\u6d41gc\u8bed\u8a00\u90fd\u4e0d\u63d0\u4f9b\u5462\uff1f\u201d\uff0c\u4e00\u822c\u90fd\u63d0\u4f9b\u4e86\uff0c\u53ea\u4e0d\u8fc7\u4f60\u6ca1\u627e\u5230\uff0c\u6bd4\u5982c# java\u90fd\u6709\u3002c#\u00a0 Marshal.AllocHGlobal Marshal.FreeHGlobal<\/p>\n<p>java sun.misc.Un<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a> allocateMemory freeMemory<\/p>\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u7cfb\u7edfdll\uff0cc#\u548cgo\u8c03\u7528dll\u975e\u5e38\u5bb9\u6613\uff0cjava\u53ef\u80fd\u8981\u5305\u88c5jni\u3002\u6216\u8005\u4efb\u610f\u8bed\u8a00\u90fd\u53ef\u4ee5\u7528libffi\u8fd9\u79cd\u4f4e\u6027\u80fd\u7684\u4e1c\u897f\u53bb\u8c03\u7528dll so\u3002<\/p>\n<p>\u6bd4\u5982\u76f4\u63a5\u8c03\u7528\u00a0 windows Kernel32.dll-&gt;virtualAlloc VirtualFree\u00a0 linux sys_mmap munmap\uff0c\u4f60\u5b8c\u5168\u53ef\u4ee5\u505a\u5230\u81ea\u5df1\u638c\u63a7\u5185\u5b58\uff0c\u4f46\u4f60\u771f\u7684\u61c2\u5417\uff1f<\/p>\n<p>\u65e0\u8bba\u4f60java c# go\uff0c\u90fd\u53ef\u4ee5\u53bb\u8c03\u7528\uff0c\u4f46\u4f60\u80fd\u77e5\u9053\u600e\u4e48\u5728\u5b9e\u9645\u4e1a\u52a1\u4e2d\u7528\u5417\uff1f\u4f60\u975e\u5e38\u591a\u5f53\u4e00\u5757\u6570\u7ec4\u7528\uff0c\u4ec5\u6b64\u800c\u5df2\uff0c\u90a3\u5f97\u7528c\u7684\u601d\u7ef4\u53bb\u7f16\u7a0b\uff0c\u6ca1\u6709c\u5de5\u7a0b\u7ecf\u9a8c\u7684\u6765\u8bf4\u8fd9\u4e2a\u529f\u80fd\u6839\u672c\u73a9\u4e0d\u8f6c\u3002<\/p>\n<p>\u67d0\u79cd\u89d2\u5ea6\u6765\u8bf4C++\u901a\u8fc7\u5bf9\u8c61\u751f\u547d\u5468\u671f\u548c\u6790\u6784\u51fd\u6570\u7684\u7ec4\u5408\uff0c\u505a\u5230\u4e86\u6808\u4e0a\u7a7a\u95f4\u5373\u7528\u5373\u653e\uff0c\u5806\u4e0a\u7a7a\u95f4\u4e0d\u5220\u4e0d\u653e\u3002<\/p>\n<p>\u5728C#\u4e2d\u53ef\u4ee5\u901a\u8fc7\u628a\u5bf9\u8c61\u8bbe\u7f6e\u4e3anull\u7684\u65b9\u5f0f\u7ed3\u675f\u5185\u5b58\u751f\u547d\u5468\u671f\u7136\u540e\u8c03\u7528System.GC.Collect()\u6765\u5f3a\u5236\u56de\u6536\u5185\u5b58\u3002<\/p>\n<p>\u5982\u679c\u5bf9\u5185\u5b58\u4e0d\u654f\u611f\uff0c\u90a3\u5c31\u653e\u5fc3\u7528GC\uff0cGC\u538b\u529b\u5927\u90a3\u662fGC\u7684\u4e8b\u513f\uff0c\u81ea\u5df1\u7701\u5fc3\u4e0d\u5c31\u597d\u4e86\uff0c\u5982\u679c\u5bf9\u5185\u5b58\u654f\u611f\uff0c\u90a3\u4f60\u5c31\u7528C\/C++\u81ea\u5df1\u63a7\u5236\uff0c\u76f8\u5bf9\u6574\u4e2a\u9879\u76ee\u800c\u8a00\uff0c\u826f\u597d\u7684\u8bbe\u8ba1\u6bd4\u51e0\u4e2a\u5f3a\u5236\u5185\u5b58\u56de\u6536\u66f4\u80fd\u51b3\u5b9a\u6027\u7684\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p>\u8bbe\u8ba1\u8bed\u8a00\u662f\u4e3a\u4e86\u89e3\u51b3\u95ee\u9898\uff0c\u800c\u4e0d\u662f\u5236\u9020\u9ebb\u70e6\u3002\u6839\u636e\u81ea\u5df1\u9700\u8981\u6311\u4e00\u628a\u6700\u8d81\u624b\u7684\u9524\u5b50\u624d\u662f\u5173\u952e\u3002<\/p>\n<p><strong>\u5ef6\u4f38\u9605\u8bfb\uff1a<\/strong><\/p>\n<h2 class=\"wp-block-heading\"><strong>\u4e8c\u3001C\u8bed\u8a00\u7684\u4e3b\u8981\u7279\u70b9<\/strong><\/h2>\n<p>1.\u8bed\u8a00\u7b80\u6d01\u3001\u7d27\u51d1\uff0c\u4f7f\u7528\u65b9\u4fbf\u3001\u7075\u6d3b \u3002C\u8bed\u8a00\u4e00\u5171\u53ea\u670932\u4e2a\u5173\u952e\u5b57\u30019\u79cd\u63a7\u5236\u8bed\u53e5\uff0c\u7a0b\u5e8f\u4e66\u5199\u5f62\u5f0f\u81ea\u7531\uff0c\u4e3b\u8981\u7528\u5c0f\u5199\u5b57\u6bcd\u8868\u793a\uff0c\u538b\u7f29\u4e86\u4e00\u5207\u4e0d\u5fc5\u8981\u7684\u6210\u5206\u3002C\u8bed\u8a00\u6bd4\u5176\u4ed6\u8bb8\u591a\u9ad8\u7ea7\u8bed\u8a00\u7b80\u7ec3\uff0c\u6e90\u7a0b\u5e8f\u77ed\uff0c\u56e0\u6b64\u8f93\u5165\u7a0b\u5e8f\u65f6\u5de5\u4f5c\u91cf\u5c11\u3002<\/p>\n<p>2.\u8fd0\u7b97\u7b26\u4e30\u5bcc\uff0cC\u8bed\u8a00\u7684\u8fd0\u7b97\u7b26\u5305\u542b\u7684\u8303\u56f4\u5f88\u5e7f\u6cdb\uff0c\u5171\u670934\u79cd\u8fd0\u7b97\u7b26\u3002C\u8bed\u8a00\u628a\u62ec\u53f7\u3001\u8d4b\u503c\u548c\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u7b49\u90fd\u4f5c\u4e3a\u8fd0\u7b97\u7b26\u5904\u7406\uff0c\u4ece\u800c\u4f7fC\u8bed\u8a00\u7684\u8fd0\u7b97\u7c7b\u578b\u6781\u5176\u4e30\u5bcc\uff0c\u8868\u8fbe\u5f0f\u7c7b\u578b\u591a\u6837\u5316\u3002<\/p>\n<p>3.\u6570\u636e\u7c7b\u578b\u4e30\u5bcc\u3002C\u8bed\u8a00\u63d0\u4f9b\u7684\u6570\u636e\u7c7b\u578b\u5305\u62ec\u6574\u5f62\u3001\u6d6e\u70b9\u578b\u3001\u5b57\u7b26\u578b\u3001\u6570\u7ec4\u7c7b\u578b\u3001\u6307\u9488\u7c7b\u578b\u3001\u7ed3\u6784\u4f53\u7c7b\u578b\u548c\u5171\u7528\u4f53\u7c7b\u578b\u7b49\uff0cC99\u53c8\u6269\u5145\u4e86\u590d\u6570\u6d6e\u70b9\u578b\u3001\u8d85\u957f\u6574\u578b\uff08long long\uff09\u548c\u5e03\u5c14\u7c7b\u578b\uff08bool\uff09\u7b49\u3002\u5c24\u5176\u662f\u6307\u9488\u7c7b\u578b\u6570\u636e\uff0c\u4f7f\u7528\u5341\u5206\u7075\u6d3b\u548c\u591a\u6837\u5316\uff0c\u80fd\u7528\u6765\u5b9e\u73b0\u5404\u79cd\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u94fe\u8868\u3001\u6811\u3001\u6808\u7b49\uff09\u7684\u8fd0\u7b97\u3002<\/p>\n<p>4.\u5177\u6709\u7ed3\u6784\u5316\u7684\u63a7\u5236\u8bed\u53e5\u3002\u7528\u51fd\u6570\u4f5c\u4e3a\u7a0b\u5e8f\u7684\u6a21\u5757\u5355\u4f4d\uff0c\u4fbf\u4e8e\u5b9e\u73b0\u7a0b\u5e8f\u7684\u6a21\u5757\u5316\u3002C\u8bed\u8a00\u662f\u5b8c\u5168\u6a21\u5757\u5316\u548c\u7ed3\u6784\u5316\u7684\u8bed\u8a00\u3002<\/p>\n<p>5.\u8bed\u6cd5\u9650\u5236\u4e0d\u592a\u4e25\u683c\uff0c\u7a0b\u5e8f\u8bbe\u8ba1\u81ea\u7531\u5ea6\u5927\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5176\u5b9e\u4e3b\u6d41GC\u8bed\u8a00\u652f\u6301GC\u548c\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u6df7\u5408\u4f7f\u7528\u7684\u65b9\u5f0f\u3002\u4efb\u4f55\u8bed\u8a00\u90fd\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528\u7cfb\u7edf\u51fd\u6570\u7533\u8bf7\u4e00\u5757\u865a\u62df\u5185\u5b58\uff0c\u6216\u8005\u53bb\u8c03 [&hellip;]","protected":false},"author":1,"featured_media":18686,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/18685"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=18685"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/18685\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/18686"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=18685"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=18685"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=18685"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}