Android Out of Memory error with Lazy Load images

后端 未结 5 1198
挽巷
挽巷 2020-12-05 22:17

I found Fedor\'s code here and implemented it into my project. The only difference is that my application does not have a list view, rather, I am accessing 1 image at a time

相关标签:
5条回答
  • 2020-12-05 22:32

    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");
    
    0 讨论(0)
  • 2020-12-05 22:48

    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.

    0 讨论(0)
  • 2020-12-05 22:49

    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

    0 讨论(0)
  • 2020-12-05 22:51

    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/

    0 讨论(0)
  • 2020-12-05 22:54

    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.

    0 讨论(0)
提交回复
热议问题