Android Out of Memory error with Lazy Load images

混江龙づ霸主 提交于 2019-11-27 21:42:19

Generally, your in-memory image cache declaration should look something like:

private static HashMap<String, SoftReference<Bitmap>> cache = 
    new HashMap<String, SoftReference<Bitmap>>();

Note your OOM problems may not necessarily be related to your Bitmap cache. Always ensure you stop/interrupt the thread spawned by your image loader in your Activity's onDestroy method or in the finalize of any other class that is managing it.

Use the Eclipse Memory Analyzer Tool in conjunction with DDMS to analyze your application's memory usage: http://www.eclipse.org/mat/

To fix my OOM from using Lazy Loader, I manually called garbage collection ( System.gc(); ) every time I create or destroy a page (probably doing one or the other would be fine) because Android doesn't always call it as often as it should, and used Fedor's clearCache method:

if (ImageLoader.getImageCache() != null)
    ImageLoader.clearCache();

The function is there, but never gets called by anything unless you make use of it. I do this in my onCreate of the second activity that uses the Lazy Load. You can also do:

@Override
public void onLowMemory() {
    super.onLowMemory();
    ImageLoader.clearCache();
}

This never seems to get called for me, but with how large your images are you might need it. Also consider shrinking the images unless you have a reason to keep them so large.

i face the same problem and i use the SoftReference for the bitmap and the url but it seems that the Dalvik VM reclaim SoftReference very quickly and my ListView images keep flickering then i tried inPurgeable= true and i get over OutOfMemoryException and my listview images didn't flicker.

consider recycling the memory you allocated to the bitmap image

bitmap.recyle(); bitmap=null;

call this function when you no longer require the bitmap image this will free up some memory

You should change the following line in android 2.1 or below.

cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"MyApp/Temp");

to this

cacheDir = new File(context.getCacheDir(), "MyApp/Temp");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!