CN

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

Word count: 1.4kReading time: 8 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

Technical review — 2026-10-13: This historical technique depends on the Bitmap implementation. The original screenshots are retained. The field is checked against AOSP android-4.4.4_r2; Android 8.0 / API 26 explains the native-pixel change. Updated GIMP 3.2.6 import settings and a current debugging alternative follow.

When investigating image-related memory problems in MAT, knowing which image a Bitmap contains makes the object much easier to connect to an app feature. “Original image” here means the pixels held by the runtime Bitmap, not the downloaded JPEG/PNG, its compression settings, or EXIF. MAT can show what retains the object; a large Bitmap alone does not establish a leak.

This workflow requires the dump to contain the pixel array. A Java wrapper and a native address alone cannot reconstruct pixels missing from the dump.

Bitmap Object in MAT

1. Exporting Raw Bitmap Data

Select a Bitmap in MAT’s Dominator Tree and inspect its fields:

Historical Bitmap Inspector

The original screenshot shows mWidth = 1536, mHeight = 2560, mBuffer, mNativeBitmap, and mIsPremultiplied. Width and height are pixel dimensions, not screen dp or the size of the displaying View.

AOSP android-4.4.4_r2 Bitmap.java contains this hidden implementation field:

1
2
@SuppressWarnings("UnusedDeclaration") // native code only
public byte[] mBuffer;

It is not a stable SDK contract. Even that release supports native-backed Bitmaps with mBuffer == null. In android-8.0.0_r1, the implementation uses mNativePtr and NativeAllocationRegistry instead; the old export recipe is not universal.

If mBuffer references a byte[], follow the field to that array instance, then select Copy → Save Value to File. Do not export only the Bitmap wrapper, an address string, or the whole table. MAT’s copy/export rules write a single primitive array as binary, while other objects may produce only a printable value.

Save Value To File

The .data extension helps GIMP choose its raw loader; it does not convert the pixel format. Saving as .raw likewise changes neither channel order nor bit depth. Keep the original export and record:

Parameter What to verify
Width and height Actual pixel dimensions: 1536×2560 in this screenshot
Config / bytes per pixel ARGB_8888 uses four bytes per pixel and eight bits per channel; RGB_565 and other layouts need different settings
rowBytes First-pixel offset and bytes per row, including padding
Channels and byte order RGBA, BGRA, or another arrangement; the ARGB_8888 name alone is insufficient
Alpha and color space Whether values are premultiplied and how the importer interprets them
Array length and valid range Whether export is complete and includes unused capacity after reuse

For tightly packed four-byte pixels with no spare allocation capacity, the pictured dimensions require 1536 × 2560 × 4 = 15,728,640 bytes (15 MiB). Retained Size in the Inspector is not the raw-file length. If data is missing or the layout is unknown, check the export and pixel metadata first; the image cannot be reliably reconstructed without them.

2. Opening the Raw Data

Linux

Here I used GIMP’s raw-data importer. This is the interface from that time:

Historical GIMP raw-data dialog

The screenshot shows the old parameter dialog. Replace its 350×350 values with the earlier Bitmap’s actual dimensions. For data confirmed as tightly packed RGBA, eight bits per channel, and no header, choose File → Open, and manually select Raw image data under Select File Type if needed. See the GIMP 3.0 file dialog.

The GIMP 3.2.6 raw loader provides these settings:

  • Pixel format: RGBA 8-bit, corresponding to the older RGB Alpha choice.
  • Data type: Unsigned Integer. Java’s signed byte representation does not make the pixel channels signed values.
  • Planar configuration: Contiguous, for interleaved per-pixel channels.
  • Width / Height: the actual dimensions, 1536 / 2560 in this example.
  • Offset: 0 only when the first pixel starts at byte zero of the headerless export.

These settings apply only to that layout. BGRA data needs the appropriate channel reordering. If rowBytes exceeds width × 4, handle each row’s padding rather than reading it as pixels in the next row. Without the layout metadata, the reconstruction is underdetermined.

Premultiplied alpha also matters. Android’s copyPixelsToBuffer() copies stored pixels as-is, including premultiplication. Interpreting them as straight-alpha RGBA can darken translucent edges. For an eight-bit premultiplied channel and alpha above zero, round(premultipliedChannel × 255 / alpha), clamped to 0–255, approximately recovers the straight value. At zero alpha, the original color cannot be recovered. The screenshot’s mIsPremultiplied is a clue to investigate alongside Config and runtime version. When the running app can encode the Bitmap as PNG, the workflow below is usually more direct.

Mac & Windows

My recommendation at the time was to use GIMP across platforms, so there was only one set of steps to learn. It was free and had the features needed here.

GIMP also runs on macOS and Windows; the operating system does not determine the raw pixel layout. The official downloads page lists platform options and 3.2.6 (2026-09-10) as the stable release at this review.

In Photoshop, choose Photoshop Raw, not Camera Raw. For confirmed tightly packed RGBA8 data, specify the actual dimensions, four channels, eight bits per channel, interleaved channels, and Header = 0 when headerless. The earlier “32-bit depth” instruction was misleading: RGBA8 totals 32 bits per pixel, while Photoshop’s 32 Bits/Channel uses 32 bits for each channel, or 96 bits for RGB alone. See Photoshop bit depth and Raw parameters.

The fourth channel must also be applied appropriately as alpha/transparency. Photoshop does not eliminate the need to handle stride, channel order, or premultiplication.

Android 8.0+: When mBuffer Is Absent

From API 26, Bitmap pixels live in the native heap, and Config.HARDWARE involves graphics memory. See the official version history. Inspect Bitmap wrappers, BitmapDrawables, and cache ownership in Android Studio, then combine that with native-allocation or image-library cache evidence. An address cannot supply pixels absent from a Java HPROF.

If your own debug code holds a valid Bitmap, let Android encode it as PNG instead of guessing raw channel layout. This complete helper class targets an API 26+ debugging scenario. The caller provides a writable file and keeps the Bitmap from being recycled or modified during export:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import android.graphics.Bitmap;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public final class BitmapDebugExport {
private BitmapDebugExport() {}

public static void writePng(Bitmap bitmap, File target) throws IOException {
if (bitmap == null || bitmap.isRecycled()) {
throw new IllegalArgumentException("Bitmap is unavailable");
}
try (FileOutputStream output = new FileOutputStream(target)) {
if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, output)) {
throw new IOException("PNG encoding failed");
}
}
}
}

Run it on a worker thread and handle failures. The compress() contract notes that encoding can take seconds. PNG ignores the quality argument, but the output is still a new encoding, not the original source file; it also does not promise a byte-exact recovery of wide-gamut or high-bit-depth content. With only an offline HPROF and no pixel data or accessible live Bitmap, report the actual limit: ownership can be analyzed, but the image cannot be reconstructed.

About Me && Blog

Below are personal introduction and related links. I hope to communicate more with everyone in the industry. If three people walk together, there must be one who can be my teacher!

  1. Blogger Personal Introduction: There are personal WeChat and WeChat group links inside.
  2. This Blog Content Navigation: A navigation of personal blog content.
  3. Excellent blog articles organized and collected by individuals - A must-know for Android efficiency optimization: Everyone is welcome to recommend themselves and recommend others (WeChat private chat is fine)
  4. Android Performance Optimization Knowledge Planet: The paid community stopped operating in 2026 and no longer accepts new members; this link preserves its history.

One person can go faster, a group of people can go further

WeChat Scan

CATALOG
  1. 1. 1. Exporting Raw Bitmap Data
  2. 2. 2. Opening the Raw Data
    1. 2.1. Linux
    2. 2.2. Mac & Windows
  3. 3. Android 8.0+: When mBuffer Is Absent
  • About Me && Blog