Skip to content

Most visited

Recently visited

navigation
Added in API level 14

ComponentCallbacks2

public interface ComponentCallbacks2
implements ComponentCallbacks

android.content.ComponentCallbacks2
Known Indirect Subclasses


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

You should implement onTrimMemory(int) 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(int) do not represent a single linear progression of memory limits, but provide you different types of clues about memory availability:

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

int TRIM_MEMORY_BACKGROUND

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

int TRIM_MEMORY_COMPLETE

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.

int TRIM_MEMORY_MODERATE

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.

int TRIM_MEMORY_RUNNING_CRITICAL

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.

int TRIM_MEMORY_RUNNING_LOW

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

int TRIM_MEMORY_RUNNING_MODERATE

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

int TRIM_MEMORY_UI_HIDDEN

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

Public methods

abstract void onTrimMemory(int level)

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

Inherited methods

From interface android.content.ComponentCallbacks

Constants

TRIM_MEMORY_BACKGROUND

Added in API level 14
int TRIM_MEMORY_BACKGROUND

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.

Constant Value: 40 (0x00000028)

TRIM_MEMORY_COMPLETE

Added in API level 14
int TRIM_MEMORY_COMPLETE

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.

Constant Value: 80 (0x00000050)

TRIM_MEMORY_MODERATE

Added in API level 14
int TRIM_MEMORY_MODERATE

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.

Constant Value: 60 (0x0000003c)

TRIM_MEMORY_RUNNING_CRITICAL

Added in API level 16
int TRIM_MEMORY_RUNNING_CRITICAL

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.

Constant Value: 15 (0x0000000f)

TRIM_MEMORY_RUNNING_LOW

Added in API level 16
int TRIM_MEMORY_RUNNING_LOW

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.

Constant Value: 10 (0x0000000a)

TRIM_MEMORY_RUNNING_MODERATE

Added in API level 16
int TRIM_MEMORY_RUNNING_MODERATE

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.

Constant Value: 5 (0x00000005)

TRIM_MEMORY_UI_HIDDEN

Added in API level 14
int TRIM_MEMORY_UI_HIDDEN

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.

Constant Value: 20 (0x00000014)

Public methods

onTrimMemory

Added in API level 14
void onTrimMemory (int level)

Called when the operating system has determined that it is a good time for a process to trim unneeded memory from its process. This will happen for example when it goes in the background and there is not enough memory to keep as many background processes running as desired. 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. May be TRIM_MEMORY_COMPLETE, TRIM_MEMORY_MODERATE, TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, or TRIM_MEMORY_RUNNING_MODERATE.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.