glide load multi images in viewPager with high heap size and cause OOM
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:
- 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.
- the activity which hold the
ViewPagerdestroyed,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:

the Activitydestroyed:

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.
What is holder.photo's XML? Do you have wrap_content? Because the Bitmaps are exceptionally large.
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.
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 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 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.