The Memory Advice API beta is now deprecated, and no longer recommended for use.
Memory Advice API 總覽
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Memory Advice API 是一項實驗性的原生 API,可協助 Android 應用程式保持在記憶體用量安全限制內。為了達到這個目標,API 會預估使用中的記憶體資源數量,然後在超過特定門檻時通知應用程式。API 也能直接向應用程式回報預估的記憶體用量百分比。
此 API 提供的估計資訊可協助判斷應用程式何時應調整記憶體用量,或找出應用程式日後版本可改善的地方。調整內容包括任何會改變記憶體用量的項目,例如降低視覺和音訊資產的細緻程度和品質。
預估記憶體資源包含 Malloc 配置的原生堆積記憶體,以及 OpenGL ES 和 Vulkan Graphics API 分配的圖形記憶體,讓 Memory Advice API 非常適合用於遊戲和大量使用圖像的應用程式。
預估值是依據以下項目計算得出:
- API 收集的裝置指標
- 裝置的機器學習資料
- 裝置測試
在 API 層級,Memory Advice API 會定義一組記憶體建議狀態,並使用一組函式將這些狀態傳遞給應用程式。
開始使用
開始在 Unity 遊戲環境使用
發布情況
Memory Advice API 是 Android Game Development Kit (AGDK) 的一部分,會透過下列管道發布:
遊戲使用的 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 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-26 (世界標準時間)。
[[["容易理解","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-08-26 (世界標準時間)。"],[],[],null,["| **Note:** The Memory Advice API Beta is now over and the library is deprecated. [learn more about alternative memory management approaches](/games/optimize/memory-allocation).\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\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\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\nThis section describes the Memory Advice API features.\n\nMemory 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\nReporting 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\nCost 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**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- [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\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)."]]