1

I have a RecyclerView. The row in the recyclerview contains an ImageView. I'm using Glide to load the images (size is 30-80 KB / image). If I scrolling from top to the bottom I get OutOfMemoryException in the end. The test device is quite old: Samsung Galaxy S3.

I know there is plenty of similar questions. I've tried them all, but I haven't been successful so far.

The RecyclerView is in a fragment in a ViewPager (with 2 pages).

Row's layout:

...
 <ImageView
    android:id="@+id/catch_pic"
    android:layout_width="0dp"
    android:layout_height="@dimen/_250sdp"
    android:layout_marginTop="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/description"/>
...

My adapter:

...
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
    final Catch actualCatch = mItems.get(position);

    CatchViewHolder catchHolder = (CatchViewHolder) holder;

    if (actualCatch.getCatchPic() != null && !actualCatch.getCatchPic().isEmpty()) {
        catchHolder.mCatchPic.setVisibility(View.VISIBLE);

        Glide
            .with(catchHolder.itemView.getContext())
            .load(BASE_URL_FOR_IMAGES.concat(actualCatch.getCatchPic()))
            .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(30)).skipMemoryCache(true))
            .into(catchHolder.mCatchPic);
    } else {
        catchHolder.mCatchPic.setVisibility(View.GONE);
    }
}

@Override
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
    super.onViewRecycled(holder);

    CatchViewHolder catchHolder = (CatchViewHolder) holder;

    Glide.with(holder.itemView.getContext()).clear(catchHolder.mCatchPic);
}
...

Logcat detail(while scrolling):

I/dalvikvm-heap: Grow heap (frag case) to 14.435MB for 3768016-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 19.355MB for 1377016-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 25.652MB for 1632016-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 38.937MB for 2764816-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 42.324MB for 2160016-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 45.533MB for 2186256-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 42.929MB for 2186256-byte allocation
I/dalvikvm-heap: Grow heap (frag case) to 45.331MB for 1997584-byte allocation

I slowly go mad. Can anybody help me where is the error?

Edit #1:

If I temporarily limit the ViewPager to 1 fragment, the result is the same:

02-07 20:07:27.564 25465-25890/net.demo I/dalvikvm-heap: Grow heap (frag case) to 39.165MB for 7990288-byte allocation
02-07 20:07:28.344 25465-25892/net.demo I/dalvikvm-heap: Grow heap (frag case) to 26.725MB for 1228816-byte allocation
02-07 20:07:28.799 25465-25892/net.demo I/dalvikvm-heap: Grow heap (frag case) to 34.354MB for 7990288-byte allocation
02-07 20:07:28.869 25465-25890/net.demo I/dalvikvm-heap: Grow heap (frag case) to 41.971MB for 7990288-byte allocation
02-07 20:07:28.949 25465-25893/net.demo I/dalvikvm-heap: Grow heap (frag case) to 50.640MB for 7990288-byte allocation
02-07 20:07:29.289 25465-25892/net.demo I/dalvikvm-heap: Grow heap (frag case) to 44.330MB for 7990288-byte allocation
02-07 20:07:29.514 25465-25894/net.demo I/dalvikvm-heap: Grow heap (frag case) to 30.028MB for 3048208-byte allocation
02-07 20:07:29.884 25465-25894/net.demo I/dalvikvm-heap: Grow heap (frag case) to 32.937MB for 3048208-byte allocation
02-07 20:07:30.044 25465-25893/net.demo I/dalvikvm-heap: Grow heap (frag case) to 32.940MB for 3048208-byte allocation
Zoe
  • 27,060
  • 21
  • 118
  • 148
wyzard
  • 543
  • 5
  • 17
  • Is `onViewRecycled` invoked ? Looks like due to fast scrolling, too many images are being loaded resulting in OOM. Instead try overriding `destroyItem` as mentioned in [this](https://github.com/bumptech/glide/issues/679) thread. Moreover, since you are using viewPager, is there possible heavy lifting going on there ? You might try to allocate one pager at a time in memory – NightFury Feb 07 '19 at 18:57
  • Thank you for your answer. I've completed my question. Yes, the onViewRecycled invoked. – wyzard Feb 07 '19 at 19:11
  • In `onViewRecycled` perhaps also try setting the ImageView drawable to null? `catchHolder.mCatchPic.setImageDrawable(null)` – sbearben Feb 07 '19 at 21:22
  • Same result. :( – wyzard Feb 07 '19 at 21:44
  • Changing to Fresco the problem has gone (so far). – wyzard Feb 08 '19 at 12:32
  • 1
    change glide requests when scroll, [link](https://stackoverflow.com/a/39707198/8536476) – chen Apr 09 '19 at 02:16
  • I've changed to Fresco, but thanks, I'll give it a try. – wyzard Apr 14 '19 at 09:56

0 Answers0