[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Reduce graphics memory consumption\n\nIn the graphics stack, a per-layer buffer cache sits between Composer HAL and\nSurfaceFlinger to reduce the overhead associated with sending file descriptors\nover IPC. Prior to Android 14, this buffer cache\nwasn't purged when a\n[`GraphicBufferProducer`](https://android.googlesource.com/platform/frameworks/native/+/android16-release/libs/gui/include/gui/IGraphicBufferProducer.h) disconnects from a SurfaceFlinger\n[`GraphicBufferConsumer`](https://android.googlesource.com/platform/frameworks/native/+/android16-release/libs/gui/include/gui/IGraphicBufferConsumer.h), such as when a\nMediaCodec is disconnected from a SurfaceView. Starting with Android\n14, you can forcefully purge this buffer cache to\nreduce graphics memory consumption.\n| **Note:** The benefits of this feature are most apparent when a user is starting and stopping videos within an app on a device with a high-resolution display and limited memory, such as a television.\n\nChoose from one of the two following options:\n\n- For devices launching with Android 14 and higher, you must implement the new Composer HAL API version 3.2. This option is activated by default and saves the most memory. Devices upgrading to 14 and later can also use this option to achieve full memory benefits.\n- For devices upgrading to Android 14 for which you don't want to implement Composer HAL 3.2 API, you can enable the backward-compatible option. This option saves almost as much memory as the previous option.\n\nThe following two sections explain how to implement each option.\n\nImplement the Composer HAL 3.2 API\n----------------------------------\n\nTo achieve full graphics buffer memory benefits, you must:\n\n1. Update your Composer HAL implementation to version 3.2.\n2. Process `LayerCommand::bufferSlotsToClear` by purging buffer cache entries indicated by the slot numbers found in the list.\n\nThe Composer HAL 3.2 APIs related to graphic buffer memory, including\n`LayerCommand:bufferSlotsToClear`, are in\n[`LayerCommand.aidl-`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/graphics/composer/aidl/android/hardware/graphics/composer3/LayerCommand.aidl).\n\nEnable the backward-compatible option\n-------------------------------------\n\nThe backward-compatible memory reduction option replaces a real buffer in\nthe cache slot with a 1x1 placeholder buffer, resulting in memory savings\nfor all purged slots, except for the current active buffer slot. To achieve\npartial memory saving benefits, enable the backward-compatible option by\nsetting the `surface_flinger.clear_slots_with_set_layer_buffer` sysprop to\n`true`. This sysprop is found in the\n[`property_contexts`](https://cs.android.com/android/platform/superproject/+/android-latest-release:system/sepolicy/private/property_contexts?q=%22surface_flinger.clear_slots_with_set_layer_buffer%22)\nfile.\n\nSetting this sysprop requires your Composer HAL implementation to correctly\nhandle multiple `setLayerBuffer` commands for the same layer in a single\npresent cycle.\n\nEnabling the backward-compatible option has the following affects:\n\n- For AIDL HALs: SurfaceFlinger sends multiple `LayerCommand` instances for a\n single layer, each with a single `BufferCommand`. Each `BufferCommand` contains a 1x1\n placeholder buffer handle and a slot number for the cache buffer slot that\n needs to be purged.\n\n- For HIDL HALs: SurfaceFlinger sends multiple `SELECT_DISPLAY`,\n `SELECT_LAYER`, `SET_BUFFER` commands. These commands contain a 1x1 placeholder\n buffer handle and a slot number for the cache buffer slot that needs to be\n purged.\n\nThe backward-compatible option might cause the Composer HAL to crash on some\ndevices. You might be able to modify your Composer HAL to solve this issue. The\ncode controlling this behavior is found here:\n\n- [SurfaceFlinger code for AIDL HALs](https://android.googlesource.com/platform/frameworks/native/+/refs/heads/android16-release/services/surfaceflinger/DisplayHardware/AidlComposerHal.cpp#854)\n\n- [SurfaceFlinger code for HIDL HALs](https://android.googlesource.com/platform/frameworks/native/+/refs/heads/android16-release/services/surfaceflinger/DisplayHardware/AidlComposerHal.cpp#718)\n\nTest graphics buffer cache memory consumption\n---------------------------------------------\n\nTests can't verify whether the cache slots are purged by HAL\nimplementations. However, you can use your debugging tools to monitor\ngraphic buffer usage. As you monitor, you should notice that there are fewer\nout-of-memory errors in scenarios where multiple different videos are stopped\nand started in quick succession on YouTube.\n\nVTS tests are available that verify that the HAL implementation is\nfunctionally capable of receiving the new API calls (HAL version 3.2+) or\nmultiple `setLayerBuffer` commands for the backward-compatible\nimplementation. However, this shouldn't be considered sufficient testing for\nproper functionality, as some devices pass these VTS tests,\nbut fail during real-world use cases.\n\nFor new VTS tests, navigate to the following links:\n\n- HIDL compatible: [`GraphicsComposerHidlCommandTest::SET_LAYER_BUFFER_multipleTimes`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/graphics/composer/2.1/vts/functional/VtsHalGraphicsComposerV2_1TargetTest.cpp#891)\n\n- AIDL 3.1 compatible: [`GraphicsComposerAidlCommandTest::SetLayerBufferMultipleTimes`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/graphics/composer/aidl/vts/VtsHalGraphicsComposer3_TargetTest.cpp#1817)\n\n- AIDL 3.2: [`GraphicsComposerAidlCommandV2Test::SetLayerBufferSlotsToClear`](https://android.googlesource.com/platform/hardware/interfaces/+/refs/heads/android16-release/graphics/composer/aidl/vts/VtsHalGraphicsComposer3_TargetTest.cpp#1770)"]]