How to release memory of bitmap using imageloader in android?

喜你入骨 提交于 2019-12-04 00:28:53
MarkySmarky
  1. It sounds like your fragments leak - your fragment stays in memory with all their content (including the bitmap) even if onDestroy is called. You can use MAT + Heap dumps the see what exactly causes the leak (there are many guides available out there). The first suspects should be Singletons of out of fragment classes that you pass "this" from your fragments. As the first step I would clear everything from your fragment in onDestroy.

  2. Make the "SimpleImageLoadingListener()" an inner static class that holds your fragment as WeakReference - this will probably reduce the leak (or at least one of them).

  3. You should be using any 3rd ImageLoader as a Singleton with one LRU cache + one disk cache. You can find some good ones here (Any best third party tool to cache image in Android?)

  4. "largeHeap" in the manifest shouldn't be on your mind at all. Once you have a leak then you'll just have a larger heap to fill up until OOM.

Yo can delete cached image in memory cache. Use MemoryCacheUtil for that:

MemoryCacheUtils.removeFromCache(imageUrl, imageLoader.getMemoryCache());

And DiscCacheUtils for delete discCache

DiscCacheUtils.removeFromCache(url, ImageLoader.getInstance().getDiscCache());

Besides, you can Clear Memory Cache using:

imageLoader.clearMemoryCache();

Also, you can control the memory and disk with limits:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())         
        .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) 
        .discCache(new UnlimitedDiscCache(cacheDir)) 
Vishwa

May be the reason is you just use the ImageLoader by creating instance of it as well as i don't know about DisplayImageOptions what you had.Try to change options settings and check it out.For me this options help me to load large images in List.

Try this options in your app.

DisplayImageOptions imageOptions = new DisplayImageOptions.Builder().showImageForEmptyUri(R.drawable.ic_launcher).showImageOnFail(R.drawable.ic_launcher).resetViewBeforeLoading(true).cacheOnDisk(true).imageScaleType(ImageScaleType.EXACTLY).bitmapConfig(Bitmap.Config.RGB_565).considerExifParams(true).displayer(new FadeInBitmapDisplayer(3000, true, true, false)).build();

You can try to avoid memory cache, and limit disk cache. For further optimisations you can check the source.

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