{"id":912,"date":"2026-06-17T15:45:52","date_gmt":"2026-06-17T15:45:52","guid":{"rendered":"https:\/\/boochlin.com\/?p=912"},"modified":"2026-06-27T17:11:26","modified_gmt":"2026-06-27T17:11:26","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%986-gc-%e5%84%aa%e5%8c%96%e6%9c%80%e5%be%8c%e4%b8%80%e5%93%a9%e8%b7%af","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=912","title":{"rendered":"Android \u721b\u88dd\u7f6e\u60f3\u8dd1\u4eba\u81c9\u8fa8\u8b586 &#8211; \u4e09\u8def\u9ad8\u983b\u5f71\u50cf\u6d41\uff0c\u4e09\u500d\u84cb\u4e9e\uff0c\u8d95\u5febgc"},"content":{"rendered":"\n<p>\u7576\u4f60\u5728\u4e00\u53f0\u53ea\u6709 2GB RAM\u3001CPU 1.3GHz \u7684\u7834\u677f\u5b50\u4e0a\uff0c\u88ab\u8981\u6c42\u540c\u6642\u8dd1\u6eff RGB\u3001\u7d05\u5916\u7dda\uff08IR\uff09\u8207 TOF \u6df1\u5ea6\u76f8\u6a5f\u300c\u4e09\u8def\u9ad8\u983b\u5f71\u50cf\u6d41\u300d\uff0c\u9084\u8981\u5373\u6642\u505a\u4eba\u81c9\u8fa8\u8b58\u9632\u507d\u6642\uff0c\u4e16\u754c\u7684\u904b\u4f5c\u898f\u5247\u5c31\u8b8a\u4e86\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u6211\u5011\u5728\u6975\u9650\u751f\u5b58\u4e2d\uff0c\u69a8\u4e7e\u7cfb\u7d71\u6548\u80fd\u3001\u628a GC \u58d3\u529b\u964d\u5230\u6700\u4f4e\u7684\u5e7e\u500b\u72e0\u62db\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-\u72e0\u62db\u4e00\u767c\u653e\u9435\u7897recycleablebuffer-\u6c60\">\u767c\u653e\u9435\u7897\uff08RecycleableBuffer \u6c60\uff09<\/h2>\n\n\n\n<p>\u76f8\u6a5f\u6bcf\u79d2\u5410\u51fa\u4f86\u7684\u5f71\u50cf\u8cc7\u6599\u8ddf\u5c71\u4e00\u6a23\u5927\u3002\u6bcf\u4e00\u5f35\u5716\u90fd\u53bb&nbsp;<code>new<\/code>&nbsp;\u65b0\u7a7a\u9593\uff08\u514d\u6d17\u9910\u5177\uff09\uff0c\u7cfb\u7d71\u5149\u662f\u64bf\u5783\u573e\u5c31\u6703\u5361\u6b7b\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u6cd5\uff1a\u5fb9\u5e95\u7981\u6b62\u514d\u6d17\u9910\u5177\uff0c\u624b\u523b\u8a18\u61b6\u9ad4\u6c60<\/strong>&nbsp;\u6211\u5011\u56fa\u5b9a&nbsp;<code>new<\/code>&nbsp;\u597d\u5e7e\u500b\u300c\u9435\u7897\u300d\uff08FrameData\uff09\u653e\u5230\u67b6\u4e0a\u3002\u88dd\u5b8c\u5f71\u50cf\u3001\u7b97\u5b8c\u4e4b\u5f8c\u4e0d\u4e1f\u68c4\uff0c\u800c\u662f\u91cd\u7f6e\u72c0\u614b\u5f8c\u653e\u56de\u67b6\u4e0a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u6c60\u5b50\u88e1\u56fa\u5b9a 5 \u500b\u9435\u7897\npublic FrameData acquire() {\n    FrameData data = pool.poll();\n    if (data == null) data = new FrameData(); \/\/ \u7897\u4e0d\u5920\u624d\u61c9\u6025 new \u4e00\u500b\u514d\u6d17\u7684\n    return data;\n}\npublic void release(FrameData data) {\n    data.reset();     \/\/ \u6d17\u7897\uff1a\u53ea\u91cd\u7f6e\u5c6c\u6027\uff0c\u4e0d\u91cb\u653e\u4f54\u5927\u7a7a\u9593\u7684 byte&#91;]\n    pool.offer(data); \/\/ \u653e\u56de\u67b6\u4e0a\u7e7c\u7e8c\u88dd\u4e0b\u4e00\u9813\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-\u72e0\u62db\u4e8c\u7269\u7406\u9694\u96e2bitmap-copy-\u7684\u7121\u5948\">\u7269\u7406\u9694\u96e2\uff08Bitmap Copy \u7684\u7121\u5948\uff09<\/h2>\n\n\n\n<p>\u6211\u5011\u96d6\u7136\u63d0\u5021\u4e0d\u7528\u514d\u6d17\u9910\u5177\uff0c\u4f46\u5c0d\u65bc\u76f8\u6a5f\u50b3\u4f86\u7684 RGB \u756b\u9762\uff0c\u6211\u5011\u5c45\u7136\u5077\u5077\u8907\u88fd\u4e86\u4e00\u4efd\uff0c\u786c\u751f\u751f\u5403\u4e86\u4e00\u6b21\u6548\u80fd\u8667\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u6cd5\uff1a\u70ba\u4e86\u5118\u901f\u6b78\u9084\u5171\u4eab\u7de9\u885d\u5340<\/strong>&nbsp;\u76f8\u6a5f\u5410\u51fa\u4f86\u7684\u8cc7\u6599\u662f\u5e95\u5c64\u7684\u300c\u96f6\u62f7\u8c9d\u5171\u4eab\u8a18\u61b6\u9ad4\u300d\u3002\u6162\u6162\u541e\u541e\u9001\u9032 AI \u88e1\u7b97\uff0c\u4e0d\u8d95\u5feb\u6b78\u9084\u7684\u8a71\uff0c\u76f8\u6a5f\u5e95\u5c64\u62ff\u4e0d\u5230\u7a7a\u7897\u5c31\u6703\u5361\u6b7b\u7f77\u5de5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void processFrame(Image image) {\n    Bitmap bitmap = imageToBitmap(image);\n    image.close(); \/\/ \ud83d\udea8 \u95dc\u9375\uff1a\u7b2c\u4e00\u6642\u9593\u6b78\u9084\u5171\u4eab Buffer \u8b93\u76f8\u6a5f\u7e7c\u7e8c\u5de5\u4f5c\n\n    \/\/ \u8907\u88fd\u4e00\u4efd\u4e7e\u6de8\u7684\u79c1\u6709\u526f\u672c\u7d66 AI\n    Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, false);\n    bitmap.recycle(); \/\/ \u56de\u6536\u8f49\u63db\u7528\u7684\u81e8\u6642 Bitmap\n\n    frameQueue.offer(copy); \/\/ \u53ea\u628a\u4e7e\u6de8\u7684\u526f\u672c\u9001\u9032\u53bb\uff0c\u907f\u514d\u7b97\u5230\u4e00\u534a\u88ab\u76f8\u6a5f\u5e95\u5c64\u8986\u84cb\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-\u72e0\u62db\u4e09\u7121\u60c5\u7684-drop-oldest\u96d9\u5c64\u80cc\u58d3\">\u7121\u60c5\u7684 Drop-Oldest\uff08\u96d9\u5c64\u80cc\u58d3\uff09<\/h2>\n\n\n\n<p>\u76f8\u6a5f\u4e00\u79d2\u585e\u7d66\u4f60 30 \u5f35\u5716\uff0c\u4f46\u4f60\u90a3\u53ef\u60b2\u7684 CPU \u4e00\u79d2\u53ea\u80fd\u6d88\u5316 6 \u5f35\u3002\u5716\u585e\u5728\u8a18\u61b6\u9ad4\u88e1\u6392\u968a\uff0c\u4e0d\u7528 10 \u79d2\u6a5f\u5668\u5c31\u6703\u6490\u7206\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u6cd5\uff1a\u786c\u9ad4\u8207\u8edf\u9ad4\u7684\u96d9\u5c64\u6b98\u9177\u6dd8\u6c70<\/strong>&nbsp;\u5be7\u53ef\u6d41\u5931\u5ba2\u4eba\uff0c\u4e5f\u7d55\u5c0d\u4e0d\u8b93\u5e97\u88ab\u64e0\u7206\u3002AI \u7d55\u5c0d\u4e0d\u80fd\u53bb\u7b97\u300c\u904e\u671f\u7684\u820a\u756b\u9762\u300d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u7b2c\u4e00\u5c64\uff1a\u786c\u9ad4\u7d1a\u4e1f\u5e40\u3002\u6388\u6b0a\u76f8\u6a5f\u5e95\u5c64\u76f4\u63a5\u6254\u6389\u820a\u5e40\uff0cJava \u7aef\u9023\u770b\u90fd\u4e0d\u770b\nImage image = reader.acquireLatestImage(); \n\n\/\/ \u7b2c\u4e8c\u5c64\uff1a\u8edf\u9ad4\u4f47\u5217\u3002\u9580\u53e3\u53ea\u653e 3 \u5f35\u6905\u5b50\nprivate static final int FD_QUEUE_MAX_SIZE = 3;\n\nprivate void enqueueFrame(FrameData newFrame) {\n    synchronized (frameQueue) {\n        \/\/ \u5982\u679c\u6eff\u5ea7\u4e86\uff0c\u628a\u5750\u6700\u4e45\u7684\u4eba\u62d6\u51fa\u4f86\u8e39\u51fa\u53bb\n        while (frameQueue.size() >= FD_QUEUE_MAX_SIZE) {\n            FrameData oldest = frameQueue.removeFirst();\n            oldest.recycle();              \/\/ \u6d17\u7897\n            frameDataPool.release(oldest); \/\/ \u653e\u56de\u9435\u7897\u67b6\uff0c\u5f62\u6210\u96f6 GC \u9589\u74b0\n        }\n        frameQueue.addLast(newFrame);\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-\u72e0\u62db\u56db\u6f0f\u6597\u5f0f\u6dd8\u6c70\u7b2c\u96f6\u95dc\u8207\u56db\u5927\u60e1\u9738\">\u6f0f\u6597\u5f0f\u6dd8\u6c70\uff08\u7b2c\u96f6\u95dc\u8207\u56db\u5927\u60e1\u9738\uff09<\/h2>\n\n\n\n<p>\u9023\u8def\u908a\u98db\u904e\u53bb\u7684\u84bc\u8805\u90fd\u8981\u7121\u8166\u555f\u52d5 CNN \u6a21\u578b\u53bb\u7b97\uff1f\u9019\u53f0\u7834\u6a5f\u5668\u7684 CPU \u6703 24 \u5c0f\u6642\u6eff\u8f09\uff0c\u7136\u5f8c\u904e\u71b1\u964d\u983b\u3001\u96fb\u6c60\u81a8\u8139\u70b8\u88c2\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u6cd5\uff1a\u6975\u9650\u904e\u6ffe\u7684\u6f0f\u6597\u6a5f\u5236<\/strong>&nbsp;90% \u7684\u6642\u9593\u5927\u9580\u53e3\u662f\u6c92\u4eba\u7684\uff0c\u90a3\u500b\u6700\u8017\u96fb\u7684 200ms \u5927\u50a2\u4f19\u5e7e\u4e4e\u90fd\u5728\u7761\u89ba\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void processFrame(FrameData frameData) {\n    \/\/ \u7b2c\u96f6\u95dc\uff1a\u6642\u9593\u540c\u6b65\u5bb9\u5dee 50ms\u3002RGB\/IR\/Depth \u4e0d\u540c\u6b65\u7b97\u51fa\u4f86\u4e5f\u662f\u5783\u573e\n    if (!trySync()) return;\n\n    \/\/ \u7b2c\u4e00\u95dc\uff1a\u81c9\u90e8\u5075\u6e2c (~5ms)\n    List&lt;FaceRect> faces = faceDetector.detect(frameData.getRgbBitmap());\n    if (faces.isEmpty()) {\n        frameData.recycle();\n        return; \/\/ \u6c92\u81c9\uff1f\u76f4\u63a5\u6efe\n    }\n\n    for (FaceRect face : faces) {\n        \/\/ \u7b2c\u4e8c\u95dc\uff1a\u6a21\u7cca\u6aa2\u67e5 (~1ms)\n        if (calculateBlurScore(face) &lt; BLUR_THRESHOLD) continue;\n\n        \/\/ \u7b2c\u4e09\u95dc\uff1a\u6d3b\u9ad4\u9632\u507d (~15ms)\uff0c\u9760\u7d05\u5916\u7dda\u8207\u6df1\u5ea6\u5716\n        if (antiSpoofing.predict(face) &lt; LIVENESS_THRESHOLD) continue;\n\n        \/\/ \u7b2c\u56db\u95dc\uff1aCNN \u795e\u7d93\u7db2\u8def\u7279\u5fb5\u64f7\u53d6 (200ms+)\n        \/\/ \u78ba\u5b9a\u662f\u500b\u7ad9\u5b9a\u4e0d\u52d5\u7684\u6d3b\u4eba\uff0c\u624d\u51c6\u9032\u5927\u5ef3\u5439\u51b7\u6c23\n        float&#91;] embedding = faceRecognition.extractFeature(face);\n        \/\/ ...\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"user-content-\u72e0\u62db\u4e94\u907f\u958b-java-\u8f49\u63db\u7684-gc-\u70b8\u5f48\">\u907f\u958b Java \u8f49\u63db\u7684 GC \u70b8\u5f48<\/h2>\n\n\n\n<p>\u8655\u7406\u76f8\u6a5f YUV \u683c\u5f0f\u6642\uff0c\u5f88\u591a\u958b\u767c\u8005\u6703\u9806\u624b\u7528 Android \u5167\u5efa\u7684 API\u3002\u5728\u6211\u5011\u7684\u677f\u5b50\u4e0a\uff0c\u9019\u6839\u672c\u662f\u760b\u72c2\u88fd\u9020 GC \u98df\u7269\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u6cd5\uff1aNative \u96f6\u4e2d\u9593\u7269\u4ef6\u76f4\u8f49<\/strong>&nbsp;\u4e00\u500b\u4e2d\u9593 Java \u7269\u4ef6\u90fd\u4e0d\u7522\u751f\uff0c\u5fb9\u5e95\u6390\u6b7b GC \u89f8\u767c\u7684\u6a5f\u6703\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u274c \u6162\u901f\u8def\u5f91\uff1a\u7522\u751f 3 \u500b GC \u70b8\u5f48 (YuvImage, ByteArrayOutputStream, byte&#91;])\nYuvImage yuvImage = new YuvImage(yuvData, ImageFormat.NV21, width, height, null);\nByteArrayOutputStream out = new ByteArrayOutputStream();\nyuvImage.compressToJpeg(...);\nbyte&#91;] jpegBytes = out.toByteArray();\nreturn BitmapFactory.decodeByteArray(jpegBytes, 0, jpegBytes.length);\n\n\/\/ \u2705 \u9ad8\u901f\u8def\u5f91\uff1aJNI \u76f4\u8f49\uff0c\u76f4\u63a5\u628a\u7d50\u679c\u585e\u9032\u9810\u5148\u6e96\u5099\u597d\u7684 rgba \u9663\u5217\uff0c\u96f6\u4e2d\u9593\u7269\u4ef6\npublic static native void nativeNV21ToRGBA(byte&#91;] nv21, int width, int height, int&#91;] rgba);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Android \u8655\u7406\u76f8\u6a5f\u4e32\u6d41\uff0c\u76f8\u6a5f\u6bcf\u79d2\u5410\u51fa\u4f86\u7684\u5f71\u50cf\u8cc7\u6599\u8ddf\u5c71\u4e00\u6a23\u5927\u3002<\/p>\n<p>\u5982\u679c\u4f60\u6bcf\u4e00\u5f35\u5716\u90fd\u53bb\u00a0new\u00a0\u4e00\u500b\u65b0\u7a7a\u9593\uff08\u5c31\u50cf\u4f7f\u7528\u514d\u6d17\u9910\u5177\uff09\uff0c\u7528\u5b8c\u5c31\u4e1f\u7d66\u7cfb\u7d71\u56de\u6536\uff0c\u7cfb\u7d71\u5149\u662f\u300c\u8ddf\u5728\u5f8c\u9762\u64bf\u5783\u573e (GC)\u300d\u5c31\u6703\u5361\u5230\u61f7\u7591\u4eba\u751f\uff1a\u4e0a\u4e00\u79d2\u958b\u9580\u5f88\u9806\uff0c\u4e0b\u4e00\u79d2\u64bf\u500b\u5783\u573e\u5c31\u5361\u4e86 800ms\u3002\u6700\u5f8c\u7cfb\u7d71\u6703\u76f4\u63a5\u56e0\u70ba\u5783\u573e\u5806\u6eff (OOM) \u800c\u9583\u9000\u3002<\/p>\n","protected":false},"author":1,"featured_media":929,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,3,80],"tags":[],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/912"}],"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=912"}],"version-history":[{"count":9,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/912\/revisions"}],"predecessor-version":[{"id":942,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/912\/revisions\/942"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/media\/929"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=912"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=912"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=912"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}