What is the onTrimMemory method?

Technology CommunityCategory: AndroidWhat is the onTrimMemory method?
VietMX Staff asked 3 years ago

Android can reclaim memory from your app in several ways or kill your app entirely if necessary to free up memory for critical tasks. To further help balance the system memory and avoid the system’s need to kill your app process, you can implement the ComponentCallbacks2 interface in your Activity classes.

onTrimMemory()callback method allows your app to listen for memory related events when your app is in either the foreground or the background, and then release objects in response to app lifecycle or system events that indicate the system needs to reclaim memory.

Sometimes to improve performance you have to increase memory usage, such as caching some data used by your activities. That’s the type of resource you should release when onTrimMemory is called, so your app uses less memory, even if it affects performance.