Skip to content

Glide is leaking the memory Trying to retry Image loading when glide fail to load  #3067

Closed
@hussainahmad

Description

@hussainahmad

Glide Version:4.6.1

Integration libraries:
Okhttpversion:4.6.1

Device/Android Version:

Issue details / Repro steps / Use case background:

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

private void loadImage(ViewHolder holder, int position) {
WeakHandler mHandler = new WeakHandler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                loadImage(holder, position);
            }
        };
Glide.with(context).
load(getUri(position))
                .apply(getFitCenter())
                .listener(new Listener() {
                    @Override
                    public void onRetry() {
                        mHandler.postDelayed(runnable, 2500);
                        handlerRunnableMapCategory.put(mHandler, runnable);
                    }
                })
                .into(imagViewProduct);

WeakHandler is Memory safer implementation of android.os.Handler
Here handlerRunnableMapCategory is a map where I am storing the instance of handler and runnable, and In onDestroy I am clearing the map and removeCallbacksAndMessages , And Its is working, but still I am getting Leak through LeakCanary
screenshot_2018-05-03-16-10-05

Activity

hussainahmad

hussainahmad commented on May 3, 2018

@hussainahmad
Author

screenshot_2018-05-03-19-50-32

sjudd

sjudd commented on May 7, 2018

@sjudd
Collaborator

handlerRunnableMapCategory in the first example is leaking memory regardless of the weak handler. None of the weak handler or runnables you're using above should be necessary.

Glide will keep a reference to the Target as long as the request is in progress. You can cancel them manually via Glide.with(Context/Fragment/Activity)).clear(Target). If you use the appropriate lifecycle (Activity/Fragment) Glide will clear pending requests when the lifecycle goes through onDestroy.

hussainahmad

hussainahmad commented on May 8, 2018

@hussainahmad
Author

But without handler it will crash the app with the reason java.lang.IllegalStateException: You can't start or clear loads in RequestListener or Target callbacks. If you must do so, consider posting your into() or clear() calls to the main thread using a Handler instead use Handler#post to post your into() , and I debugged handlerRunnableMapCategory is clearing and and setting null in onDestroy , Is there any best approach to retry image in onLoadFailed ?

stale

stale commented on May 15, 2018

@stale

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.

sjudd

sjudd commented on May 15, 2018

@sjudd
Collaborator

Ah sorry I missed that onRetry is called in Glide's RequestListener callback. In that case you do need to post, though without a delay.

In what I can see of the leak stack traces, it looks like the Glide request hasn't been cleared. If the Context you're using is not the activity context, you'll need to clear the request manually with Glide.with(context).clear(target).

hussainahmad

hussainahmad commented on May 16, 2018

@hussainahmad
Author

@sjudd I am using ApplicationContext, I think that's the issue. So Glide is unable to clear, I have to do this manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sjudd@hussainahmad

        Issue actions

          Glide is leaking the memory Trying to retry Image loading when glide fail to load · Issue #3067 · bumptech/glide