Added in API level 14

ComponentCallbacks2


interface ComponentCallbacks2 : ComponentCallbacks
android.content.ComponentCallbacks2

Extended ComponentCallbacks interface with a new callback for finer-grained memory management. This interface is available in all application components (android.app.Activity, android.app.Service, ContentProvider, and android.app.Application).

You should implement onTrimMemory to incrementally release memory based on current system constraints. Using this callback to release your resources helps provide a more responsive system overall, but also directly benefits the user experience for your app by allowing the system to keep your process alive longer. That is, if you don't trim your resources based on memory levels defined by this callback, the system is more likely to kill your process while it is cached in the least-recently used (LRU) list, thus requiring your app to restart and restore all state when the user returns to it.

The values provided by onTrimMemory do not represent a single linear progression of memory limits, but provide you different types of clues about memory availability:

  • When your app is running:
    1. TRIM_MEMORY_RUNNING_MODERATE
      The device is beginning to run low on memory. Your app is running and not killable.
    2. TRIM_MEMORY_RUNNING_LOW
      The device is running much lower on memory. Your app is running and not killable, but please release unused resources to improve system performance (which directly impacts your app's performance).
    3. TRIM_MEMORY_RUNNING_CRITICAL
      The device is running extremely low on memory. Your app is not yet considered a killable process, but the system will begin killing background processes if apps do not release resources, so you should release non-critical resources now to prevent performance degradation.
  • When your app's visibility changes:
    1. TRIM_MEMORY_UI_HIDDEN
      Your app's UI is no longer visible, so this is a good time to release large resources that are used only by your UI.
  • When your app's process resides in the background LRU list:
    1. TRIM_MEMORY_BACKGROUND
      The system is running low on memory and your process is near the beginning of the LRU list. Although your app process is not at a high risk of being killed, the system may already be killing processes in the LRU list, so you should release resources that are easy to recover so your process will remain in the list and resume quickly when the user returns to your app.
    2. TRIM_MEMORY_MODERATE
      The system is running low on memory and your process is near the middle of the LRU list. If the system becomes further constrained for memory, there's a chance your process will be killed.
    3. TRIM_MEMORY_COMPLETE
      The system is running low on memory and your process is one of the first to be killed if the system does not recover memory now. You should release absolutely everything that's not critical to resuming your app state.

      To support API levels lower than 14, you can use the onLowMemory method as a fallback that's roughly equivalent to the ComponentCallbacks2.TRIM_MEMORY_COMPLETE level.

    Note: When the system begins killing processes in the LRU list, although it primarily works bottom-up, it does give some consideration to which processes are consuming more memory and will thus provide more gains in memory if killed. So the less memory you consume while in the LRU list overall, the better your chances are to remain in the list and be able to quickly resume.

More information about the different stages of a process lifecycle (such as what it means to be placed in the background LRU list) is provided in the Processes and Threads document.

Summary

Constants
static Int

Level for onTrimMemory(int): the process has gone on to the LRU list.

static Int

Level for onTrimMemory(int): the process is nearing the end of the background LRU list, and if more memory isn't found soon it will be killed.

static Int

Level for onTrimMemory(int): the process is around the middle of the background LRU list; freeing memory can help the system keep other processes running later in the list for better overall performance.

static Int

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running extremely low on memory and is about to not be able to keep any background processes running.

static Int

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running low on memory.

static Int

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running moderately low on memory.

static Int

Level for onTrimMemory(int): the process had been showing a user interface, and is no longer doing so.

Public methods
abstract Unit
onTrimMemory(level: Int)

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process.

Inherited functions

Constants

TRIM_MEMORY_BACKGROUND

Added in API level 14
static val TRIM_MEMORY_BACKGROUND: Int

Level for onTrimMemory(int): the process has gone on to the LRU list. This is a good opportunity to clean up resources that can efficiently and quickly be re-built if the user returns to the app.

Value: 40

TRIM_MEMORY_COMPLETE

Added in API level 14
Deprecated in API level 35
static val TRIM_MEMORY_COMPLETE: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is nearing the end of the background LRU list, and if more memory isn't found soon it will be killed.

Value: 80

TRIM_MEMORY_MODERATE

Added in API level 14
Deprecated in API level 35
static val TRIM_MEMORY_MODERATE: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is around the middle of the background LRU list; freeing memory can help the system keep other processes running later in the list for better overall performance.

Value: 60

TRIM_MEMORY_RUNNING_CRITICAL

Added in API level 16
Deprecated in API level 35
static val TRIM_MEMORY_RUNNING_CRITICAL: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running extremely low on memory and is about to not be able to keep any background processes running. Your running process should free up as many non-critical resources as it can to allow that memory to be used elsewhere. The next thing that will happen after this is onLowMemory() called to report that nothing at all can be kept in the background, a situation that can start to notably impact the user.

Value: 15

TRIM_MEMORY_RUNNING_LOW

Added in API level 16
Deprecated in API level 35
static val TRIM_MEMORY_RUNNING_LOW: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running low on memory. Your running process should free up unneeded resources to allow that memory to be used elsewhere.

Value: 10

TRIM_MEMORY_RUNNING_MODERATE

Added in API level 16
Deprecated in API level 35
static val TRIM_MEMORY_RUNNING_MODERATE: Int

Deprecated: Apps are not notified of this level since API level 34

Level for onTrimMemory(int): the process is not an expendable background process, but the device is running moderately low on memory. Your running process may want to release some unneeded resources for use elsewhere.

Value: 5

TRIM_MEMORY_UI_HIDDEN

Added in API level 14
static val TRIM_MEMORY_UI_HIDDEN: Int

Level for onTrimMemory(int): the process had been showing a user interface, and is no longer doing so. Large allocations with the UI should be released at this point to allow memory to be better managed.

Value: 20

Public methods

onTrimMemory

Added in API level 14
abstract fun onTrimMemory(level: Int): Unit

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process. You should never compare to exact values of the level, since new intermediate values may be added -- you will typically want to compare if the value is greater or equal to a level you are interested in.

To retrieve the processes current trim level at any point, you can use ActivityManager.getMyMemoryState(RunningAppProcessInfo).

Parameters
level Int: The context of the trim, giving a hint of the amount of trimming the application may like to perform. Value is android.content.ComponentCallbacks2#TRIM_MEMORY_COMPLETE, android.content.ComponentCallbacks2#TRIM_MEMORY_MODERATE, android.content.ComponentCallbacks2#TRIM_MEMORY_BACKGROUND, android.content.ComponentCallbacks2#TRIM_MEMORY_UI_HIDDEN, android.content.ComponentCallbacks2#TRIM_MEMORY_RUNNING_CRITICAL, android.content.ComponentCallbacks2#TRIM_MEMORY_RUNNING_LOW, or android.content.ComponentCallbacks2#TRIM_MEMORY_RUNNING_MODERATE