自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
快取應用程式凍結器
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 11 QPR3 以上版本支援快取應用程式凍結器。這項功能會停止執行快取的程序,並減少可能會在快取時嘗試運作的異常應用程式所使用的資源。此架構會將快取應用程式的程序遷移至已凍結的 cgroup,藉此減少在有活躍快取應用程式時的 CPU 使用率。您可以使用系統設定標記或開發人員選項啟用應用程式凍結器。
實作應用程式冷凍庫
快取應用程式凍結器會運用核心 cgroup v2 凍結器。裝置出貨時隨附相容的核心,可 (視情況) 啟用此功能。如要這麼做,請啟用開發人員選項「Suspend execution for cached apps」(暫停快取應用程式的執行),或將裝置設定旗標 activity_manager_native_boot use_freezer
設為 true
。例如:
adb shell device_config put activity_manager_native_boot use_freezer true && adb reboot
如果旗標 use_freezer
設為 false 或開發人員選項已停用,系統就會停用凍結器。您可以變更軟體版本或更新中的裝置設定,切換這項設定。
應用程式凍結器不會公開官方 API (也沒有參考實作用戶端),但會使用隱藏的系統 API setProcessFrozen
和 enableFreezer
。如要進一步瞭解這些 API,請參閱 ActivityManager 的 Process.java
和 ActivityManagerService API。開發人員也可以參考 ActivityManager
中的用戶端程式碼進行實驗。
處理自訂功能
一般來說,程序在快取時不會執行任何工作,但某些應用程式可能會提供自訂功能,並由快取時執行的程序支援這些功能。在執行這類應用程式的裝置上啟用應用程式凍結器時,快取的程序會遭到凍結,可能會導致自訂功能無法運作。
解決方法:如果應用程式有需要在快取時執行活動的程序,請在程序需要執行任何工作以允許應用程式保持活動狀態之前,將程序狀態變更為非快取狀態 (例如 Bound ForeGround Service (BFGS) 或前景)。
測試應用程式凍結器
如要確認應用程式凍結器是否正常運作,請使用下列資源:
使用 adb shell dumpsys activity
指令檢查已凍結的程序清單,並使用 grep 尋找 Apps frozen:
。
檢查 /sys/fs/cgroup/uid_0/cgroup.freeze
檔案是否存在。
查看 Logcat,每次程序從冷凍庫遷移進出時,這會顯示已凍結和未凍結的項目。例如:
adb logcat | grep -i "\(freezing\|froze\)"
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","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 (世界標準時間)。"],[],[],null,["# Cached apps freezer\n\nAndroid 11 QPR3 or higher supports the cached apps\nfreezer. This feature stops execution for cached processes and reduces resource\nusage by misbehaving apps that might attempt to operate while cached. The\nframework freezes cached apps by migrating their processes into a frozen cgroup;\nthis reduces active and idle CPU consumption in presence of active cached apps.\nThe app freezer can be enabled using a system configuration flag or a developer\noption.\n\nImplement the apps freezer\n--------------------------\n\nThe cached apps freezer leverages the kernel cgroup v2 freezer. Devices shipping\nwith a compatible kernel can (optionally) enable it. To do so, enable the\ndeveloper option \"Suspend execution for cached apps\" or set the device config\nflag `activity_manager_native_boot use_freezer` to `true`. For example: \n\n adb shell device_config put activity_manager_native_boot use_freezer true && adb reboot\n\nThe freezer is disabled when the flag `use_freezer` is set to false or the\ndeveloper option is disabled. You can toggle this setting by changing a device\nconfiguration in a software release or update.\n\nThe apps freezer doesn't expose official APIs (and doesn't have a reference\nimplementation client), but does use the hidden system APIs `setProcessFrozen`\nand `enableFreezer`. For details on these APIs, refer to ActivityManager's\n`Process.java` and the ActivityManagerService API. Developers can also refer to\nthe client code in `ActivityManager` for experiments.\n\nHandle custom features\n----------------------\n\nTypically, processes aren't expected to do any work when cached, but some apps\nmight have custom features supported by processes that are expected to run while\ncached. When the apps freezer is enabled on a device running such an app, the\ncached processes are frozen and might prevent custom features from working.\n\nAs a workaround, if an app has a process that needs to perform activities while\ncached, change the process status to non-cached (such as Bound ForeGround\nService (BFGS) or foreground) before the process needs to do any work to allow\nthe app to remain active.\n\nTest the apps freezer\n---------------------\n\nTo verify the app freezer is working as intended, use the following resources:\n\n- Check for a list of frozen processes using the `adb shell dumpsys activity`\n command and grep for `Apps frozen:`.\n\n- Check for the presence of the `/sys/fs/cgroup/uid_0/cgroup.freeze` file.\n\n- View logcat, which shows frozen and unfrozen entries each time a process\n migrates in or out of the freezer. For example:\n\n adb logcat | grep -i \"\\(freezing\\|froze\\)\""]]