koral-- / android-gif-drawable

Views and Drawable for displaying animated GIFs on Android
Other
9.54k stars 1.78k forks source link

possible memory leak #653

Open LionHere opened 5 years ago

LionHere commented 5 years ago

Encountered a situation of possible memory leak, or was it a misusage?

My code was like this:

frequently_called_binding_function (My_GifImageView_Pool pool) {
    GifImageView gifImageView= pool.get();

    gifImageView.setImageBitmap(a small size bitmap as a thumbnail);

    runOnWorkerThread(() -> {
        ParcelFileDescriptor pfd = download a gif from server();
        runOnUiThread(() -> {
            GifDrawable gifDrawable = new GifDrawable(pfd.getFileDescriptor());
            gifImageView.setImageDrawable(gifDrawable);
            pfd.close();
        }
    });
}

That pool had a fix number of GifImageViews, and never re-created new GifImageView. Runing this binding function a dozens of time bumped the natvie heap from 30mb to ~200mb, and forcing gc would not help.

I fix this by manually recycling GifDrawable before #setImageBitmap:

gifImageView.getDrawable().recycle(); // leaving out null check and cast
gifImageView.setImageBitmap(a small size bitmap as a thumbnail);

I'm not sure if this should be considered as a leak, and I'm sorry if I wasted anyone's time.

koral-- commented 5 years ago

Could you share a minimal project which reproduces this issue?

LionHere commented 5 years ago

Could you share a minimal project which reproduces this issue?

hope this could help: LionHere/AndroidGIFDrawableDemo

koral-- commented 5 years ago

Thanks, I'll check this

koral-- commented 5 years ago

Thanks, I haven't considered such usage. It seems that there is no leak but due to the fact GifDrawable uses single Executor for all instances by default, subsequent actions are scheduled more frequently than they can be purged.

I'll analyze this deeper and get back.