glide icon indicating copy to clipboard operation
glide copied to clipboard

glide load multi images in viewPager with high heap size and cause OOM

Open moooy opened this issue 10 years ago • 6 comments

Glide Version:3.7.0

Issue details / Repro steps / Use case background: I have an Activity with ViewPager which have four pages to show a number of images,and each page is a category of these images. each page has a fragment with RecyclerView.my problems as follows:

  1. when the first page to load images,the app's heap size increase from 40M to 120M, and when swipe to the third page,the heap size is 150M,and have OOM.
  2. the activity which hold the ViewPager destroyed,but the memory did not release.

Glide load line / GlideModule (if any) / list Adapter code (if any): Fragment:

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setFragmentContentView(R.layout.fragment_nkphoto_grid);
        View view = super.onCreateView(inflater, container, savedInstanceState);

        /*init photoList*/
        RequestManager requestManager = Glide.with(this);
        photoList.setHasFixedSize(true);
        mGridLayoutManager = new GridLayoutManager(getContext(),4);
        photoList.setLayoutManager(mGridLayoutManager);
        mPhotoListAdapter = new NKPhotoListAdapter(Glide.with(this));
        photoList.setAdapter(mPhotoListAdapter);
        return view;
    }

Adapter:

public class NKPhotoListAdapter extends NKPhotoSelectAdapter<NKPhotoListHolder>{
    private final RequestManager glide;

    private List<NKPhotoBean> mPhotos;

    public NKPhotoListAdapter(RequestManager glide) {
        this.glide = glide;
    }

    private void setPhotos(List<NKPhotoBean> photos) {
        mPhotos = photos;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mPhotos.size();
    }

    @Override
    public void onBindViewHolder(NKPhotoListHolder holder,int position) {
        NKPhotoBean photo = mPhotos.get(position);

        glide.load(photo.getThumbURL())
               .diskCacheStrategy(DiskCacheStrategy.RESULT)
               .placeholder(R.drawable.placeholder)
               .into(holder.photo);
    }


    @Override
    public NKPhotoListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         View view = LayoutInflater.from(mContext)
                .inflate(R.layout.layout_photo_grid_item,parent,false);
        }
        return  new NKPhotoListHolder(view);
    }

    @Override
    public void onViewRecycled(NKPhotoListHolder holder) {
        if (holder.photo != null) {
            Glide.clear(holder.photo);
        }
        super.onViewRecycled(holder);
    }
}

first page load image: 20160711193511

the Activitydestroyed: 1 2 3

moooy avatar Jul 11 '16 11:07 moooy

4 pages of 4-column photo grids. That's a lot of images. They're all loaded at once, because ViewPager doesn't work the same as RecyclerView. It keeps off-screen pages.

Hmm, after reading all these details you were in the right place. #1104 is really similar. https://github.com/bumptech/glide/issues/1104#issuecomment-231169607 is all I can say. With, or without Glide you would have this problem. #842 and #785 has similarly excessive image-heavy UIs.

TWiStErRob avatar Jul 11 '16 17:07 TWiStErRob

What is holder.photo's XML? Do you have wrap_content? Because the Bitmaps are exceptionally large.

TWiStErRob avatar Jul 11 '16 17:07 TWiStErRob

holder.photo 's XML:

<com.xxxx.ui.NKSquareImageView
        android:id="@+id/photo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        tools:src="@drawable/denglu_logo"/>

NKSquareImageView is a subclass of ImageView with same length and width.

moooy avatar Jul 12 '16 01:07 moooy

wrap_content instructs Glide load a large image. Because it loads the perfect size for each ImageView, saying wrap is creating a logical loop: Glide want a the size from the view, view wants the size from the image. Since no-one knows what's the appropriate size, in this case Glide loads a screen-sized image (this is the largest possible image to be shown on screen, preparing for the possibility of a single view in a fullscreen activity; those 4MB Bitmaps =1920*1080*2(format 565)). Try changing to match_parent on at least one side. The Recycler grid should constrain the item size anyway.

TWiStErRob avatar Jul 12 '16 09:07 TWiStErRob

@TWiStErRob I'm having the same issue but with RecyclerView within a Fragment. Is it possible that the system will destroy fragment in back stack to lower down heap?

howyongjun avatar Feb 13 '17 08:02 howyongjun

@howyongjun look into implementing Fragment.onDestroyView, I suggest something like this: https://github.com/bumptech/glide/issues/1686#issuecomment-272557838. I think you can make sure that the Fragment view is destroyed by removing it in the transaction.

TWiStErRob avatar Feb 13 '17 21:02 TWiStErRob