{"id":453,"date":"2025-06-10T07:44:25","date_gmt":"2025-06-10T07:44:25","guid":{"rendered":"https:\/\/boochlin.com\/?p=453"},"modified":"2026-09-19T17:03:56","modified_gmt":"2026-09-19T17:03:56","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-%e6%98%af%e5%94%af%e4%b8%80%e6%9c%83%e9%97%9c%e5%bf%83%e4%bd%a0oom-%e7%9a%84%e5%a5%bd%e4%ba%ba","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=453","title":{"rendered":"Android \u721b\u88dd\u7f6e\u60f3\u8dd1\u4eba\u81c9\u8fa8\u8b585 \u2013 GC \u662f\u552f\u4e00\u6703\u95dc\u5fc3\u4f60 OOM \u7684\u597d\u4eba"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2025\/05\/ChatGPT-Image-May-22-2025-05_25_48-AM.png\" alt=\"\"><\/p>\n<p>\u524d\u5169\u7bc7\u6211\u5011\u50cf\u6ce5\u6c34\u5320\u4e00\u6a23\uff0c\u7528 ZRAM \u88dc\u6f0f\u6d1e\uff0c\u7528 LMKD \u7bc9\u9ad8\u7246\uff0c\u7d42\u65bc\u4fdd\u8b49\u4e86\u4eba\u81c9\u8fa8\u8b58\u4e3b\u7a0b\u5f0f\u4e0d\u6703\u88ab\u7cfb\u7d71\u5916\u90e8\u7684\u6b7b\u795e\u65ac\u6bba\u3002<\/p>\n<p>\u4f46\u5de5\u4f4d\u4e0a\u7684\u771f\u5be6\u6b98\u9177\u5728\u65bc\uff1a<strong>\u5916\u9762\u7684\u4eba\u6bba\u4e0d\u9032\u4f86\uff0c\u5167\u90e8\u7684\u4eba\u537b\u96a8\u6642\u80fd\u628a\u8239\u947f\u6c89\u3002<\/strong><\/p>\n<p>\u7576\u6211\u5011\u628a\u96d9\u76f8\u6a5f\uff08RGB 1080p + ToF 480p\uff09\u4e32\u6d41\u5168\u901f\u63a5\u4e0a\u4eba\u81c9\u8fa8\u8b58\u6f14\u7b97\u6cd5\u5f8c\uff0c\u771f\u6a5f\u8dd1\u4e86\u4e0d\u5230\u5341\u5206\u9418\uff0c\u756b\u9762\u958b\u59cb\u8089\u773c\u53ef\u898b\u5730\u5361\u9813\u3001\u6495\u88c2\uff0c\u539f\u672c\u9806\u66a2\u7684 30fps \u6389\u5230\u500b\u4f4d\u6578\u3002\u6293\u51fa Logcat \u4e00\u770b\uff0c\u87a2\u5e55\u88ab\u5bc6\u5bc6\u9ebb\u9ebb\u7684\u9ec3\u5b57\u6d17\u677f\uff1a<\/p>\n<pre><code class=\"hljs language-plaintext\">Explicit concurrent mark sweep GC freed 104857(3MB) AllocSpace objects, 0(0B) LOS objects, 39% free, 54MB\/90MB, paused 12.345ms total 45.678ms<\/code><\/pre>\n<p>\u5e73\u5747\u6bcf 200 \u6beb\u79d2\u89f8\u767c\u4e00\u6b21 GC\uff0c\u6bcf\u6b21\u66ab\u505c\uff08Pause\uff09\u5341\u5e7e\u6beb\u79d2\u3002\u76f8\u6a5f\u7684\u4e00\u5e40\u6642\u9593\u53ea\u6709 33.3ms\uff0cGC \u53ea\u8981\u505c\u9813\u500b\u5169\u6b21\uff0c\u76f8\u6a5f\u786c\u9ad4\u7de9\u885d\u5340\u7acb\u523b\u6ea2\u51fa\uff08Buffer Drop\uff09\uff0c\u6574\u500b\u756b\u9762\u7576\u5834\u5b9a\u683c\u3002<\/p>\n<p>\u5f88\u591a Android \u5de5\u7a0b\u5e2b\u5929\u5929\u62b1\u6028 GC \u505c\u9813\u5bb3\u6b7b\u4eba\u3002<br \/>\n\u4eca\u5929\u6211\u8981\u8aaa\u53e5\u5927\u5be6\u8a71\uff1a<strong>GC \u662f\u6574\u53f0\u6a5f\u5668\u88e1\u552f\u4e00\u95dc\u5fc3\u4f60\u6703\u4e0d\u6703 OOM \u7684\u597d\u4eba\u3002\u8981\u4e0d\u662f\u5b83\u5728\u5f8c\u9762\u62fc\u547d\u5e6b\u4f60\u64e6\u5c41\u80a1\uff0c\u4f60\u7684 App \u65e9\u5728\u958b\u6a5f\u7b2c\u4e09\u79d2\u5c31\u70b8\u6b7b\u4e86\u3002<\/strong><\/p>\n<p>\u554f\u984c\u5f9e\u4f86\u4e0d\u662f GC\uff0c\u800c\u662f\u4f60\u5728\u76f8\u6a5f\u7ba1\u7dda\u88e1\u760b\u72c2 new \u7269\u4ef6\u7684\u611a\u8822\u884c\u70ba\u3002<\/p>\n<hr>\n<h2>1. \u5147\u6848\u73fe\u5834\uff1a\u6bcf\u79d2 60 \u6b21\u7684 Bitmap \u6b7b\u4ea1\u72c2\u6b61<\/h2>\n<p>\u7ffb\u958b\u7576\u6642\u5718\u968a\u5beb\u7684\u76f8\u6a5f\u9810\u89bd\u56de\u547c\u4ee3\u78bc\uff0c\u6211\u5dee\u9ede\u628a\u525b\u559d\u7684\u62ff\u9435\u5674\u5728\u87a2\u5e55\u4e0a\uff1a<\/p>\n<pre><code class=\"hljs language-kotlin\"><span class=\"hljs-comment\">\/\/ \u72af\u7f6a\u73fe\u5834\uff1a\u6bcf\u79d2\u57f7\u884c 30 \u6b21\u7684\u76f8\u6a5f\u56de\u547c<\/span>\n<span class=\"hljs-keyword\">override<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">fun<\/span> <span class=\"hljs-title\">onImageAvailable<\/span><span class=\"hljs-params\">(reader: <span class=\"hljs-type\">ImageReader<\/span>)<\/span><\/span> {\n    <span class=\"hljs-keyword\">val<\/span> image = reader.acquireLatestImage() ?: <span class=\"hljs-keyword\">return<\/span>\n    \n    <span class=\"hljs-comment\">\/\/ \u7f6a\u884c\u4e00\uff1a\u6bcf\u5e40\u7533\u8acb\u4e00\u500b\u5e7e\u767e KB \u7684 byte \u9663\u5217<\/span>\n    <span class=\"hljs-keyword\">val<\/span> nv21Bytes = YuvUtils.imageToNv21(image)\n    \n    <span class=\"hljs-comment\">\/\/ \u7f6a\u884c\u4e8c\uff1a\u6bcf\u5e40\u5ba3\u544a\u4e00\u500b\u5168\u65b0\u7684 Bitmap \u5bb9\u5668\uff08\u72c2\u5403 6.2MB Java Heap\uff09<\/span>\n    <span class=\"hljs-keyword\">val<\/span> bitmap = BitmapFactory.decodeByteArray(nv21Bytes, <span class=\"hljs-number\">0<\/span>, nv21Bytes.size)\n    \n    <span class=\"hljs-comment\">\/\/ \u7f6a\u884c\u4e09\uff1a\u65cb\u8f49\u756b\u9762\u53c8 new \u4e00\u500b\u65b0 Bitmap\uff01<\/span>\n    <span class=\"hljs-keyword\">val<\/span> rotatedBitmap = Bitmap.createBitmap(bitmap, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, w, h, matrix, <span class=\"hljs-literal\">true<\/span>)\n    \n    faceDetector.process(rotatedBitmap)\n    image.close()\n}<\/code><\/pre>\n<p>\u6211\u5011\u4f86\u64a5\u4e00\u4e0b\u7b97\u76e4\uff1a<br \/>\n1080p RGB \u5f71\u50cf\uff0c\u8f49\u6210 ARGB_8888 \u7684 Bitmap\uff0c\u55ae\u5f35\u4f54\u7528\u8a18\u61b6\u9ad4\u662f <code>1920 * 1080 * 4 = 8.29 MB<\/code>\u3002<br \/>\n\u6bcf\u79d2\u8dd1 30 \u5e40\uff1a<br \/>\n<code>8.29 MB * 30 = 248.7 MB \/ \u79d2<\/code>\uff01<\/p>\n<p>\u4f60\u7684 Java \u865b\u64ec\u6a5f\u5806\u8a18\u61b6\u9ad4\u4e0a\u9650\uff08heapgrowthlimit\uff09\u53ea\u6709 192MB\u3002\u4f60\u5728\u4e00\u79d2\u4e4b\u5167\u5411\u7cfb\u7d71\u7d22\u8981 250MB \u7684\u65b0\u8a18\u61b6\u9ad4\uff0cART \u865b\u64ec\u6a5f\u70ba\u4e86\u4e0d\u8b93\u8a18\u61b6\u9ad4\u88ab\u6490\u7206\uff0c\u53ea\u80fd\u760b\u72c2\u767c\u52d5 <strong>Concurrent Mark Sweep (CMS)<\/strong>\uff0c\u628a\u6240\u6709\u57f7\u884c\u7dd2\u6309\u5728\u5730\u4e0a\u6469\u64e6\uff0c\u903c\u5927\u5bb6\u505c\u4e0b\u4f86\u7b49\u5b83\u6e05\u7406\u5783\u573e\u3002<\/p>\n<p>\u9019\u4e0d\u53eb\u6548\u80fd\u8abf\u6821\uff0c\u9019\u53eb\u4eba\u70ba\u88fd\u9020\u8a18\u61b6\u9ad4\u98a8\u66b4\u3002<\/p>\n<hr>\n<h2>2. \u9435\u7897\u54f2\u5b78\uff1aZero-Allocation\uff08\u96f6\u8a18\u61b6\u9ad4\u5206\u914d\uff09<\/h2>\n<p>\u5728\u4f4e\u968e\u6676\u7247\u7684\u9ad8\u983b\u5f71\u50cf\u7ba1\u7dda\u88e1\uff0c\u552f\u4e00\u7684\u751f\u5b58\u6cd5\u5247\u53eb\u505a <strong>\u300c\u9435\u7897\u539f\u5247\u300d<\/strong>\uff1a<br \/>\n<strong>\u958b\u5de5\u524d\u767c\u4f60\u4e00\u500b\u9435\u7897\uff0c\u5403\u98ef\u6d17\u7897\u90fd\u5728\u9019\u500b\u7897\u88e1\uff0c\u7d55\u4e0d\u8cb7\u514d\u6d17\u7d19\u7897\uff0c\u7528\u5b8c\u5c31\u4e1f\u3002<\/strong><\/p>\n<p>\u6574\u5957\u7ba1\u7dda\u5f9e\u76f8\u6a5f\u63a5\u6536\u3001\u683c\u5f0f\u8f49\u63db\u5230\u6f14\u7b97\u6cd5\u63a8\u8ad6\uff0c<strong>\u5728\u57f7\u884c\u671f\u9593\uff08Runtime\uff09\u8a18\u61b6\u9ad4\u5206\u914d\u5fc5\u9808\u7cbe\u78ba\u70ba\u96f6\uff08Zero-Allocation\uff09<\/strong>\uff01<\/p>\n<h3>1. \u9810\u5148\u5206\u914d\u55ae\u4e00 Bitmap\uff0c\u7528 <code>copyPixelsFromBuffer<\/code> \u539f\u5730\u91cd\u5beb<\/h3>\n<p>\u4e0d\u8981\u518d\u547c\u53eb <code>BitmapFactory.decode<\/code> \u6216 <code>Bitmap.createBitmap<\/code>\u3002\u5728\u7cfb\u7d71\u521d\u59cb\u5316\u6642\uff0c\u53ea\u5efa\u7acb\u552f\u4e00\u7684\u4e00\u500b\u5168\u57df\u8907\u7528 Bitmap\uff1a<\/p>\n<pre><code class=\"hljs language-kotlin\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">ZeroAllocRenderer<\/span>(<span class=\"hljs-keyword\">val<\/span> width: <span class=\"hljs-built_in\">Int<\/span>, <span class=\"hljs-keyword\">val<\/span> height: <span class=\"hljs-built_in\">Int<\/span>) {\n    <span class=\"hljs-comment\">\/\/ \u5168\u751f\u547d\u9031\u671f\u53ea\u6709\u9019\u4e00\u500b Bitmap\uff0c\u6c38\u9060\u4e0d\u88ab GC \u56de\u6536<\/span>\n    <span class=\"hljs-keyword\">val<\/span> reusableBitmap: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)\n    <span class=\"hljs-comment\">\/\/ \u9810\u5148\u914d\u7f6e\u9023\u7e8c Direct ByteBuffer\uff0c\u898f\u907f JVM Heap<\/span>\n    <span class=\"hljs-keyword\">val<\/span> directBuffer: ByteBuffer = ByteBuffer.allocateDirect(width * height * <span class=\"hljs-number\">4<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">fun<\/span> <span class=\"hljs-title\">updatePixels<\/span><span class=\"hljs-params\">(nativeRgbaPtr: <span class=\"hljs-type\">Long<\/span>)<\/span><\/span> {\n        directBuffer.clear()\n        <span class=\"hljs-comment\">\/\/ \u5e95\u5c64 JNI \u76f4\u63a5\u900f\u904e memcpy \u704c\u5165\u50cf\u7d20\u8cc7\u6599\uff0c\u96f6 JNI \u8de8\u908a\u754c\u8907\u88fd<\/span>\n        NativeBridge.copyFrameToBuffer(nativeRgbaPtr, directBuffer, width * height * <span class=\"hljs-number\">4<\/span>)\n        directBuffer.position(<span class=\"hljs-number\">0<\/span>)\n        <span class=\"hljs-comment\">\/\/ \u539f\u5730\u66ff\u63db\u50cf\u7d20\uff0c\u5b8c\u5168\u4e0d\u7522\u751f\u4efb\u4f55 Java \u81e8\u6642\u7269\u4ef6\uff01<\/span>\n        reusableBitmap.copyPixelsFromBuffer(directBuffer)\n    }\n}<\/code><\/pre>\n<h3>2. \u7269\u4ef6\u6c60\u8207 ByteBuffer \u5faa\u74b0\u501f\u9084<\/h3>\n<p>\u6f14\u7b97\u6cd5\u8f38\u51fa\u7684\u4eba\u81c9\u908a\u754c\u6846\uff08<code>Rect<\/code>\uff09\u3001\u7279\u5fb5\u503c\u9663\u5217\uff08<code>float[]<\/code>\uff09\uff0c\u5168\u9762\u4f7f\u7528\u975c\u614b\u7269\u4ef6\u6c60\uff08Object Pool\uff09\uff1a<\/p>\n<pre><code class=\"hljs language-kotlin\"><span class=\"hljs-comment\">\/\/ \u501f\u51fa\u8207\u6b78\u9084\u6a5f\u5236\uff1a\u7cfb\u7d71\u555f\u52d5\u6642\u9810\u5148\u6eab\u6c60\uff08Pre-warm\uff09\uff0c\u78ba\u4fdd\u57f7\u884c\u671f\u96f6\u5206\u914d<\/span>\n<span class=\"hljs-keyword\">val<\/span> faceBoxPool = Pools.SynchronizedPool&lt;FaceResult&gt;(<span class=\"hljs-number\">10<\/span>).apply {\n    <span class=\"hljs-keyword\">for<\/span> (i <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0<\/span> until <span class=\"hljs-number\">10<\/span>) {\n        release(FaceResult()) <span class=\"hljs-comment\">\/\/ \u9810\u5148\u6ce8\u6eff 10 \u500b\u9435\u7897<\/span>\n    }\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">fun<\/span> <span class=\"hljs-title\">processFrame<\/span><span class=\"hljs-params\">()<\/span><\/span> {\n    <span class=\"hljs-comment\">\/\/ \u5f9e\u6c60\u4e2d\u501f\u51fa\uff0c\u4fdd\u8b49\u547d\u4e2d\u65e2\u6709\u5be6\u4f8b<\/span>\n    <span class=\"hljs-keyword\">val<\/span> result = faceBoxPool.acquire() ?: <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-comment\">\/\/ \u6975\u9650\u4fdd\u8b77\uff1a\u6c60\u6eff\u5373\u964d\u7d1a\uff0c\u7d55\u4e0d\u81e8\u6642 new<\/span>\n    <span class=\"hljs-keyword\">try<\/span> {\n        algorithm.detect(result)\n        render(result)\n    } <span class=\"hljs-keyword\">finally<\/span> {\n        result.reset()\n        faceBoxPool.release(result) <span class=\"hljs-comment\">\/\/ \u6b78\u9084\u6c60\u5b50\uff0cGC \u9023\u5f71\u5b50\u90fd\u770b\u4e0d\u5230<\/span>\n    }\n}<\/code><\/pre>\n<hr>\n<h2>3. \u6230\u679c\uff1a\u5f9e\u98a8\u66b4\u5230\u6b7b\u6c34<\/h2>\n<p>\u91cd\u69cb\u5b8c\u6210\u5f8c\uff0c\u518d\u6b21\u6253\u958b Android Studio Profiler \u89c0\u5bdf\u8a18\u61b6\u9ad4\u76e3\u63a7\u66f2\u7dda\uff1a<\/p>\n<ul>\n<li><strong>Java Heap \u5206\u914d\u66f2\u7dda<\/strong>\uff1a\u5f9e\u539f\u672c\u92f8\u9f52\u72c0\u7684\u5287\u70c8\u5fc3\u96fb\u5716\uff08\u4e00\u79d2\u9707\u76ea\u5341\u5e7e\u6b21\uff09\uff0c\u8b8a\u6210\u4e86\u4e00\u689d<strong>\u7d55\u5c0d\u7b46\u76f4\u7684\u5e73\u76f4\u6b7b\u7dda<\/strong>\u3002<\/li>\n<li><strong>GC \u89f8\u767c\u983b\u7387<\/strong>\uff1a\u5f9e\u539f\u672c\u6bcf\u79d2 5 \u6b21\uff0c\u964d\u70ba<strong>\u9023\u7e8c\u904b\u4f5c 2 \u5c0f\u6642\uff0c0 \u6b21 GC \u505c\u9813<\/strong>\u3002<\/li>\n<li><strong>\u756b\u9762\u5e40\u7387<\/strong>\uff1a\u76f8\u6a5f 1080p \u4e32\u6d41\u7a69\u5b9a\u9396\u6b7b\u5728 <strong>30.0 fps<\/strong>\uff0c\u5f71\u683c\u6e32\u67d3\u5ef6\u9072\u5f9e 45ms \u9a5f\u964d\u81f3 4.2ms\u3002<\/li>\n<\/ul>\n<p>\u4e0d\u8981\u602a GC \u5361\u9813\uff0c\u5148\u770b\u81ea\u5df1\u6709\u6c92\u6709\u5728\u76f8\u6a5f\u9ad8\u983b\u7ba1\u7dda\u88e1\u760b\u72c2 new \u7269\u4ef6\u3002<\/p>\n<p>\u7576\u4f60\u505a\u5230\u4e86\u57f7\u884c\u671f Zero-Allocation\uff0cGC \u5c31\u5f9e\u4f60\u7684\u4e16\u754c\u88e1\u5fb9\u5e95\u9000\u5834\u4e86\u3002<\/p>\n<p>\u4e0b\u4e00\u7bc7\uff0c\u6211\u5011\u6478\u9032\u76f8\u6a5f\u8207\u786c\u9ad4\u7684\u66f4\u6df1\u6c34\u5340\uff1a<strong>\u4e09\u8def\u9ad8\u983b\u5f71\u50cf\u6d41\u96f6\u8907\u88fd\uff08Zero-Copy\uff09<\/strong>\u3002\u770b\u770b\u5982\u4f55\u5728\u76f8\u6a5f HAL3\u3001GPU \u7d0b\u7406\u8207 NDK \u6f14\u7b97\u6cd5\u4e4b\u9593\uff0c\u5229\u7528 Linux ION \u5be6\u9ad4\u6307\u6a19\u76f4\u63a5\u7a7f\u900f\uff0c\u5fb9\u5e95\u7d42\u7d50\u8a18\u61b6\u9ad4\u8907\u88fd\u7684\u983b\u5bec\u6d69\u52ab\u3002<\/p>\n<p>\u611f\u8b1d\u89c0\u770b\uff0c\u6536\u5de5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u5169\u7bc7\u6211\u5011\u50cf\u6ce5\u6c34\u5320\u4e00\u6a23\uff0c\u7528 ZRAM \u88dc\u6f0f\u6d1e\uff0c\u7528 LMKD \u7bc9\u9ad8\u7246\uff0c\u7d42\u65bc\u4fdd\u8b49\u4e86\u4eba\u81c9\u8fa8\u8b58\u4e3b\u7a0b\u5f0f\u4e0d\u6703\u88ab\u7cfb\u7d71\u5916\u90e8\u7684\u6b7b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1252,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[166],"tags":[225,111],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/453"}],"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=453"}],"version-history":[{"count":25,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions"}],"predecessor-version":[{"id":1531,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/453\/revisions\/1531"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/1252"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}