{"id":264597,"date":"2023-03-27T09:19:34","date_gmt":"2023-03-27T01:19:34","guid":{"rendered":"https:\/\/gulass.cn\/?p=264597"},"modified":"2023-02-20T20:20:36","modified_gmt":"2023-02-20T12:20:36","slug":"jvm-dead-object","status":"publish","type":"post","link":"https:\/\/gulass.cn\/jvm-dead-object.html","title":{"rendered":"JVM \u5982\u4f55\u786e\u5b9a\u6b7b\u4ea1\u5bf9\u8c61\uff1f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728 JVM \u4e2d\uff0c\u6709\u4e24\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\uff0c\u4e00\u4e2a\u662f JVM \u7684\u5185\u5b58\u5e03\u5c40\uff08JVM \u8fd0\u884c\u65f6\u7684\u6570\u636e\u533a\u57df\uff09\uff0c\u53e6\u4e00\u4e2a\u5c31\u662f\u5783\u573e\u56de\u6536\u3002\u800c\u5783\u573e\u56de\u6536\u4e2d\u53c8\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\uff0c\u4e00\u4e2a\u662f\u5982\u4f55\u786e\u5b9a JVM \u4e2d\u7684\u5783\u573e\u5bf9\u8c61\uff0c\u53e6\u4e00\u4e2a\u662f\u4f7f\u7528\u4e0d\u540c\u7684\u5783\u573e\u6536\u96c6\u5668\u8fdb\u884c\u5783\u573e\u56de\u6536\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5783\u573e\u5bf9\u8c61\u7684\u5224\u5b9a\u6709\u4e24\u79cd\u5e38\u7528\u7684\u7b97\u6cd5\uff1a\u5f15\u7528\u8ba1\u6570\u5668\u7b97\u6cd5\u548c\u53ef\u8fbe\u6027\u5206\u6790\u7b97\u6cd5\u3002<\/p>\n

1.\u5f15\u7528\u8ba1\u6570\u5668\u7b97\u6cd5<\/strong><\/div>\n

\u5f15\u7528\u8ba1\u6570\u7b97\u6cd5\uff08Reference Counting\uff09 \u5c5e\u4e8e\u5783\u573e\u6536\u96c6\u5668\u7684\u65e9\u671f\u5b9e\u73b0\u7b97\u6cd5\u4e86\uff0c\u5b83\u6307\u7684\u662f\u5728\u521b\u5efa\u5bf9\u8c61\u65f6\u5173\u8054\u4e00\u4e2a\u4e0e\u4e4b\u76f8\u5bf9\u5e94\u7684\u8ba1\u6570\u5668\uff0c\u5f53\u6b64\u5bf9\u8c61\u88ab\u4f7f\u7528\u65f6\u52a0 1\uff0c\u76f8\u53cd\u9500\u6bc1\u65f6 -1\u3002\u5f53\u6b64\u8ba1\u6570\u5668\u4e3a 0 \u65f6\uff0c\u5219\u8868\u793a\u6b64\u5bf9\u8c61\u672a\u4f7f\u7528\uff0c\u53ef\u4ee5\u88ab\u5783\u573e\u6536\u96c6\u5668\u56de\u6536\u3002<\/p>\n

\u5f15\u7528\u8ba1\u6570\u7b97\u6cd5\u7684\u4f18\u7f3a\u70b9\u5f88\u660e\u663e\uff0c\u5176\u4f18\u70b9\u662f\u5783\u573e\u56de\u6536\u6bd4\u8f83\u53ca\u65f6\uff0c\u5b9e\u65f6\u6027\u6bd4\u8f83\u9ad8\uff0c\u53ea\u8981\u5bf9\u8c61\u8ba1\u6570\u5668\u4e3a 0\uff0c\u5219\u53ef\u4ee5\u76f4\u63a5\u8fdb\u884c\u56de\u6536\u64cd\u4f5c\uff1b\u800c\u7f3a\u70b9\u662f\u65e0\u6cd5\u89e3\u51b3\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\uff0c\u6bd4\u5982\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

\r\npublic class RefCounterTest {\r\n    \/\/ \u5bf9\u8c61 A\r\n    static class RefObjectA {\r\n        private RefObjectB refObjectB;\r\n\r\n        public void setRefObjectB(RefObjectB refObjectB) {\r\n            this.refObjectB = refObjectB;\r\n        }\r\n    }\r\n    \/\/ \u5bf9\u8c61 B\r\n    static class RefObjectB {\r\n        private RefObjectA refObjectA;\r\n\r\n        public void setRefObjectA(RefObjectA refObjectA) {\r\n            this.refObjectA = refObjectA;\r\n        }\r\n    }\r\n    \/\/ \u6d4b\u8bd5\u4ee3\u7801\r\n    public static void main(String[] args) {\r\n        RefObjectA objectA = new RefObjectA();\r\n        RefObjectB objectB = new RefObjectB();\r\n        objectA.setRefObjectB(objectB);\r\n        objectB.setRefObjectA(objectA);\r\n        objectA = null;\r\n        objectB = null;\r\n    }\r\n}\r\n<\/pre>\n

\u5982\u4ee5\u4e0a\u4ee3\u7801\u6240\u793a\uff0c\u5373\u4f7f\u662f\u5c06 main \u65b9\u6cd5\u4e2d\u7684 objectA \u548c objectB \u90fd\u8bbe\u7f6e\u4e3a null\uff0c\u4e5f\u5c31\u662f\u8fd9\u4e24\u4e2a\u5bf9\u8c61\u90fd\u5f7b\u5e95\u4e0d\u4f7f\u7528\u4e86\uff0c\u4f46\u662f\u56e0\u4e3a\u4e8c\u8005\u5b58\u5728\u76f8\u4e92\u5f15\u7528\u7684\u5173\u7cfb\uff0c\u6240\u4ee5\u5b83\u4eec\u6240\u5bf9\u5e94\u7684\u5bf9\u8c61\u8ba1\u6570\u5668\u4e0d\u4e3a 0\uff0c\u8fd9\u6837\u5faa\u73af\u5f15\u7528\u5bfc\u81f4\u5783\u573e\u6570\u636e\u65e0\u6cd5\u88ab\u6e05\u9664\u7684\u4e8b\u4ef6\u5c31\u4ea7\u751f\u4e86\u3002<\/p>\n

2.\u53ef\u8fbe\u6027\u5206\u6790\u7b97\u6cd5<\/strong><\/div>\n

\u53ef\u8fbe\u6027\u5206\u6790\u7b97\u6cd5\uff08Reachability Analysis\uff09 \u662f\u76ee\u524d\u4e3b\u6d41\u865a\u62df\u673a\u4e2d\uff0c\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u5224\u65ad\u5783\u573e\u5bf9\u8c61\u7684\u5b9e\u73b0\u7b97\u6cd5\uff0c\u5b83\u6307\u7684\u662f\u4ece\u5bf9\u8c61\u7684\u8d77\u70b9\uff08GC Roots\uff09\u5f00\u59cb\u5411\u4e0b\u641c\u7d22\uff0c\u5982\u679c\u5bf9\u8c61\u5230 GC Roots \u6ca1\u6709\u4efb\u4f55\u5f15\u7528\u94fe\u76f8\u8fde\u65f6\uff0c\u4e5f\u5c31\u662f\u8bf4\u6b64\u5bf9\u8c61\u5230 GC Roots \u4e0d\u53ef\u8fbe\u65f6\uff0c\u5219\u8868\u793a\u6b64\u5bf9\u8c61\u53ef\u4ee5\u88ab\u5783\u573e\u56de\u6536\u5668\u6240\u56de\u6536\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n

\"\"<\/p>\n

\u5728 Java \u8bed\u8a00\u4e2d\uff0c\u53ef\u4f5c\u4e3a\u6839\u8282\u70b9\uff08GC Roots\uff09\u7684\u5bf9\u8c61\u6709\u4ee5\u4e0b 4 \u7c7b\uff1a
\n1. Java \u865a\u62df\u673a\u6808\u4e2d\u7684\u5f15\u7528\u5bf9\u8c61\uff0c\u4e5f\u5c31\u662f Java \u865a\u62df\u673a\u6808\u5e27\u4e2d\uff0c\u672c\u5730\u53d8\u91cf\u8868\u6240\u5b58\u50a8\u7684\uff08\u5f15\u7528\uff09\u5bf9\u8c61\u3002\u5728 Java \u865a\u62df\u673a\u6808\u5e27\u4e2d\u5b58\u50a8\u7684\u5bf9\u8c61\u90fd\u662f\u5c06\u6765\u6267\u884c\u65f6\uff0c\u8981\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u548c\u5f15\u7528\u5bf9\u8c61\u76f8\u5173\u7684\u5bf9\u8c61\u90fd\u4e0d\u80fd\u88ab\u56de\u6536\uff1b
\n2. \u672c\u5730\u65b9\u6cd5\u6808\u4e2d\u7684\u5f15\u7528\u5bf9\u8c61\u548c Java \u865a\u62df\u673a\u6808\u4e2d\u7684\u5f15\u7528\u5bf9\u8c61\u7c7b\u4f3c\uff0c\u4e5f\u4e0d\u80fd\u88ab\u56de\u6536\uff1b
\n3. \u65b9\u6cd5\u533a\u4e2d\u7c7b\u9759\u6001\u5c5e\u6027\u5f15\u7528\u7684\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u4f5c\u4e3a GC Roots\uff1b
\n4. \u65b9\u6cd5\u533a\u4e2d\u5e38\u91cf\u5f15\u7528\u7684\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u4f5c\u4e3a GC Roots\u3002\u56e0\u4e3a\u5e38\u91cf\u662f\u4fdd\u5b58\u5728\u5e38\u91cf\u6c60\u4e2d\u7684\uff0c\u5c5e\u4e8e\u5168\u5c40\u53ef\u4f7f\u7528\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u4e5f\u80fd\u4f5c\u4e3a GC Roots\u3002<\/p>\n

3.\u6709\u5173\u201c\u5f15\u7528\u201d<\/strong><\/div>\n

\u4e0d\u7ba1\u662f\u5f15\u7528\u8ba1\u6570\u6cd5\u8fd8\u662f\u53ef\u8fbe\u6027\u5206\u6790\u7b97\u6cd5\u90fd\u4e0e\u5bf9\u8c61\u7684\u201c\u5f15\u7528\u201d\u6709\u5173\uff0c\u8fd9\u8bf4\u660e\u5bf9\u8c61\u7684\u5f15\u7528\u51b3\u5b9a\u4e86\u5bf9\u8c61\u7684\u751f\u6b7b\uff0c\u800c Java \u4e2d\u7684\u5f15\u7528\u4e5f\u6bd4\u8f83\u590d\u6742\uff0c\u5b83\u4ece JDK 1.2 \u4e4b\u540e\uff0c\uff08\u5f15\u7528\uff09\u5206\u6210\u4e86\u4ee5\u4e0b 4 \u79cd\u7c7b\u578b\uff1a<\/p>\n

1. \u5f3a\u5f15\u7528\uff1a\u5728\u4ee3\u7801\u4e2d\u666e\u904d\u5b58\u5728\u7684\uff0c\u7c7b\u4f3c Object obj = new Object() \u8fd9\u7c7b\u5f15\u7528\uff0c\u53ea\u8981\u5f3a\u5f15\u7528\u8fd8\u5728\uff0c\u5783\u573e\u6536\u96c6\u5668\u6c38\u8fdc\u4e0d\u4f1a\u56de\u6536\u6389\u88ab\u5f15\u7528\u7684\u5bf9\u8c61\uff1b
\n2. \u8f6f\u5f15\u7528\uff1a\u662f\u4e00\u79cd\u76f8\u5bf9\u5f3a\u5f15\u7528\u5f31\u5316\u4e00\u4e9b\u7684\u5f15\u7528\uff0c\u53ef\u4ee5\u8ba9\u5bf9\u8c61\u8c41\u514d\u4e00\u4e9b\u5783\u573e\u6536\u96c6\uff0c\u53ea\u6709\u5f53 JVM \u8ba4\u4e3a\u5185\u5b58\u4e0d\u8db3\u65f6\uff0c\u624d\u4f1a\u53bb\u8bd5\u56fe\u56de\u6536\u8f6f\u5f15\u7528\u6307\u5411\u7684\u5bf9\u8c61\uff0cJVM \u4f1a\u786e\u4fdd\u5728\u629b\u51fa OutOfMemoryError \u4e4b\u524d\uff0c\u6e05\u7406\u8f6f\u5f15\u7528\u6307\u5411\u7684\u5bf9\u8c61\uff1b
\n3. \u5f31\u5f15\u7528\uff1a\u975e\u5fc5\u9700\u5bf9\u8c61\uff0c\u4f46\u5b83\u7684\u5f3a\u5ea6\u6bd4\u8f6f\u5f15\u7528\u66f4\u5f31\uff0c\u88ab\u5f31\u5f15\u7528\u5173\u8054\u7684\u5bf9\u8c61\u53ea\u80fd\u751f\u5b58\u5230\u4e0b\u4e00\u6b21\u5783\u573e\u6536\u96c6\u53d1\u751f\u4e4b\u524d\uff1b
\n4. \u865a\u5f15\u7528\uff1a\u4e5f\u79f0\u4e3a\u5e7d\u7075\u5f15\u7528\u6216\u5e7b\u5f71\u5f15\u7528\uff0c\u662f\u6700\u5f31\u7684\u4e00\u79cd\u5f15\u7528\u5173\u7cfb\uff0c\u65e0\u6cd5\u901a\u8fc7\u865a\u5f15\u7528\u6765\u83b7\u53d6\u4e00\u4e2a\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u4e3a\u5bf9\u8c61\u8bbe\u7f6e\u865a\u5f15\u7528\u7684\u76ee\u7684\u53ea\u6709\u4e00\u4e2a\uff0c\u5c31\u662f\u5f53\u7740\u4e2a\u5bf9\u8c61\u88ab\u6536\u96c6\u5668\u56de\u6536\u65f6\u6536\u5230\u4e00\u6761\u7cfb\u7edf\u901a\u77e5\u3002<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

\u5783\u573e\u5bf9\u8c61\u7684\u5224\u5b9a\u6709\u4e24\u79cd\u5e38\u7528\u7684\u7b97\u6cd5\uff1a\u5f15\u7528\u8ba1\u6570\u5668\u7b97\u6cd5\u548c\u53ef\u8fbe\u6027\u5206\u6790\u7b97\u6cd5\u3002\u5176\u4e2d\u5f15\u7528\u8ba1\u6570\u5668\u7b97\u6cd5\u5b9e\u73b0\u7b80\u5355\u3001\u8fd0\u884c\u9ad8\u6548\uff0c\u4f46\u662f\u5b58\u5728\u5faa\u73af\u5f15\u7528\u7684\u95ee\u9898\uff0c\u6240\u4ee5\u4e3b\u6d41\u7684\u865a\u62df\u673a\u4f7f\u7528\u7684\u90fd\u662f\u53ef\u8fbe\u6027\u5206\u6790\u7b97\u6cd5\uff0c\u53ef\u8fbe\u6027\u5206\u6790\u7b97\u6cd5\u662f\u4ece\u5bf9\u8c61\u7684\u6839\u8282\u70b9 GC Roots \u5411\u4e0b\u641c\u7d22\uff0c\u5982\u679c\u6839\u8282\u70b9\u76f8\u8fde\u5c31\u662f\u6b63\u5e38\u7684\u5bf9\u8c61\uff0c\u5426\u5219\u4e3a\u5783\u573e\u5bf9\u8c61\u53ef\u4ee5\u88ab\u5783\u573e\u56de\u6536\u5668\u56de\u6536\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5783\u573e\u5bf9\u8c61\u7684\u5224\u5b9a\u6709\u4e24\u79cd\u5e38\u7528\u7684\u7b97\u6cd5\uff1a\u5f15\u7528\u8ba1\u6570\u5668\u7b97\u6cd5\u548c\u53ef\u8fbe\u6027\u5206\u6790\u7b97\u6cd5\u3002 \u5f15\u7528\u8ba1\u6570\u7b97\u6cd5\uff08Reference Count […]<\/p>\n","protected":false},"author":1903,"featured_media":264600,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-264597","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/264597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=264597"}],"version-history":[{"count":1,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/264597\/revisions"}],"predecessor-version":[{"id":264599,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/264597\/revisions\/264599"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/264600"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=264597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=264597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=264597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}