自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
缓存的应用冻结器
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android 11 QPR3 或更高版本支持缓存的应用冻结器。此功能可停止执行缓存的进程,从而减少行为不当的应用(此类应用可能会试图在缓存期间运行)所使用的资源。框架通过将进程迁移到冻结的 cgroup 来冻结缓存的应用;这可以减少活跃缓存应用存在的活跃和空闲 CPU 消耗。应用冻结器可以使用系统配置标志或开发者选项来启用。
实现应用冻结器
缓存应用冻结器利用内核 cgroup v2 冻结器。搭载兼容内核的设备可以(选择性)启用该内核。为此,请启用“暂停执行已缓存的应用”开发者选项,或将设备配置标志 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
中的客户端代码进行实验。
处理自定义功能
通常情况下,进程在缓存时不应执行任何操作,但某些应用可能会有一些由预期在缓存期间运行的进程支持的自定义功能。在运行此类应用的设备上启用应用冻结器功能后,缓存进程会被冻结,并且可能会阻止自定义功能的正常运行。
下面是一个权宜解决方法:如果某个应用有进程需要在缓存期间执行 activity,请在该进程需要执行任何工作之前将进程状态更改为非缓存状态(例如绑定前台服务 [BFGS] 或前台状态),从而使应用保持活跃状态。
测试应用冻结器
如需验证应用冻结器是否按预期运行,请使用以下资源:
使用 adb shell dumpsys activity
命令检查冻结进程列表,并对 Apps frozen:
执行 grep 命令。
检查是否存在 /sys/fs/cgroup/uid_0/cgroup.freeze
文件。
查看 Logcat,它会在进程每次迁移进/出冻结器时展示冻结和未冻结的条目。例如:
adb logcat | grep -i "\(freezing\|froze\)"
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],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\\)\""]]