The Memory Advice API beta is now deprecated, and no longer recommended for use.
Memory Advice API 概览
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Memory Advice API 是一个实验性原生 API,可帮助 Android 应用满足内存用量的安全限制。为此,该 API 会估算正在使用的内存资源量,并在超出特定阈值时通知应用,还可以直接向应用报告估算的内存使用量百分比。
该 API 提供的估算结果有助于确定应用应何时调整其内存用量,或为应用的未来版本寻找改进空间。调整可能包括任何会更改内存占用的内容,例如降低视觉和音频资源的细节和质量。
估算的内存资源包括由 malloc 分配的原生堆内存,以及由 OpenGL ES API 和 Vulkan Graphics API 分配的图形内存,使得 Memory Advice API 非常适合游戏和图形密集型应用。
估算依据如下:
- API 收集的设备指标
- 来自设备的机器学习数据
- 设备测试
在 API 级别,Memory Advice API 会定义一组内存建议状态,并通过一组函数将这些状态传递给应用。
使用入门
适用于 Unity 游戏的使用入门
分发情况
Memory Advice API 是 Android Game Development Kit (AGDK) 的一部分,并通过以下渠道分发:
Android Games 应使用为其构建环境推荐的 Memory Advice API 版本。对于 Android Studio,我们建议使用 Jetpack 版本。对于 Android Game Development Extension (AGDE),我们建议使用 AGDK 下载页面上的版本。
功能
本部分介绍了 Memory Advice API 的功能。
内存状态
该 API 会向应用报告以下内存状态:
- MEMORYADVICE_STATE_OK
- MEMORYADVICE_STATE_APPROACHING_LIMIT
- MEMORYADVICE_STATE_CRITICAL
如需了解详情,请参阅 memoryadvice_memorystate 参考页面。
报告内存状态
该 API 支持通过轮询或回调机制报告内存状态:
代价和调用频率注意事项
Memory Advice API 使用计算时间来生成内存状态。代价因设备而异,但通常每次调用持续 1 毫秒到 3 毫秒。在确定轮询内存状态的频率或为内存状态回调指定何种频率时,您应考虑此开销。
要求
设备要求
- Android 4.4(API 级别 19)或更高版本。
应用要求
NDK 版本要求
Memory Advice API 版本支持大多数常见的 NDK 版本。请参阅版本说明,查看受支持的 NDK 列表。
其他资源
问题和反馈
Memory Advice API 目前处于 Beta 版阶段,欢迎提供反馈。如果您有任何建议或在使用该 API 时发现了任何问题,请在 IssueTracker 中创建问题。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-26。
[null,null,["最后更新时间 (UTC):2025-08-26。"],[],[],null,["# Memory Advice API overview\n\nThe Memory Advice API is an experimental native API that\nhelps Android apps stay within safety limits for memory use. The API\nachieves this by estimating the amount of memory resources that are in use, and\nthen notifying the app when certain thresholds are exceeded. The API can\nalso report the estimated percentage of memory use directly to your app.\n| **Note:** You can use the Memory Advice API as an alternative to [`onTrimMemory`](/reference/android/content/ComponentCallbacks2#onTrimMemory(int)) events for the purpose of memory management.\n\nThe estimations provided by the API help to determine when your app should\nadjust its memory use, or to find areas to improve in future versions of your\napp. The adjustments could include anything that changes memory consumption, such\nas lowering the details and quality of visual and audio assets.\n\nThe estimated memory resources include native heap memory allocated by malloc,\nand graphics memory allocated by the OpenGL ES and Vulkan Graphics APIs making\nthe Memory Advice API ideal for games and graphic intensive apps.\n\nThe estimates are made based on the following:\n\n- Device metrics collected by the API\n- Machine learning data from devices\n- Device testing\n\nAt the API level, the Memory Advice API defines a set of memory\nadvice states and communicates them to apps with a set of functions.\n\n[Get Started](/games/sdk/memory-advice/start)\n[Get Started for Unity games](/games/engines/unity/memory-advice)\n\nDistributions\n-------------\n\nThe Memory Advice API is a part of the Android Games Development\nKit (AGDK) and is distributed through the following channels:\n\n- Binary releases in the [Android Games Jetpack libraries](/jetpack/androidx/releases/games).\n- Binary releases on the [AGDK download page](/games/agdk/get-agdk#agdk-libraries).\n- Source code on [AOSP](https://android.googlesource.com/platform/frameworks/opt/gamesdk/+/refs/heads/master/test/memoryadvice).\n\nGames should use the Memory Advice API release that is\nrecommended for their build environment. For Android Studio, we recommend the\nJetpack release. For the [Android Game Development Extension](/games/agde)\n(AGDE), we recommend the release on the AGDK download page.\n\nFeatures\n--------\n\nThis section describes the Memory Advice API features.\n\n### Memory state\n\nThe API reports the following memory states to apps:\n\n- MEMORYADVICE_STATE_OK\n- MEMORYADVICE_STATE_APPROACHING_LIMIT\n- MEMORYADVICE_STATE_CRITICAL\n\nFor details, see the [memoryadvice_memorystate](/reference/games/memory-advice/group/memory-advice#memoryadvice_memorystate) reference pages.\n\n### Reporting the memory state\n\nThe API supports reporting the memory state through either a polling or a callback mechanism:\n\n- **Polling** : \n\n An app can call the API at any time to retrieve the memory state.\n\n- **Callback** : \n\n An app can register a callback function with the Memory Advice API.\n The app specifies the calling frequency for the callback. The API\n uses the frequency to determine when to execute the callback when the application memory\n condition is approaching the safe limit or is critically low. The callback is not invoked\n if the memory condition is safe. The callback is executed from its own thread,\n not from a thread belonging to the app.\n\n### Cost and calling frequency considerations\n\nThe Memory Advice API uses computational time to generate\nthe memory status. The cost varies by device, but is usually between 1ms and 3ms\nper call. You should consider this overhead when determining how often\nto poll the memory state, or what frequency to specify for the memory state callback.\n\nRequirements\n------------\n\n**Device requirements**\n\n- Android 4.4 (API level 19) or higher.\n\n**App requirements**\n\n- The API only supports native apps (apps primarily written in C/C++).\n\n- The API only supports physical devices; emulators are not supported.\n\n**NDK version requirements**\n\nThe Memory Advice API release supports most popular NDK versions,\nrefer to the [release notes](https://developer.android.com/jetpack/androidx/releases/games)\nfor the supported NDK list.\n| **Note:** Addition versions of the NDK are supported by [the full AGDK library](/games/agdk/get-agdk#agdk-libraries).\n\nAdditional Resources\n--------------------\n\n- [Memory Advice API release notes](https://developer.android.com/jetpack/androidx/releases/games)\n- [Memory Advice API reference documentation](/reference/games/memory-advice/group/memory-advice)\n- [Memory Advice sample on Github](https://github.com/android/games-samples/blob/main/agdk/agdktunnel/app/src/main/cpp/memory_consumer.cpp#L168)\n- [Memory Advice API source code](https://android.googlesource.com/platform/frameworks/opt/gamesdk/+/refs/heads/master/games-memory-advice/)\n\nIssues and Feedback\n-------------------\n\nThe Memory Advice API is in beta and welcomes feedback, if you have suggestions or\nfind issues while using the API, [create an issue in IssueTracker](https://issuetracker.google.com/issues/new?component=897320&template=1456805)."]]