androidbitmaprecyclebitmapshader

Recycle the Bitmap of a BitmapShader


What would be the correct (recommended) way to recycle/release the bitmap of a BitmapShader?

Should we keep a reference of the bitmap and once the BitmapShader is no longer in use then to call bitmap.recycle()?

Looking at the BitmapShader source, it clearly shows that the class doesn’t perform any type of releasing over the bitmap, yet it keeps a reference to avoid garbage collection since most of the implementation is in native code.


Solution

  • After some research I've found that the bitmap must still be recycled, as the BitmapShader doesn't perform any release on it.

    The way to proceed is to ensure the BitmapShader is no longer used at all by any other class. So, if for example the BitmapShader is used by a Paint instace, such as paint.setShader(bitmapShader), then before recycling the bitmap, remove the shader reference like paint.setShader(null).

    Once ensured the BitmapShader isn't used and is not going to be reused at all, then recycle the bitmap as usual.