{"id":115,"date":"2023-02-18T10:14:24","date_gmt":"2023-02-18T02:14:24","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=115"},"modified":"2023-02-18T10:15:06","modified_gmt":"2023-02-18T02:15:06","slug":"java-system-gc-happens","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/java-system-gc-happens\/","title":{"rendered":"\u8c03\u7528System.gc()\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<ul>\n<li><code>System.gc();<\/code> \/\/\u544a\u8bc9\u5783\u573e\u6536\u96c6\u5668\u6253\u7b97\u8fdb\u884c\u5783\u573e\u6536\u96c6\uff0c\u800c\u5783\u573e\u6536\u96c6\u5668\u8fdb\u4e0d\u8fdb\u884c\u6536\u96c6\u662f\u4e0d\u786e\u5b9a\u7684<\/li>\n<li><code>System.runFinalization();<\/code> \/\/\u5f3a\u5236\u8c03\u7528\u5df2\u7ecf\u5931\u53bb\u5f15\u7528\u7684\u5bf9\u8c61\u7684finalize\u65b9\u6cd5<\/li>\n<\/ul>\n<h2>\u67e5\u770b\u6e90\u7801<\/h2>\n<p><!-- more --><\/p>\n<p>\u5f53\u6211\u4eec\u8c03\u7528<code>System.gc()<\/code>\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u5e76\u4e0d\u4f1a\u9a6c\u4e0a\u8fdb\u884c\u5783\u573e\u56de\u6536\uff0c\u751a\u81f3\u4e0d\u4e00\u5b9a\u4f1a\u6267\u884c\u5783\u573e\u56de\u6536\uff0c\u67e5\u770b\u7cfb\u7edf\u6e90\u7801\u53ef\u4ee5\u770b\u5230<\/p>\n<pre><code class=\"language-java\">\/**\n * Indicates to the VM that it would be a good time to run the\n * garbage collector. Note that this is a hint only. There is no guarantee\n * that the garbage collector will actually be run.\n *\/\npublic static void gc() {\n    boolean shouldRunGC;\n    synchronized(lock) {\n        shouldRunGC = justRanFinalization;\n        if (shouldRunGC) {\n            justRanFinalization = false;\n        } else {\n            runGC = true;\n        }\n    }\n    if (shouldRunGC) {\n        Runtime.getRuntime().gc();\n    }\n}<\/code><\/pre>\n<p>\u4e5f\u5c31\u662f<code>justRanFinalization=true<\/code>\u7684\u65f6\u5019\u624d\u4f1a\u6267\u884c<\/p>\n<p>\u67e5\u627e\u53d1\u73b0\u5f53\u8c03\u7528<code>runFinalization()<\/code>\u7684\u65f6\u5019<code>justRanFinalization<\/code>\u53d8\u4e3a<code>true<\/code><\/p>\n<p>\u4e0b\u9762\u662f<code>runFinalization()<\/code>\u7684\u6e90\u7801<\/p>\n<pre><code class=\"language-java\">\/**\n* Provides a hint to the VM that it would be useful to attempt\n* to perform any outstanding object finalization.\n*\/\npublic static void runFinalization() {\n    boolean shouldRunGC;\n    synchronized(lock) {\n        shouldRunGC = runGC;\n        runGC = false;\n    }\n    if (shouldRunGC) {\n        Runtime.getRuntime().gc();\n    }\n    Runtime.getRuntime().runFinalization();\n    synchronized(lock) {\n        justRanFinalization = true;\n    }\n}<\/code><\/pre>\n<p>\u5176\u5b9e\u5f53\u6211\u4eec\u76f4\u63a5\u8c03\u7528<code>System.gc()<\/code>\u53ea\u4f1a\u628a\u8fd9\u6b21gc\u8bf7\u6c42\u8bb0\u5f55\u4e0b\u6765\uff0c\u7b49\u5230<code>runFinalization=true<\/code>\u7684\u65f6\u5019\u624d\u4f1a\u5148\u53bb\u6267\u884cGC\uff0c<code>runFinalization=true<\/code>\u4e4b\u540e\u4f1a\u5728\u5141\u8bb8\u4e00\u6b21<code>System.gc()<\/code>\u3002\u4e4b\u540e\u5728call <code>System.gc()<\/code>\u8fd8\u4f1a\u91cd\u590d\u4e0a\u9762\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u6240\u4ee5<code>System.gc()<\/code>\u8981\u8ddf<code>System.runFinalization()<\/code>\u4e00\u8d77\u642d\u914d\u4f7f\u7528\u624d\u597d\u3002<\/p>\n<p>\u67e5\u770b<code>ZygoteInit.java<\/code>\u91cc\u9762<code>gc()<\/code>\u548c<code>runFinalizationSync()<\/code>\u662f\u914d\u5408\u4f7f\u7528\u7684\uff0c\u8fd9\u6837\u624d\u6709\u6548\u679c<\/p>\n<pre><code class=\"language-java\">static void gcAndFinalize() {\n    final VMRuntime runtime = VMRuntime.getRuntime();\n\n    \/* runFinalizationSync() lets finalizers be called in Zygote,\n    * which doesn&#039;t have a HeapWorker thread.\n    *\/\n    System.gc();\n    runtime.runFinalizationSync();\n    System.gc();\n}<\/code><\/pre>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u7531\u6b64\u53ef\u89c1\uff0c\u5f53\u6211\u4eec\u9700\u8981\u8c03\u7528\u7684<code>System.gc()<\/code>\u7684\u65f6\u5019\uff0c\u8981\u8fd9\u6837\u624d\u4f1a\u6267\u884c<\/p>\n<pre><code class=\"language-java\">System.gc();\nruntime.runFinalizationSync();\nSystem.gc();<\/code><\/pre>\n<p>\u4e0d\u8fc7\u4e2a\u4eba\u5efa\u8bae\u4e0d\u5230\u4e07\u4e0d\u5f97\u5df2\u4e0d\u8981\u8c03\u7528\uff0c\u56e0\u4e3ajvm\u6709\u81ea\u5df1\u7684gc\u7b56\u7565\uff0c\u6839\u672c\u4e0d\u9700\u8981\u6211\u4eec\u6765\u624b\u52a8<\/p>\n","protected":false},"excerpt":{"rendered":"<p>System.gc(); \/\/\u544a\u8bc9\u5783\u573e\u6536\u96c6\u5668\u6253\u7b97\u8fdb\u884c\u5783\u573e\u6536\u96c6\uff0c\u800c\u5783\u573e\u6536\u96c6\u5668\u8fdb\u4e0d\u8fdb\u884c\u6536\u96c6\u662f\u4e0d\u786e\u5b9a\u7684 System [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[57],"class_list":["post-115","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-gc"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=115"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/115\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}