[[["容易理解","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,["# Low memory killers\n\nThe Android platform runs on the premise that free memory is wasted memory. Android\ntries to use all available memory at all times. For example, the system keeps\napps in memory after they've been closed so the user can quickly switch back to\nthem. For this reason, Android devices often run with very little free memory\n(see [Android Memory allocation here](/topic/performance/memory-management)).\n\nThe Android [low memory killer (LMK) daemon](https://source.android.com/docs/core/perf/lmkd)\nprocess monitors the memory state of a running Android system and reacts to\nhigh memory pressure by killing the least essential processes to keep the\nsystem performing at acceptable levels.\n\nTo decide which process to kill, the LMK daemon uses an *out of memory* score called\n[oom_adj_score](https://android.googlesource.com/platform/system/memory/lmkd/+/master/README.md)\nto [prioritize the running processes](/topic/performance/memory-management#low-memory_killer).\nProcesses with a high score are killed first. Background apps are killed first;\nsystem processes, last. The following table\nlists the LMK scoring categories from high to low. Items in the\nhighest-scoring category, in row one, are killed first:\n\n\u003cbr /\u003e\n\n**Figure 1.** Android processes, with high scores at the top and low scores at the bottom.\n\n\u003cbr /\u003e\n\nLMK metric on Android vitals\n----------------------------\n\nAndroid vitals can help you monitor and improve your app's LMK rate. Android\nvitals measures only one LMK rate: **User-perceived LMK rate**.\n\nThe metric reflects the percentage of your daily active users who experienced\nat least one user-perceived LMK. A user-perceived LMK is an LMK that is likely\nto have been noticed by the user. For example, LMKs that happen while your app\nis displaying an activity or running as a foreground service.\n\nYou can find the metric under the **Stability** section in Android vitals:\n**Figure 2.** Access **User-perceived LMK rate** in Android vitals.\n\nAs with similar to other core vital metrics, such as ANRs and crashes, you can filter\nthe metrics, compare your metrics with your peer, or monitor the metric's change for a\nlong period of time (up to 3 years). Data is available for existing apps\nstarting from 28 Jan 2025.\n**Figure 3.** Overview of LMK rate in Android Vitals.\n\nMemory profiling tools\n----------------------\n\nThe following tools can help you find and diagnose memory issues in the\nfollowing ways:\n\n- See how your app allocates memory over time. You can find a real-time graph of how much memory your app is using, the number of allocated Java objects, and when garbage collection occurs.\n- Initiate garbage collection events and take a snapshot of the Java heap while your app runs.\n- Record your app's memory allocations, inspect all allocated objects, and view the stack trace for each allocation.\n\nIf your game is running on a native engine, use\n[HWAddress Sanitizer](/ndk/guides/hwasan) to debug native memory allocation.\n\n### Android Studio Memory Profiler\n\nAndroid Studio provides the [Memory Profiler](/studio/profile/memory-profiler)\nas a component of the\n[Android Profiler](/studio/preview/features/android-profiler) which helps you\nidentify memory leaks and memory churn that can lead to stutter, freezes, and\neven app crashes. The profiler shows a real-time graph of your app's memory use\nand lets you capture a heap dump, force garbage collection, and track\nmemory allocation.\n**Figure 4.** Viewing global JNI references in Android Studio Memory Profiler.\n\n### Unity memory profiling tools\n\nIf you are using Unity Engine to build your apps, you can follow\n[Unity memory profiling guidance](https://unity.com/how-to/use-memory-profiling-unity).\nUnity offers two tools to analyze memory usage in your application in Unity.\n\nThe first is the [Memory Profiler module](https://docs.unity3d.com/Manual/ProfilerMemory.html),\nwhich is a built-in profiler that gives you basic information about where your application uses memory.\n**Figure 5.** Unity Profiler window with the **Memory** module selected.\n\nThe second tool is the [Memory Profiler package](https://docs.unity3d.com/Packages/com.unity.memoryprofiler@1.1/manual/index.html),\nwhich is a Unity package that you can add to your project. The package adds an\nadditional Memory Profiler window to the Unity Editor. The Memory Profiler enables you\nto analyze memory usage in your application in even more detail. You can store\nand compare snapshots to find memory leaks or see the memory layout to find\nmemory fragmentation issues.\n**Figure 6.** Analyzing a memory snapshot using the Memory Profiler window.\n\n### Unreal Memory Insights\n\nApps built by Unreal Engine can use\n[Unreal Memory Insights](https://dev.epicgames.com/documentation/en-us/unreal-engine/memory-insights-in-unreal-engine)\nto see detailed information about memory allocation and deallocation,\nincluding the Low Level Memory (LLM) tags and callstacks associated with\nblocks of memory.\n\nThe Memory Insights query system can find live allocations at any\npoint in time, identify changes in memory usage, locate memory leaks, and\ndifferentiate short-term from long-term allocations.\n\nFrom UE 5.4, Memory Insights supports memory tracing with callstacks for\nAndroid projects.\n**Figure 6.** Unreal's Memory Insights tracker.\n\nLow memory insights\n-------------------\n\nAndroid provides callbacks and APIs that enable you to trim your game's memory requirements\nand determine why previous game runs were terminated.\n\n### Callbacks\n\nDon't register for deprecated [trim](/reference/android/content/ComponentCallbacks2#summary)\ncallbacks. Android doesn't have any\nAPIs for detecting native memory pressure events when the system is running into memory limits.\nTrim callbacks haven't been helpful at preventing low-memory kills,\nso Android deprecated [all of them](/reference/android/content/ComponentCallbacks2#summary)\nother than [`TRIM_MEMORY_UI_HIDDEN`](/reference/android/content/ComponentCallbacks2#TRIM_MEMORY_UI_HIDDEN)\nand [`TRIM_MEMORY_BACKGROUND`](/reference/android/content/ComponentCallbacks2#TRIM_MEMORY_BACKGROUND).\n\n### Game termination\n\nAndroid tries to use all available memory to cache apps and games to ensure they\nload quickly (improving the user experience), but when memory becomes limited,\nthe system kills the most memory-intensive apps and games to free up memory for\nnormal device operation.\n\nInformation, insights, and best practices to help you achieve better game memory\nusage include the following:\n\n- Use [`ApplicationExitInfo`](/reference/android/app/ApplicationExitInfo): This API returns the reason why the previous game run was killed by the Android system. Use `ApplicationExitInfo`to check for low memory as a reason for a previous process run [death](/reference/android/app/ApplicationExitInfo#REASON_LOW_MEMORY). Check whether the game was killed due to low memory so the game can be optimized to use less memory on that device.\n- Look at total [physical RAM](/reference/android/app/ActivityManager.MemoryInfo#totalMem): To prevent games from being killed when in the foreground and to match the device's capabilities, look at the total physical RAM per device for fine granularity to adjust game memory usage based on that. If the goal is to prevent apps from being killed shortly after moving to the background (to allow the player to multitask), use the [trim](/reference/android/content/ComponentCallbacks2#constants_1) callbacks ([`TRIM_MEMORY_UI_HIDDEN`](/reference/android/content/ComponentCallbacks2#TRIM_MEMORY_UI_HIDDEN) specifically) to reduce your game memory usage.\n\nAdditional resources\n--------------------\n\n- [Overview of memory management](/topic/performance/memory-overview)"]]