Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High CPU usage when playing GIFs #1029

Closed
edrobap opened this issue Mar 4, 2016 · 4 comments
Closed

High CPU usage when playing GIFs #1029

edrobap opened this issue Mar 4, 2016 · 4 comments
Labels

Comments

@edrobap
Copy link

edrobap commented Mar 4, 2016

I am displaying GIFs in a RecyclerView. Code:

Glide.with(acitivity)
    .load(url)
    .placeholder(R.drawable.image_placeholder)
    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
    .listener(listener)
    .into(viewHolder.imageView);

If I keep the Fragment containing this RecyclerView open, very high amount of CPU (~40%) is used by the app. And the phone heats up if I use the Fragment for 1-2 minutes. Is it normal for GIFs? Am I doing anything wrong here?

@TWiStErRob
Copy link
Collaborator

You're saying you're displaying a list of images that are animated, I think it's normal to use some CPU power this case, especially if you have a GridLayoutManager. Glide decodes frames on the fly and this way it saves a lot of memory and also reuses the frames so no need for constant GC-ing.

If you were to pre-load all Bitmaps of all frames (spike in CPU) and then use those memory-cached frames (low CPU) you would likely run into OOM. Consider displaying static images (.asBitmap()) and play the GIF animation on tap (reload same model into same image but without asBitmap.

Alternatively stop playing GIFs after 10 seconds of no user interaction by doing setting a flag on the adapter (that the user didn't move) and calling notifyDataSetChanged and use asBitmap is the flag is on.

@edrobap
Copy link
Author

edrobap commented Mar 4, 2016

I checked with LinearLayoutManager and 2 GIFs visible at a time the CPU usage is 30-40%. With GridLayoutManager and 10 GIFs visible (skips lot of frames but that okay) at a time it is 60-70%.

I like your suggestion of playing GIF on tap and stop playing after some timeout. Should save some battery. Thanks

@TWiStErRob
Copy link
Collaborator

This is really a game of compromise, CPU/battery or memory... both are scarce resources in Android.

You should be able to call setLoopCount on GlideDrawable/GifDrawable so it only plays a few times, instead of counting seconds, use .into(new GlideDrawableImageViewTarget(imageView, 3)) to repeat 3 times, note that as a side effect you'll need to explicitly add .centerCrop() or .fitCenter(). (#860)

@edrobap
Copy link
Author

edrobap commented Mar 4, 2016

Thanks a lot for your helpful suggestions.

@edrobap edrobap closed this as completed Mar 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants