Added in API level 14

ComponentCallbacks2


interface ComponentCallbacks2 : ComponentCallbacks
android.content.ComponentCallbacks2

Callbacks for app state transitions that can be used to improve background memory management. This interface is available in all application components (android.app.Activity, , ContentProvider, and android.app.Application).

You should implement onTrimMemory to release memory when your app goes to background states. Doing so helps the system keep your app's process cached in memory for longer, such that the next time that the user brings your app to the foreground, the app will perform a warm or hot start, resuming faster and retaining state.

Trim memory levels and how to handle them

  • TRIM_MEMORY_UI_HIDDEN
    Your app's UI is no longer visible. This is a good time to release large memory allocations that are used only by your UI, such as Bitmaps, or resources related to video playback or animations.
  • TRIM_MEMORY_BACKGROUND
    Your app's process is considered to be in the background, and has become eligible to be killed in order to free memory for other processes. Releasing more memory will prolong the time that your process can remain cached in memory. An effective strategy is to release resources that can be re-built when the user returns to your app.

Apps that continue to do work for the user when they're not visible can respond to the TRIM_MEMORY_UI_HIDDEN callback by changing their behavior to favor lower memory usage. For example, a music player may keep full-sized album art for all tracks in the currently playing playlist as Bitmaps cached in memory. When the app is backgrounded but music playback continues, the app can change the caching behavior to cache fewer, or smaller, Bitmaps in memory.

The ordinal values for trim levels represent an escalating series of memory pressure events, with incrementing values accordingly. More states may be added in the future. As such, it's important not to check for specific values, but rather check if the level passed to onTrimMemory is greater than or equal to the levels that your application handles. For example:

<code>public void onTrimMemory(int level) {
      if (level &gt;= TRIM_MEMORY_BACKGROUND) {
          // Release any resources that can be rebuilt
          // quickly when the app returns to the foreground
          releaseResources();
      } else if (level &gt;= TRIM_MEMORY_UI_HIDDEN) {
          // Release UI-related resources
          releaseUiResources();
      }
  }
  </code>

Note: the runtime may invoke Garbage Collection (GC) in response to application state changes. There is no need to explicitly invoke GC from your app.

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