Universal Image Loader UIL nostra out of memory error

不打扰是莪最后的温柔 提交于 2019-12-01 06:04:23
Raghunandan

https://github.com/nostra13/Android-Universal-Image-Loader.

From the above link

If you often get OutOfMemoryError in your app using Universal Image Loader then try next (all of them or several):

  1. Reduce thread pool size in configuration (.threadPoolSize(...)). 1 - 5 is recommended.

  2. Use .bitmapConfig(Bitmap.Config.RGB_565) in display options. Bitmaps in RGB_565 consume 2 times less memory than in ARGB_8888.

  3. Use .memoryCache(new WeakMemoryCache()) in configuration or disable caching in memory at all in display options (don't call .cacheInMemory()).

  4. Use .imageScaleType(ImageScaleType.IN_SAMPLE_INT) in display options. Or try .imageScaleType(ImageScaleType.EXACTLY).

  5. Avoid using RoundedBitmapDisplayer. It creates new Bitmap object with ARGB_8888 config for displaying during work.

Example: Modify the c.ode in the below link sticking to the above points to avoid OOM

Caching images and displaying

SOLVED:

It wasnt't a UIL problem. The problem was with the page adapter. The bitmaps in the Fragments weren't handled properly by the GC and there was some indirect reference retaining them. Freeing the bitmaps explicitely solved the problem.

EDIT:

It wasn't a proper UIL problem. I was working with SupportFragment library, and this may be not properly tweaked. The trick for me was manually release every bitmap that was not on screen, but the problem should be looked at in a different way. This application uses a very large amount of WebViews, that in my opinion are evil due to fact that their memory consumption is simply silly! Adding UIL with hi-res images makes my app going OOM due to the combination of these factors.

It's a bit empirical solution, but it really works.

I have following configuration with disk cache. I never faced problem of OOM.

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
            YourActivity.this).threadPoolSize(5)
            .threadPriority(Thread.MIN_PRIORITY + 3)
            .denyCacheImageMultipleSizesInMemory()
            // .memoryCache(new UsingFreqLimitedMemoryCache(2000000)) // You
            // can pass your own memory cache implementation
            .memoryCacheSize(1048576 * 10)
            // 1MB=1048576 *declare 20 or more size if images are more than
            // 200
            .discCache(new UnlimitedDiscCache(cacheDir))
            // You can pass your own disc cache implementation
            // .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
            .build();

To create cache dir,

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