Skip to content

Android Improving Performance With Images In Memory

Android: Reduce Image Memory Usage#

Decoded bitmaps can use much more memory than their compressed image files. For example, an image stored as a small PNG or JPEG still has to be decoded into pixels before it can be rendered.

Common bitmap configs:

ARGB_8888  32 bits per pixel, includes alpha
RGB_565    16 bits per pixel, no alpha
ALPHA_8     8 bits per pixel, alpha only
HARDWARE   stored only in graphics memory

RGB_565 can reduce memory use for images that do not need alpha or high color precision. Do not use it blindly; gradients and photos may show banding.

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;

Bitmap bitmap = BitmapFactory.decodeResource(
    getResources(),
    R.drawable.first_bitmap,
    options
);

For large images, also decode them at the size you need instead of loading the full-resolution asset into memory.

Sources#