{"id":202635,"date":"2025-05-22T10:04:01","date_gmt":"2025-05-22T02:04:01","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202635\/"},"modified":"2025-05-22T10:04:01","modified_gmt":"2025-05-22T02:04:01","slug":"%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87%e5%9e%83%e5%9c%be%e6%94%b6%e9%9b%86%e5%99%a8%e9%85%8d%e7%bd%ae%e4%bc%98%e5%8c%96java%e5%87%bd%e6%95%b0%e7%9a%84%e5%86%85%e5%ad%98%e4%bd%bf%e7%94%a8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202635\/","title":{"rendered":"\u5982\u4f55\u901a\u8fc7\u5783\u573e\u6536\u96c6\u5668\u914d\u7f6e\u4f18\u5316Java\u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u901a\u8fc7\u5783\u573e\u6536\u96c6\u5668\u914d\u7f6e\u4f18\u5316Java\u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\uff1f<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5982\u4f55\u901a\u8fc7\u5783\u573e\u6536\u96c6\u5668\u914d\u7f6e\u4f18\u5316Java\u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p>\u4f18\u5316 Java lambda \u51fd\u6570\u5185\u5b58\u4f7f\u7528\u7684\u65b9\u6cd5\uff1a\u9009\u62e9\u5408\u9002\u7684 GC \u7b97\u6cd5\uff1aParallelGC\uff08\u9002\u7528\u4e8e\u4f4e\u5ef6\u8fdf\u548c\u541e\u5410\u91cf\uff09\u6216 G1GC\uff08\u9002\u7528\u4e8e\u5927\u5806\u6216\u4f4e\u6682\u505c\u65f6\u95f4\uff09\u914d\u7f6e\u5173\u952e GC \u53c2\u6570\uff1aParallelGC\uff1a-XX:ParallelGCThreads\uff08\u7ebf\u7a0b\u6570\uff09\u3001-XX:MaxGCPauseMillis\uff08\u6700\u5927\u6682\u505c\u65f6\u95f4\uff09G1GC\uff1a-XX:G1HeapRegionSize\uff08\u5806\u533a\u57df\u5927\u5c0f\uff09\u3001-XX:MaxGCPauseMillis\uff08\u6700\u5927\u6682\u505c\u65f6\u95f4\uff09<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729926143671c93ff7d29d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5982\u4f55\u901a\u8fc7\u5783\u573e\u6536\u96c6\u5668\u914d\u7f6e\u4f18\u5316 Java \u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528<\/strong><\/p>\n<p><strong>\u5f15\u8a00<\/strong><\/p>\n<p>\u5783\u573e\u6536\u96c6\u5668 (GC) \u7528\u4e8e\u7ba1\u7406 Java \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5185\u5b58\uff0c\u4f18\u5316\u5176\u914d\u7f6e\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u5185\u5b58\u6548\u7387\u3002\u672c\u6587\u5c06\u6307\u5bfc\u60a8\u5982\u4f55\u901a\u8fc7\u8c03\u6574 GC \u53c2\u6570\u6765\u4f18\u5316 Java lambda \u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<p><strong>\u5e38\u89c1\u7684 GC \u7b97\u6cd5<\/strong><\/p>\n<p>Java \u63d0\u4f9b\u4e86\u591a\u79cd GC \u7b97\u6cd5\uff0c\u6bcf\u79cd\u7b97\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u70b9\u548c\u7f3a\u70b9\uff1a<\/p>\n<ul>\n<li><strong>ParallelGC\uff1a<\/strong> \u9002\u5408\u591a\u6838\u7cfb\u7edf\uff0c\u5e76\u53d1\u6267\u884c GC\u3002<\/li>\n<li><strong>CMSGC\uff1a<\/strong> \u9002\u7528\u4e8e\u5177\u6709\u5927\u5806\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u91c7\u7528\u5e76\u53d1\u6807\u8bb0\u548c\u6e05\u9664\u7b97\u6cd5\u3002<\/li>\n<li><strong>G1GC\uff1a<\/strong> \u73b0\u4ee3 GC \u7b97\u6cd5\uff0c\u5b9e\u73b0\u5806\u5206\u4ee3\u548c\u589e\u91cf\u6536\u96c6\uff0c\u9002\u7528\u4e8e\u5927\u5bb9\u91cf\u5806\u3002<\/li>\n<\/ul>\n<p><strong>\u9009\u62e9\u6700\u9069 GC \u7b97\u6cd5<\/strong><\/p>\n<p>\u5bf9\u4e8e Java lambda \u51fd\u6570\uff0c\u901a\u5e38\u9009\u62e9 ParallelGC \u6216 G1GC\uff1a<\/p>\n<ul>\n<li><strong>ParallelGC\uff1a<\/strong> \u9002\u7528\u4e8e\u5927\u591a\u6570\u51fd\u6570\uff0c\u7279\u522b\u662f\u4f4e\u5ef6\u8fdf\u548c\u541e\u5410\u91cf\u4f18\u5148\u7684\u51fd\u6570\u3002<\/li>\n<li><strong>G1GC\uff1a<\/strong> \u9002\u7528\u4e8e\u5177\u6709\u5927\u5806\u6216\u4f4e\u6682\u505c\u65f6\u95f4\u8981\u6c42\u7684\u51fd\u6570\uff0c\u4f46\u53ef\u80fd\u4ea7\u751f\u66f4\u9ad8\u7684\u5f00\u9500\u3002<\/li>\n<\/ul>\n<p><strong>\u914d\u7f6e GC \u53c2\u6570<\/strong><\/p>\n<p>\u4e00\u65e6\u9009\u62e9\u4e86 GC \u7b97\u6cd5\uff0c\u5c31\u53ef\u4ee5\u914d\u7f6e\u4ee5\u4e0b\u53c2\u6570\uff1a<\/p>\n<ul>\n<li><strong>-XX:ParallelGCThreads\uff1a<\/strong> \u5e76\u884c GC \u7ebf\u7a0b\u6570 (ParallelGC)<\/li>\n<li><strong>-XX:G1HeapRegionSize\uff1a<\/strong> G1GC \u5806\u533a\u57df\u5927\u5c0f (G1GC)<\/li>\n<li><strong>-XX:MaxGCPauseMillis\uff1a<\/strong> GC \u6682\u505c\u65f6\u95f4\u7684\u6700\u5927\u5141\u8bb8\u503c<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b Java lambda \u51fd\u6570\uff1a<\/p>\n<pre>import com.google.cloud.functions.HttpFunction;\nimport com.google.cloud.functions.HttpRequest;\nimport com.google.cloud.functions.HttpResponse;\nimport java.io.BufferedWriter;\nimport java.io.IOException;\n\npublic class MemoryIntensive implements HttpFunction {\n    @Override\n    public void service(HttpRequest request, HttpResponse response)\n            throws IOException {\n        \/\/ \u53ef\u80fd\u4f1a\u5bfc\u81f4 OOM \u5f02\u5e38\u7684\u5185\u5b58\u5bc6\u96c6\u578b\u64cd\u4f5c\n        BufferedWriter writer = response.getWriter();\n        for (int i = 0; i &lt; 10000000; i++) {\n            writer.write(\"This is a memory intensive operation.\");\n        }\n    }\n}<\/pre>\n<p><strong>\u4f18\u5316<\/strong><\/p>\n<p>\u4e3a\u4e86\u4f18\u5316\u6b64\u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\uff0c\u53ef\u4ee5\u8c03\u6574 GC \u53c2\u6570\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e ParallelGC\uff1a<code>-XX:ParallelGCThreads=4 -XX:MaxGCPauseMillis=200<\/code><\/li>\n<li>\u5bf9\u4e8e G1GC\uff1a<code>-XX:G1HeapRegionSize=16m -XX:MaxGCPauseMillis=100<\/code><\/li>\n<\/ul>\n<p><strong>\u90e8\u7f72<\/strong><\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5728\u51fd\u6570\u90e8\u7f72\u671f\u95f4\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u6765\u5e94\u7528\u8fd9\u4e9b\u914d\u7f6e\uff1a<\/p>\n<pre>gcloud functions deploy function_name \\\n    --runtime java \\\n    --env-vars GC_CONFIG=\"-XX:MaxGCPauseMillis=200\"<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u8c03\u6574 GC \u914d\u7f6e\uff0c\u60a8\u53ef\u4ee5\u663e\u7740\u4f18\u5316 Java lambda \u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\u3002\u901a\u8fc7\u9009\u62e9\u9002\u5f53\u7684 GC \u7b97\u6cd5\u5e76\u914d\u7f6e\u5173\u952e\u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u9632\u6b62 OOM \u5f02\u5e38\uff0c\u63d0\u9ad8\u51fd\u6570\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u901a\u8fc7\u5783\u573e\u6536\u96c6\u5668\u914d\u7f6e\u4f18\u5316Java\u51fd\u6570\u7684\u5185\u5b58\u4f7f\u7528\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u5185\u5b58\u4f18\u5316,\u5783\u573e\u6536\u96c6\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u901a\u8fc7\u5783\u573e\u6536\u96c6\u5668\u914d\u7f6e\u4f18\u5316Jav&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202635","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202635"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202635\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}