Bitmap memory usage

Bitmaps are often the largest memory-consuming objects in an app. Decoding and scaling operations are frequently on the critical path for frame rendering. Optimizing bitmap memory usage provides significant improvements in UI responsiveness, battery life, and overall stability by reducing jank, ANRs, and OOM-related process kills.

Contributors to high bitmap memory usage

Virtual memory that was never used may also be included in the calculation. If you see unexpectedly high bitmap memory usage, verify you're not allocating memory that goes unused.

Resources

Analyze bitmaps in Android Studio

Android Studio profiling for bitmaps

Use the Memory Profiler to inspect memory allocations in real-time, capture heap dumps, and analyze objects for memory leaks; additionally, use the heap analyzer to detect memory leaks, identify duplicate bitmap allocations, and visualize object retention.

Automated leak detection with LeakCanary

Integrate the LeakCanary library to automate the detection of memory leaks in your app. LeakCanary provides automatic heap analysis, identifying objects that should have been garbage collected but are still held in memory, such as bitmaps retained by destroyed components.

Bitmap Performance Documentation

These resources provide comprehensive guidance on best practices for efficient bitmap handling across different Android components.

Developer checklist for optimizing bitmap memory usage

To optimize bitmap memory efficiency, follow the three core principles: reduce, reuse, and recycle.

  • Reduce: Minimize the initial memory footprint when loading or displaying bitmaps.
  • Reuse: Implement caching mechanisms to avoid redundant bitmap allocations.
  • Recycle: Proactively release resources to allow memory re-allocation for active processes.

The following developer checklist can help you optimize bitmap memory usage.

Core Principle Area Description
Reduce Eliminate Duplicate Bitmaps Analyze heap dumps using the Memory Profiler to detect redundant bitmap allocations. Refer to the Managing Bitmap Memory guide.
Leverage Image Loading Libraries Use libraries like Glide and Coil, to automate threading, caching, and efficient decoding.
Implement Downsampling Decode images to match target UI container dimensions instead of loading full-resolution assets.
Use RGB_565 for Opaque Images Reduce memory footprint by 50% by switching from ARGB_8888 to a 16-bit configuration for images without transparency.
Prioritize VectorDrawables Use vectors for icons and basic graphics to ensure sharp scaling with minimal memory overhead.
Optimize Server-Side Image Delivery Configure backend APIs to serve images tailored to device density and UI container dimensions.
Eliminate Transparent Margins Avoid allocating memory for "invisible" pixels by using InsetDrawable or layout padding instead of baked-in margins. See Engineering memory-performant Android apps.
Reuse Configure Optimal Cache Sizes Tailor memory and disk cache limits based on device RAM and screen resolution. Refer to Caching Bitmaps.
Recycle Purge Resources in Background Implement TRIM_MEMORY_BACKGROUND to clear caches and improve process survival during system memory pressure.
Release Assets when UI is Hidden Use TRIM_MEMORY_UI_HIDDEN to release bitmap caches when the app is no longer visible to the user.
Monitor for Memory Leaks Use LeakCanary and Memory Profiler to find bitmaps retained after their LifecycleOwner is destroyed. Refer to Manage your app's memory.