Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Application onLowMemory() on API >= 14

a tiny question.

According to the Android docs:

Preferably, you should implement ComponentCallbacks2.onTrimMemory from ComponentCallbacks2 to incrementally unload your resources based on various levels of memory demands. That API is available for API level 14 and higher, so you should only use this onLowMemory method as a fallback for older versions

So. Will onLowMemory() be called by higher API's as well?
Clearly, I just want to call it from within onTrimMemory() when the level is TRIM_MEMORY_COMPLETE, but I don't want it to be called twice :)

Thx!

like image 993
guy_m Avatar asked Sep 10 '15 10:09

guy_m


People also ask

How do I fix out of memory error on Android?

If you haven't seen any OOM in your Android application, then you are going to have one in future. The OutOfMemoryError comes in Android due to memory leaks. So, in order to remove the OutOfMemoryError, you need to remove memory leaks from your Android application.

What is the onTrimMemory () method Android?

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.

How do I stop an Android application from closing?

In android, by pressing a back button or home button. So put an event key listener for back & home button and terminate the service.


1 Answers

Yes, absolutely. It is not deprecated at any API level. You should be implementing both onTrimMemory() and onLowMemory() for the sake of keeping the system healthy.

These are covered in more detail in the video: Trimming and Sharing Memory (Android Performance Patterns Season 3 ep5).

like image 200
Kirill Zotov Avatar answered Oct 02 '22 02:10

Kirill Zotov