EN

Android Memory Optimization (3) - Viewing Original Bitmaps in MAT

Word count: 485Reading time: 3 min
2015/04/11
loading

This is the final article in our MAT series, detailing how to reconstruct original images from memory snapshots to debug leaks.

  1. Android Memory Optimization (1) - Introduction to MAT
  2. Android Memory Optimization (2) - Advanced MAT Usage
  3. Android Memory Optimization (3) - Viewing Original Bitmaps in MAT

This article documents an older MAT technique for manually restoring Bitmap pixels from an offline heap, which is still useful when you need to identify the exact image being retained. The current investigation flow usually starts in Android Studio Memory Profiler: capture a heap dump, inspect retained size and references for Bitmap, BitmapDrawable, or image-cache objects, then export to MAT only when the offline evidence needs this manual reconstruction. If the issue involves native Bitmaps, textures, image-library caches, or system memory pressure, combine this with dumpsys meminfo, Perfetto/native heap profiling, and cache statistics from the image library itself.

When using MAT to analyze Android memory, you’ll frequently encounter Bitmap and BitmapDrawable$BitmapState objects. In many cases, these Bitmaps consume the majority of the heap. Memory leaks caused by Bitmaps are especially critical and must be handled promptly. When a potential image-related leak is found, seeing the actual image contents can be invaluable for diagnosis.

This article explains how to restore a Bitmap array object in MAT back into a viewable image.

Bitmap Object in MAT

1. Exporting Raw Bitmap Data

In MAT’s Dominator Tree view, select a Bitmap object and check the Inspector pane:

Bitmap Inspector

The Inspector shows essential information: mBuffer, mHeight, mWidth, and mNativeBitmap. Take note of the width and height, as we’ll need them later.

The mBuffer is defined in Bitmap.java:

1
2
3
4
5
6
7
8
9
/**
* Backing buffer for the Bitmap.
* Made public for quick access from drawing methods -- do NOT modify
* from outside this class
*
* @hide
*/
@SuppressWarnings("UnusedDeclaration") // native code only
public byte[] mBuffer;

The pixel data is stored in this byte[] array. To export it, right-click either the mBuffer entry in the Inspector or the byte[] child of the Bitmap object in the Dominator Tree. Select Copy -> Save Value To File:

Save Value To File

When saving, ensure the filename ends with the .data extension (or .raw for some tools).

2. Opening the Raw Data

Linux

Use the powerful image editor GIMP. Open GIMP and select File -> Open. Choose your exported .data file. An attribute dialog will appear:

GIMP Raw Data Import

Set the Image Type to RGB Alpha. Enter the Width and Height precisely as you saw them in the MAT Inspector. Leave other settings at their defaults and click Open. GIMP will render the pixel data as an image.

Mac & Windows

If you use PhotoShop, ensure you saved the file with a .raw extension. When opening, set the depth to 32-bit, and provide the same width and height.

Alternatively, GIMP is also available for Mac and Windows. Using GIMP across all platforms provides a consistent, free, and feature-rich experience for this task.

About Me && Blog

(Links and introduction)

CATALOG
  1. 1. 1. Exporting Raw Bitmap Data
  2. 2. 2. Opening the Raw Data
    1. 2.1. Linux
    2. 2.2. Mac & Windows
  • About Me && Blog