I am using glide for image loading in my android app, to avoid any crashes I am loading images with application context. What will be effect of this on performance of application and memory?
2 Answers
What will be effect of this on performance of application and memory?
Glide provides so many .with()
methods for a reason: it follows lifecycle.
Imagine a Fragment
that is dynamically added to an Activity. In its onCreateView
method it starts a Glide load of a 3MB image. Now, what if the user presses the back button and the Fragment is removed or the whole activity is closed?
- If you use
with(getActivity().getApplicationContext())
nothing will happen, all 3MBs of data is downloaded and then decoded, cached, probably even set to the ImageView, which is then garbage collected, because the only reference to it was from Glide internals. - If you use
with((Fragment)this)
Glide subscribes to the Fragment's lifecycle events and as soon as the Fragment is stopped, the any outstanding request should be paused; and when destroyed, all pending requests be cleared. This means that the image download will stop midway and no more resources will be used by that dead Fragment. - If you use
with(getActivity())
Glide subscribes to the Activity's lifecycle events and the same thing happens as above, but only when the Activity is stopped or destroyed. - If you use
with(view)
Glide will do the same as inwith(view.getContext())
which is equivalent to the Activity case just above.
So the best practice is to use the closest possible context/fragment to avoid unused request completions! (There's also a manual way to stop a load: Glide.clear(ImageView|Target)
.)
To apply this in practice try to use with(this)
when possible, but when it's not, like in an adapter, or a centralized image loading method, pass in a RequestManager glide
as an argument and use glide.load(...
, for example:
static loadImage(RequestManager glide, String url, ImageView view) {
glide.load(url).into(view);
}
or in adapter:
class MyAdapter extends WhichEveryOneYouUse {
private final RequestManager glide;
MyAdapter(RequestManager glide, ...) {
this.glide = glide;
...
}
void getView/onBindViewHolder(... int position) {
// ... holder magic, and get current item for position
glide.load... or even loadImage(glide, item.url, holder.image);
}
}
and use these from Activity/Fragment:
loadImage(Glide.with(this), url, findViewById(R.id.image));
// or
list.setAdapter(new MyAdapter(Glide.with(this), data));
-
16Awesome explanation! It saved me a lot of time to investigate what was the main reason for often OOM exception. Thank you! Dec 16, 2015 at 13:21
-
2Note: calling
Glide.with(view.getContext())
is effectively equivalent toGlide.with(this)
for any view appearing on that activity, becauseRequestManagerRetriever.get(Context context)
checks whether the context is an instanceofActivity
and casts it appropriately, e.g.get((Activity)context)
. So it will end up using the sameget(Activity)
method either way. Jun 9, 2016 at 23:13 -
1So we should NOT need manually call
Glide.with(this).onDestroy()
? Assuming we use the correctContext
with ourGlide.with()..
call, sinceGlide
will hook into the Activity/Fragments lifecycle?– SakiboyMay 30, 2017 at 8:35 -
2untill Glide developers fix "You cannot start a load for a destroyed activity" throwing exception, I suggest you to use ApplicationContext– OMArikanMay 21, 2018 at 16:48
-
3@Nurseyit Author of Coil here. Similar to Glide, Coil will use an Activity's lifecycle if you start a
load
inside of a Fragment. However, both Coil and Glide will respond to theView.onDetach
events, which are triggered when a Fragment is moved to the backstack. Also, as Coil uses the AndroidX lifecycle components, any request made inside of a destroyed Activity will be instantly cancelled. Sep 16, 2019 at 21:57
A generic solution to sync Glide requests with the lifecycle of an owner. Can be called from anywhere: Activity, Fragment, RV Adapter, Custom View etc.
private fun RequestManager.syncWithLifecycleOwner(view: View): RequestManager {
val syncRequest = object : DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) = onStart()
override fun onStop(owner: LifecycleOwner) = onStop()
override fun onDestroy(owner: LifecycleOwner) {
onDestroy()
owner.lifecycle.removeObserver(this)
}
}
view.findViewTreeLifecycleOwner()?.lifecycle?.addObserver(syncRequest)
return this
}
You then can make a simple extension function like so:
fun ImageView.loadUrl(url: String) {
Glide
.with(context.applicationContext)
.syncWithLifecycleOwner(this)
.load(url)
.into(this)
}
findViewTreeLifecycleOwner()
is present in the AndroidX Lifecycle lib. It provides the Activity or the Fragment View's lifecycle (viewLifecycleOwner
) this specific ImageView is attached to. You will need to pass in application context from within the view, to make sure the Glide libs does not call the callbacks itself.
-
This looks interesting, but those Elvises are strange... if it doesn't find the lifecycle, it'll just do nothing and you'll never know all your images are loaded in a static context. Aug 25, 2022 at 8:23
-
Omitted error logging for brevity. Up to you :) Would use Coil nowadays anyway. Fits Android lifecycles better with coroutine support.– Rvb84Aug 25, 2022 at 14:07
-
This looks to be implemented in 4.15.0 github.com/bumptech/glide/commit/… Feb 24 at 18:56