{"id":933,"date":"2026-07-07T16:23:36","date_gmt":"2026-07-07T16:23:36","guid":{"rendered":"https:\/\/boochlin.com\/?p=933"},"modified":"2026-09-19T19:17:43","modified_gmt":"2026-09-19T19:17:43","slug":"android-%e7%88%9b%e8%a3%9d%e7%bd%ae%e6%83%b3%e8%b7%91%e4%ba%ba%e8%87%89%e8%be%a8%e8%ad%98-gc-thrashing-%e6%80%9d%e8%b7%af%e8%a3%9c%e5%85%85","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=933","title":{"rendered":"Android \u721b\u88dd\u7f6e\u60f3\u8dd1\u4eba\u81c9\u8fa8\u8b58 \u2013 GC Thrashing \u601d\u8def\u88dc\u5145"},"content":{"rendered":"<p>\u5728\u6211\u5011\u90a3\u53f0\u53ea\u6709 2GB RAM\u3001CPU \u6548\u80fd\u5b71\u5f31\u7684\u5d4c\u5165\u5f0f\u9580\u7981\u6a5f\u4e0a\u8dd1\u4eba\u81c9\u8fa8\u8b58\uff0c\u958b\u767c\u8005\u7684\u65e5\u5e38\u57fa\u672c\u4e0a\u5c31\u662f\u8ddf\u7cfb\u7d71\u5728\u61f8\u5d16\u908a\u6436\u8a18\u61b6\u9ad4\u3002<\/p>\n<p>\u4e00\u5207\u7684\u958b\u7aef\uff0c\u6e90\u81ea\u65bc\u4e00\u6bb5\u8b93\u6211\u5011\u6bdb\u9aa8\u609a\u7136\u7684 Logcat\u3002<\/p>\n<p>\u67d0\u5929\uff0c\u6211\u5011\u767c\u73fe\u9580\u7981\u6a5f\u9023\u7e8c\u904b\u4f5c\u5e7e\u5c0f\u6642\u5f8c\uff0c\u756b\u9762\u958b\u59cb\u5287\u70c8\u6389\u5e40\u3001\u8fa8\u8b58\u5361\u6210\u5e7b\u71c8\u7247\u3002\u7cfb\u7d71\u6c92\u6709 OOM \u9583\u9000\uff0c\u4f46\u63a5\u4e0a adb \u6253\u958b logcat \u4e00\u770b\uff0c\u756b\u9762\u6b63\u5728\u4ee5\u6bcf\u79d2\u6578\u5341\u884c\u7684\u901f\u5ea6\u760b\u72c2\u6d17\u983b\uff1a<\/p>\n<pre><code class=\"hljs language-plaintext\">I\/art: Background concurrent copying GC freed 50(2KB) AllocSpace objects, 0(0B) LOS objects, 11% free, 21MB\/23MB, paused 2.1ms total 25.1ms\nI\/art: Background concurrent copying GC freed 12(1KB) AllocSpace objects, 0(0B) LOS objects, 10% free, 21MB\/23MB, paused 1.8ms total 22.3ms<\/code><\/pre>\n<p>\u9019\u5728\u7cfb\u7d71\u5c64\u9762\u53eb\u505a <strong>GC Thrashing\uff08\u5783\u573e\u56de\u6536\u985b\u7c38\uff09<\/strong>\u3002<\/p>\n<p>\u6ce8\u610f\u770b\u90a3\u500b <code>freed<\/code> \u7684\u6578\u5b57\uff1aART \u7684\u5783\u573e\u56de\u6536\u5668\u6b63\u8655\u65bc\u6975\u5ea6\u6050\u614c\u7684\u72c0\u614b\uff0c\u6bcf 20 \u6beb\u79d2\u5c31\u62da\u8001\u547d\u8dd1\u4e00\u6b21 GC\uff0c\u6bcf\u6b21\u537b\u53ea\u80fd\u6e05\u51fa\u5fae\u4e0d\u8db3\u9053\u7684 1~2 KB \u5783\u573e\u3002CPU \u7b97\u529b\u5168\u90e8\u88ab GC \u6436\u5149\uff0cAI \u6a21\u578b\u6839\u672c\u5206\u4e0d\u5230\u6642\u9593\u904b\u7b97\u3002<\/p>\n<p>\u6211\u5011\u7528 Memory Profiler \u6293\u4e0b Heap Dump\uff0c\u7a0b\u5f0f\u78bc\u660e\u660e\u6c92\u6709 Memory Leak\uff0c\u4f46\u537b\u770b\u5230\u5e7e\u5343\u500b OpenCV \u7684 <code>Mat<\/code> \u548c ToF \u9ede\u96f2\u5305\u88dd\u7269\u4ef6\uff0c\u5168\u90e8\u5361\u5728\u4e00\u500b\u53eb <code>java.lang.ref.FinalizerReference<\/code> \u7684\u4f47\u5217\u88e1\u5927\u6392\u9577\u9f8d\u3002<\/p>\n<p>\u9019\u8b93\u6211\u5011\u7576\u5834\u50bb\u773c\uff1a<strong>\u300c\u7b49\u7b49\uff0c\u6211\u5011\u4e0d\u662f\u65e9\u5c31\u5347\u5230 Android 10 \u4e86\u55ce\uff1f\u5b98\u65b9\u4e0d\u662f\u65e9\u5c31\u5168\u9762\u6539\u7528 Cleaner \u4e86\u55ce\uff1f\u70ba\u4ec0\u9ebc Finalizer \u7684\u5e7d\u9748\u9084\u5728\uff1f\u300d<\/strong><\/p>\n<hr>\n<h2>\u6b77\u53f2\u5e7d\u9748\uff1aAndroid 10 \u70ba\u4ec0\u9ebc\u9084\u6709 Finalizer\uff1f<\/h2>\n<p>\u5f88\u591a\u958b\u767c\u8005\u90fd\u6709\u4e00\u500b\u7f8e\u9e97\u7684\u8aa4\u89e3\uff1a\u4ee5\u70ba Android 8.0 \u628a <code>Bitmap<\/code> \u6539\u7528 <code>NativeAllocationRegistry<\/code> \u4e4b\u5f8c\uff0c<code>finalize()<\/code> \u9019\u500b\u6b77\u53f2\u5305\u88b1\u5c31\u5df2\u7d93\u5fb9\u5e95\u5f9e Android \u4e16\u754c\u4e0a\u6d88\u5931\u4e86\u3002<\/p>\n<p><strong>\u73fe\u5be6\u7d66\u4e86\u6211\u5011\u4e00\u8a18\u91cd\u62f3\u3002<\/strong><\/p>\n<p>\u5728 Android 10 \u4e2d\uff0c\u771f\u76f8\u662f\u9019\u6a23\u7684\uff1a<\/p>\n<ul>\n<li><strong>AOSP \u7cfb\u7d71\u985e\u5225\u78ba\u5be6\u63db\u4e86<\/strong>\uff1a<code>Bitmap<\/code>\u3001<code>Paint<\/code>\u3001<code>Surface<\/code> \u78ba\u5be6\u5168\u9762\u9077\u79fb\u5230\u4e86 <code>Cleaner<\/code> \/ <code>NativeAllocationRegistry<\/code>\u3002<\/li>\n<li><strong>ART \u865b\u64ec\u6a5f\u4f9d\u7136\u4fdd\u7559\u76f8\u5bb9\u6a5f\u5236<\/strong>\uff1a<code>FinalizerDaemon<\/code>\uff08\u55ae\u57f7\u884c\u7dd2\u57f7\u884c\u4f47\u5217\uff09\u8207 <code>FinalizerWatchdogDaemon<\/code>\uff0810 \u79d2\u770b\u9580\u72d7\uff09\u4f9d\u7136\u5e38\u99d0\u5728\u80cc\u666f\u3002<\/li>\n<li><strong>\u6700\u81f4\u547d\u7684\u523a\u5ba2\uff1a\u7b2c\u4e09\u65b9\u51fd\u5f0f\u5eab\u8207\u820a JNI \u5c01\u88dd<\/strong>\uff1a\u8a31\u591a\u7b2c\u4e09\u65b9 C++ \u5c01\u88dd\u5eab\uff08\u4f8b\u5982\u67d0\u4e9b\u7248\u672c\u7684 OpenCV <code>Mat<\/code>\u3001\u820a\u7248\u76f8\u6a5f HAL \u5305\u88dd\u5c64\uff09\uff0c\u70ba\u4e86\u9632\u6b62\u958b\u767c\u8005\u5fd8\u8a18\u624b\u52d5\u91cb\u653e Native \u8a18\u61b6\u9ad4\uff0c\u5e95\u5c64\u4f9d\u7136\u5beb\u8457 <code>protected void finalize()<\/code> \u4f86\u7576\u4f5c\u5b89\u5168\u7db2\u3002<\/li>\n<\/ul>\n<p>\u5728\u4e00\u822c\u624b\u6a5f App \u4e0a\uff0c\u5076\u723e new \u5e7e\u500b Mat \u6839\u672c\u611f\u89ba\u4e0d\u51fa\u5dee\u5225\u3002<\/p>\n<p>\u4f46\u5728\u6211\u5011\u9019\u53f0\u6bcf\u79d2\u541e\u5410 30 \u5e40\u9ad8\u983b\u5f71\u50cf\u7684\u9580\u7981\u6a5f\u4e0a\uff0c\u6bcf\u79d2\u7522\u751f\u5e7e\u767e\u500b\u5e36\u6709 <code>finalize()<\/code> \u7684\u5f71\u50cf\u8207\u9ede\u96f2\u7269\u4ef6\u3002<strong>\u53ea\u8981 Class \u5ba3\u544a\u4e86 <code>finalize()<\/code>\uff0cAndroid 10 \u7684 ART \u5728 new \u7269\u4ef6\u6642\uff0c\u5c31\u6703\u7121\u689d\u4ef6\u70ba\u5b83\u5efa\u7acb\u4e00\u500b <code>FinalizerReference<\/code> \u585e\u9032\u4f47\u5217\uff01<\/strong><\/p>\n<hr>\n<h2>\u4e0d\u662f\u6d29\u6f0f\uff0c\u662f\u56b4\u91cd\u585e\u8eca<\/h2>\n<p>\u770b\u4e00\u773c\u6211\u5011\u4ee5\u524d\u81ea\u4ee5\u70ba\u8070\u660e\u7684\u820a\u7a0b\u5f0f\u78bc\uff0c\u4f60\u5c31\u77e5\u9053\u554f\u984c\u51fa\u5728\u54ea\uff1a<\/p>\n<pre><code class=\"hljs language-java\"><span class=\"hljs-comment\">\/\/ \u4ee5\u524d\u81ea\u4ee5\u70ba\u7701\u4e8b\u7684\u5beb\u6cd5\uff1a\u628a\u91cb\u653e\u8cac\u4efb\u63a8\u7d66 GC<\/span>\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">TofBufferWrapper<\/span> {\n    <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-type\">long<\/span> nativePtr; <span class=\"hljs-comment\">\/\/ \u6307\u5411 C++ \u5e7e\u5341 MB \u7684 16-bit \u9ede\u96f2<\/span>\n\n    <span class=\"hljs-type\">public<\/span> <span class=\"hljs-title function_\">TofBufferWrapper<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">long<\/span> ptr)<\/span> {\n        <span class=\"hljs-built_in\">this<\/span>.nativePtr = ptr;\n    }\n\n    <span class=\"hljs-comment\">\/\/ \u6bd2\u7624\u6240\u5728\uff1a\u8a66\u5716\u9760 finalize() \u5e6b\u6211\u81ea\u52d5\u547c\u53eb C++ free<\/span>\n    <span class=\"hljs-meta\">@Override<\/span>\n    <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-type\">void<\/span> <span class=\"hljs-title function_\">finalize<\/span><span class=\"hljs-params\">()<\/span> <span class=\"hljs-keyword\">throws<\/span> Throwable {\n        <span class=\"hljs-keyword\">try<\/span> {\n            <span class=\"hljs-keyword\">if<\/span> (nativePtr != <span class=\"hljs-number\">0<\/span>) {\n                nativeRelease(nativePtr); <span class=\"hljs-comment\">\/\/ JNI \u547c\u53eb C++ free<\/span>\n            }\n        } <span class=\"hljs-keyword\">finally<\/span> {\n            <span class=\"hljs-built_in\">super<\/span>.finalize();\n        }\n    }\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5728\u4f4e\u968e\u786c\u9ad4\u4e0a\u5f15\u7206\u4e86\u9023\u74b0\u8eca\u798d\uff1a<\/p>\n<ol>\n<li><strong>\u7368\u6728\u6a4b\u6548\u61c9<\/strong>\uff1a\u5168 App \u53ea\u6709\u552f\u4e00\u4e00\u689d\u57f7\u884c\u7dd2\uff08<code>FinalizerDaemon<\/code>\uff09\u8ca0\u8cac\u57f7\u884c\u6240\u6709\u7269\u4ef6\u7684 <code>finalize()<\/code>\u3002\u5728 1.3GHz \u7684\u7834 CPU \u4e0a\uff0c\u55ae\u4e00\u57f7\u884c\u7dd2\u6839\u672c\u6d88\u5316\u4e0d\u5b8c\u6bcf\u4e00\u5e40\u5e7e\u5341\u842c\u50cf\u7d20\u7684\u5716\u50cf\u6b98\u9ab8\u3002<\/li>\n<li><strong>\u8a18\u61b6\u9ad4\u6bad\u5c4d\u5316<\/strong>\uff1a\u7576\u4e00\u500b\u7269\u4ef6\u88ab GC \u5224\u5b9a\u4e0d\u53ef\u9054\u6642\uff0c\u5b83<strong>\u4e0d\u6703\u7576\u5834\u88ab\u91cb\u653e<\/strong>\uff0c\u800c\u662f\u88ab\u9001\u9032 Finalizer \u4f47\u5217\u6392\u968a\u7b49\u5f85\uff0c\u9019\u8b93\u5b83\u81f3\u5c11\u5f97\u5728\u8a18\u61b6\u9ad4\u88e1\u591a\u8cf4\u4e0a\u4e00\u6574\u8f2a GC \u9031\u671f\u3002\u7a7a\u9593\u6e05\u4e0d\u51fa\u4f86\uff0cJava Heap \u88ab\u4f54\u6eff\uff0cGC \u5c31\u66f4\u983b\u7e41\u89f8\u767c\uff0c\u76f4\u63a5\u5f15\u7206 GC Thrashing\u3002<\/li>\n<li><strong>\u96b1\u85cf\u7684 10 \u79d2\u6b7b\u5211<\/strong>\uff1a<code>FinalizerWatchdogDaemon<\/code> \u53ea\u8981\u767c\u73fe\u67d0\u500b\u7269\u4ef6\u7684 <code>finalize()<\/code> \u57f7\u884c\u8d85\u904e 10 \u79d2\uff08\u5728 CPU \u88ab AI \u8207 GC \u6436\u5149\u6642\u6975\u5bb9\u6613\u767c\u751f\uff09\uff0c\u770b\u9580\u72d7\u5c31\u6703\u76f4\u63a5\u5674\u51fa <code>TimeoutException<\/code>\uff0c\u7121\u60c5\u5730\u628a App \u6574\u500b\u884c\u7a0b\u69cd\u6583\u3002<\/li>\n<li><strong>GC \u76f2\u5340<\/strong>\uff1aJava \u7684 <code>TofBufferWrapper<\/code> \u53ea\u6709\u5e7e\u5341 bytes\uff0cJava Heap \u89ba\u5f97\u5929\u4e0b\u592a\u5e73\uff1b\u4f46\u5e95\u5c64 C++ \u65e9\u5c31\u5806\u4e86\u5e7e\u767e MB \u7684\u9ede\u96f2\uff0c\u76f4\u63a5\u89f8\u767c LMKD \u6bba\u884c\u7a0b\u3002<\/li>\n<\/ol>\n<hr>\n<h2>\u5fb9\u5e95\u62c6\u5f48\uff1a\u5f9e Finalize \u8d70\u5411\u986f\u5f0f\u751f\u547d\u9031\u671f<\/h2>\n<p>\u5fb9\u5e95\u6839\u6cbb GC Thrashing \u7684\u65b9\u6cd5\u53ea\u6709\u4e00\u500b\uff1a<strong>\u62d4\u6389\u6240\u6709 <code>finalize()<\/code>\uff0c\u56de\u6b78\u986f\u5f0f\u751f\u547d\u9031\u671f\u3002<\/strong><\/p>\n<ol>\n<li><strong>\u5be6\u4f5c <code>AutoCloseable<\/code><\/strong>\uff1a\u6240\u6709\u6301\u6709 Native \u8a18\u61b6\u9ad4\u7684\u985e\u5225\uff0c\u5168\u90e8\u5f37\u8feb\u5be6\u4f5c <code>close()<\/code>\uff0c\u914d\u5408 <code>try-with-resources<\/code> \u6216\u986f\u5f0f\u56de\u6536\u3002<\/li>\n<li><strong>\u5c0d\u6a19 <code>NativeAllocationRegistry<\/code><\/strong>\uff1a\u5982\u679c\u771f\u7684\u9700\u8981\u5b89\u5168\u7db2\uff0c\u6539\u7528 Android \u5b98\u65b9\u7684 <code>NativeAllocationRegistry<\/code> \u8a3b\u518a Cleaner\uff0c\u8b93\u91cb\u653e\u52d5\u4f5c\u8d70\u975e\u540c\u6b65\u8f15\u91cf Cleaner \u6a5f\u5236\uff0c\u907f\u958b <code>FinalizerDaemon<\/code> \u7684\u7368\u6728\u6a4b\u3002<\/li>\n<li><strong>\u7d42\u6975\u9435\u5f8b\uff1a\u7269\u4ef6\u6c60\u91cd\u7528<\/strong>\uff1a\u56de\u5230\u524d\u9762\u8aaa\u7684\u9435\u7897\u67b6\u69cb\uff0c\u9023 <code>new<\/code> \u90fd\u4e0d\u8981 <code>new<\/code>\uff0c\u5f9e\u6e90\u982d\u8b93 <code>FinalizerReference<\/code> \u7684\u751f\u7522\u7dda\u5fb9\u5e95\u505c\u5de5\u3002<\/li>\n<\/ol>\n<hr>\n<h2>\u6838\u5fc3\u6559\u8a13\u8207\u6536\u5de5<\/h2>\n<p>\u5728\u6bcf\u79d2 30 \u5e40\u7684\u5f71\u50cf\u7ba1\u7dda\u88e1\uff0c\u628a\u751f\u547d\u9031\u671f\u4ea4\u7d66 GC <code>finalize()<\/code> \u7684\u90a3\u4e00\u523b\uff0c\u4f60\u7684\u7cfb\u7d71\u5c31\u5df2\u7d93\u5beb\u597d\u4e86\u6b7b\u4ea1\u9810\u544a\u3002<\/p>\n<p>\u611f\u8b1d\u89c0\u770b\uff0c\u6536\u5de5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6211\u5011\u90a3\u53f0\u53ea\u6709 2GB RAM\u3001CPU \u6548\u80fd\u5b71\u5f31\u7684\u5d4c\u5165\u5f0f\u9580\u7981\u6a5f\u4e0a\u8dd1\u4eba\u81c9\u8fa8\u8b58\u6a21\u578b\uff0c\u958b\u767c\u8005\u7684\u65e5\u5e38\u5c31\u662f\u8ddf\u7cfb\u7d71\u6436\u8a18\u61b6\u9ad4\u3002<\/p>\n<p>\u4e00\u5207\u7684\u958b\u7aef\uff0c\u6e90\u81ea\u65bc\u4e00\u6bb5\u8b93\u6211\u5011\u6bdb\u9aa8\u609a\u7136\u7684 Log\u3002<\/p>\n<p>\u67d0\u5929\uff0c\u6211\u5011\u767c\u73fe AI \u8fa8\u8b58\u7cfb\u7d71\u5728\u9023\u7e8c\u904b\u884c\u5e7e\u5c0f\u6642\u5f8c\uff0c\u756b\u9762\u958b\u59cb\u56b4\u91cd\u6389\u5e40\u3001\u7cfb\u7d71\u6975\u5ea6\u5361\u9813\u3002\u6c92\u6709 OOM \u9583\u9000\uff0c\u4f46\u7576\u6211\u628a\u6a5f\u5668\u63a5\u4e0a adb \u6253\u958b logcat \u6642\uff0c\u756b\u9762\u6b63\u88ab\u9019\u7a2e\u65e5\u8a8c\u4ee5\u6bcf\u79d2\u6578\u5341\u884c\u7684\u901f\u5ea6\u760b\u72c2\u6d17\u983b\uff1a<\/p>\n","protected":false},"author":1,"featured_media":1240,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[166],"tags":[204,226,111,301],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/933"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=933"}],"version-history":[{"count":12,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/933\/revisions"}],"predecessor-version":[{"id":1595,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/933\/revisions\/1595"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/1240"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}