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 onLowM
Check Memory Analysis for Android Applications .
From API level 14, there's method called onTrimMemory(int)
that fit your need.
Here's the doc:
http://developer.android.com/reference/android/content/ComponentCallbacks2.html
You're able to be notified when the system is about to recollect memory and also how critical the current situation is.
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();
My application has a lot of images and sometimes it crashes due to low memory
That part is not clear, if your application is downloading any image from internet or end user picking image from gallery and adding it? In this case
Or better you can remove the image from memory yourself using any algorithm(how about least recently used/viewed?)
Another case is your application itself is using too much memory because of unneeded references and leaks..This case you never warn the user that there is no more memory..It is simply your responsibility to effectively use memory...This case normally arises mainly when
In any case, it is unwise to alert user of low memory..A programmer should always program thinking you can make the app better by using less memory..
This thread will be very useful to know about your memory footprint inforamation..
And about usage of MAT tool...
Along with onLowMemory()
and onTrimMemory()
, which monitor system memory, here's a way to monitor app memory. It uses Zsolt Safrany's answer at it's core.
private final float CHECK_MEMORY_FREQ_SECONDS = 3.0f;
private final float LOW_MEMORY_THRESHOLD_PERCENT = 5.0f; // Available %
private Handler memoryHandler_;
public void onCreate( Bundle savedInstanceState ) {
// Do Stuff...
// Start Monitoring App Memory Availablity
memoryHandler_ = new Handler();
checkAppMemory();
}
public void checkAppMemory(){
// Get app memory info
long available = Runtime.getRuntime().maxMemory();
long used = Runtime.getRuntime().totalMemory();
// Check for & and handle low memory state
float percentAvailable = 100f * (1f - ((float) used / available ));
if( percentAvailable <= LOW_MEMORY_THRESHOLD_PERCENT )
handleLowMemory();
// Repeat after a delay
memoryHandler_.postDelayed( new Runnable(){ public void run() {
checkAppMemory();
}}, (int)(CHECK_MEMORY_FREQ_SECONDS * 1000) );
}
public void handleLowMemory(){
// Free Memory Here
}
As explained at android developers is hard to determine how much memory we have available for our app since: The exact heap size limit varies between devices based on how much RAM the device has available overall
. So is a better approach to ask if we are running in a lowMemory
environment using ActivityManager.MemoryInfo
before doing a memory heavy operation like downloading an image or trying to load an image into memory, by doing this you will prevent OutOfMemoryExceptions
public void doSomethingMemoryIntensive() {
// Before doing something that requires a lot of memory,
// check to see whether the device is in a low memory state.
ActivityManager.MemoryInfo memoryInfo = getAvailableMemory();
if (!memoryInfo.lowMemory) {
// Do memory intensive work ...
}
}
// Get a MemoryInfo object for the device's current memory status.
private ActivityManager.MemoryInfo getAvailableMemory() {
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
return memoryInfo;
}