Listening to Memory events using onTrimMemory() on Android

Gurpreet Singh
2 min readAug 22, 2017

--

Anyone who has been developing android application knows how memory-scarce the platform is, with each application being provided with upto 24/32mb of memory (which can go to as low as 16mb for older devices)*. In such a scenario, it is the developer’s duty to look out for memory leaks and free any resources that are not been used by the application.

One way of managing memory in response to system events is the onTrimMemory() method. From the docs:

onTrimMemory(): 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.

Found in the ComponentsCallback2 interface, this method should be implemented to incrementally release memory based on current system constraints. This helps provide a more responsive system overall, and benefits the user experience for the application by allowing the system to keep the process alive longer.

Example code snippet can be:

Note: The onTrimMemory() callback was added in Android 4.0 (API level 14). For earlier versions (if you’re one such unfortunate person), you can use the onLowMemory() callback as a fallback for older versions, which is roughly equivalent to the TRIM_MEMORY_COMPLETE event.

For a better read on how to optimise the memory usage, do read the official docs, or show some love and I’ll post another article on Using More Memory-Efficient Code Constructs to create android applications. All kinds of feedback is welcome.

Reach out to me on Twitter to say Hi!

References:

  1. Memory limits per app are here depending on screen size and Android version: https://drive.google.com/file/d/0B7Vx1OvzrLa3Y0R0X1BZbUpicGc/view
  2. https://stackoverflow.com/questions/19398827/understanding-ontrimmemory-int-level

--

--

Gurpreet Singh

Learner. Inquisitive. Embraces Functional & Reactive programming and TDD. Likes experimenting. Trying to read & write well. Also, Google certified Android Dev.