bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.7k stars 6.13k forks source link

Memory Leak in Android 2.3 #472

Closed aqiansunboy closed 9 years ago

aqiansunboy commented 9 years ago

glide

does newest Glide solove memory leak in Android 2.3?

TWiStErRob commented 9 years ago

Glide 2.3=API 9 is not supported. If you mean 2.3.3+, can you please add a more detailed description by using the issue template in /CONTRIBUTING.md?

sjudd commented 9 years ago

FWIW, this also doesn't look like a memory leak. As far as I can tell, you're looking at a GifDrawable that is currently playing. It's expected that Glide will retain a reference to the GifDrawable while it is playing because we only keep the most recent frame in memory. If you stop the drawable (or clear() the Target/View it is playing in), these references should go away.

aqiansunboy commented 9 years ago

@TWiStErRob ,Thank you for your comments.It is API 10.The glide version I'm not sure,because this project is writed by other people who had left the company,and he manual edited glide's code.I plan when current version is publised,I will replace of the newest glide lib.

aqiansunboy commented 9 years ago

@sjudd Thank you for your comments.I finish the MainActivity when I exit my app,and the MainActivity instance is reference by glide,result in the memory can not be released.When I restart the app and exit and restart....the memory is leak more and more,until OOM......

sjudd commented 9 years ago

Can you provide the load line you're using? The only way this could happen (that I can think of) is if you're continuing to play the gif in the background, which means you're not clearing it and you're either providing Glide with the application context, or a fragment or activity that doesn't actually contain your GIF.

It's also possible you have an old version of Glide, which is going to be hard for us to help you with, especially if there are local modifications.

Try either clearing the view or Target, or pass in the activity to Glide.with().

aqiansunboy commented 9 years ago

@sjudd Thank you for your comments.we create one single pattern Class named GlideUtil,and use application context as instance field:

mContext = ForumsApplication.getApplication();

load line is like that:

public void loadImage(ReloadImageView riv, boolean isLoadImmediatly)
{
   final boolean isWifi = NetworkUtils.isWifiAvailable(mContext);
   final boolean canDown = !sp.isShowImageWithWifi() || isWifi || riv.isShowInMobileNetwork();
   if (TextUtils.isEmpty(riv.getUrl()) || !canDown)
   {
           riv.getView()
               .setImageResource(R.color.hui_DDDDDD);

            return;
   }
    Glide.with(mContext)
             .load(riv.getUrl(), mHeader)
             .isLoadImmediatly(isLoadImmediatly)
             .listener(strL)
             .error(R.color.hui_DDDDDD)
             .placeholder(R.color.hui_DDDDDD)
             .into(riv);
}

then we use load image like that anywhere

GlideUtil.getInstance().loadImage(imageUrl, true);

Does this way useing glide have any problem?

sjudd commented 9 years ago

Ah yes, if you're loading animated GIFs. Android doesn't change visibility or provide any other hint to Drawables in ImageViews when an application is backgrounded, or an Activity is destroyed, so there's no built in way to automatically stop animated Drawables when the user can no longer see them.

Glide gets around this by listening to the lifecycle of the Fragment or Activity you pass in to with() using a view-less Fragment. Doing so allows us to stop animations when the corresponding Activity or Fragment is stopped, which prevents this memory leak and a bunch of cpu usage when your app is backgrounded.

By passing in the application context always, you're removing Glide's ability to listen to your application's lifecycle.

Either:

  1. Pass in the Fragment or Activity containing your ReloadImageView to with() for every load.
  2. Call Glide.clear() on any ReloadImageViews that might be showing animated GIFs.
aqiansunboy commented 9 years ago

@sjudd Thank you for your comments, I got it,and I will refact my code next version.