possible memory leak

Author: LionHereCreated Apr 27, 2019Updated May 6, 2019
Labelsquestion

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

My code was like this:

java
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:

java
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.

Source: koral--/android-gif-drawable