glide icon indicating copy to clipboard operation
glide copied to clipboard

OOM in case of deep fragment stack with recycler views

Open chainani opened this issue 11 years ago • 5 comments

I am using Glide 3.6.0 to populate multiple ImageViews inside a recyclerview in a fragment. I initialise glide using the following call on the onCreate of the application:

public static final int DEFAULT_DISK_USAGE_BYTES = 25 * 1024 * 1024;

GlideBuilder builder = new GlideBuilder(getApplicationContext())
            .setDiskCache(new DiskCache.Factory() {
                @Override
                public DiskCache build() {
                    return DiskLruCacheWrapper.get(Glide.getPhotoCacheDir(getApplicationContext()),
                           DEFAULT_DISK_USAGE_BYTES);
                }
            })
            //.setDiskCache(disklru)
            .setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);

Glide.setup(builder);

I populate the Image Using the following inside the onBindViewHolder:

Glide.with(BaseApplication.getInstance()).load(Uri.parse(imageURLString)).dontAnimate()
        .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(holder.imageView);

On Item click I replace the fragment with another one of the same kind but with different data and images.

I keep getting an OOM with the following stack trace:

Request threw uncaught throwable
      java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: Failed to allocate a 1904412        byte allocation with 184940 free bytes and 180KB until OOM
      at java.util.concurrent.FutureTask.report(FutureTask.java:93)
      at java.util.concurrent.FutureTask.get(FutureTask.java:163)
      at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor.afterExecute(FifoPriorityThreadPoolExecutor.java:96)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1120)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      at java.lang.Thread.run(Thread.java:818)
      at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor$DefaultThreadFactory$1.run(FifoPriorityThreadPoolExecutor.java:118)
Caused by: java.lang.OutOfMemoryError: Failed to allocate a 1904412 byte allocation with 184940 free bytes and 180KB until OOM
      at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
      at android.graphics.Bitmap.nativeCreate(Native Method)
      at android.graphics.Bitmap.createBitmap(Bitmap.java:812)
      at android.graphics.Bitmap.createBitmap(Bitmap.java:789)
      at android.graphics.Bitmap.createBitmap(Bitmap.java:756)
      at com.bumptech.glide.load.resource.bitmap.TransformationUtils.fitCenter(TransformationUtils.java:112)
      at com.bumptech.glide.load.resource.bitmap.FitCenter.transform(FitCenter.java:24)
      at com.bumptech.glide.load.resource.bitmap.BitmapTransformation.transform(BitmapTransformation.java:54)
      at com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperTransformation.transform(GifBitmapWrapperTransformation.java:34)
      at com.bumptech.glide.load.engine.DecodeJob.transform(DecodeJob.java:236)
      at com.bumptech.glide.load.engine.DecodeJob.transformEncodeAndTranscode(DecodeJob.java:139)
      at com.bumptech.glide.load.engine.DecodeJob.decodeFromSource(DecodeJob.java:129)
      at com.bumptech.glide.load.engine.EngineRunnable.decodeFromSource(EngineRunnable.java:122)
      at com.bumptech.glide.load.engine.EngineRunnable.decode(EngineRunnable.java:101)
      at com.bumptech.glide.load.engine.EngineRunnable.run(EngineRunnable.java:58)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      at java.lang.Thread.run(Thread.java:818)
      at com.bumptech.glide.load.engine.executor.FifoPriorityThreadPoolExecutor$DefaultThreadFactory$1.run(FifoPriorityThreadPoolExecutor.java:118)

I am already using "big heap". I get multiple such errors.

chainani avatar Oct 01 '15 15:10 chainani

Try using the Fragment instance in Glide.with(), so Glide can free resources when it's destroyed, hopefully allowing for the new Fragment to use that memory. I just written down the behavior here: http://stackoverflow.com/a/32887693/253468

What is the imageView's XML size? Is the Adapter and ViewHolder a static class?

TWiStErRob avatar Oct 01 '15 15:10 TWiStErRob

AWESOME! That worked, thanks. Though I am curious as to why it worked. Is the Bitmap pool not being cleared until application is closed if i use the application context? Could you please explain a little bit more. The Adapter and VH are static btw. I'm not sure what you mean by xml size.

chainani avatar Oct 01 '15 16:10 chainani

I meant android:layout_width and height, in XML, are they wrap/match or some dp? I'm asking because I just debugged an issue (613) where I discovered that if the layout says 1000dp, but the Android layout calculates 100dp, the Bitmap size is still 1000dp, wasting memory. wrap/match causes Glide to wait for layout and read the size after that loading the correct minimal size.

As to why it worked... the Bitmap pool only contains Bitmaps which are not used any more. Which means that any image currently showing in an ImageView is not in the pool. They only go back to the pool once the target (view) is cleared. The clearing only happens if you explicitly do it, start a new load into it, or by the correct lifecycle event. Also the pool can be trimmed on low memory events, but since the active images are not in the pool, they're still held in memory, causing a memory-leak like event. Hope this clears it up.

TWiStErRob avatar Oct 01 '15 19:10 TWiStErRob

@TWiStErRob , how do Glide react if the fragment is detached instead of destroyed?

howyongjun avatar Feb 16 '17 08:02 howyongjun

@howyongjun look at SupportRequestManagerFragment

TWiStErRob avatar Feb 16 '17 09:02 TWiStErRob