Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Detect Lowmemory in android?

Tags:

android

My application has a lot of images and sometimes it crashes due to low memory. I wrote this function that I found on the developer site.

public void onLowMemory(){

}

But the problem is that this function never gets called during those low memory circumstances. What should I do to make this function to be called. I need to alert users when there is low memory on their device.

like image 242
James Avatar asked Oct 11 '11 05:10

James


2 Answers

onLowMemory() is not a big help because it gets called only at times where the overall system is running out of memory and not in cases where your app is running out of its available heap space.

So my answer is that you should not depend on onLowMemory() being invoked. And IMHO there is no callback method for your problem.

You can simply poll the available heap space and check whether you are running out of memory or not.

Runtime.getRuntime().maxMemory();
Runtime.getRuntime().totalMemory();
Runtime.getRuntime().freeMemory();
like image 62
Zsolt Safrany Avatar answered Oct 10 '22 00:10

Zsolt Safrany


Check Memory Analysis for Android Applications .

like image 39
Arun Badole Avatar answered Oct 09 '22 22:10

Arun Badole