49

I'm analyzing memory usage of my Android app with help of Eclipse Memory Analyzer (also known as MAT). Sometimes I can find strange instances of android.graphics.Bitmap class, utilizing big portion of heap. Problem is what I can't find source of this bitmaps, no filename, no resourceID, nothing. All information what I can find for bitmap is following: bitmap_info

There is a field mBuffer with array of image pixels, I assume. But it's in some internal Android format, not PNG.

Question: how can I view image represented by this bitmap from memory dump?

HitOdessit
  • 7,198
  • 4
  • 36
  • 59

2 Answers2

124

I have found a way to view such bitmaps:

  • First, you need to download and install GIMP
  • Next, find your Bitmap object in MAT, right-click on mBuffer field, in the popup menu choose "Copy" -> "Save Value To File" menu item and save value of this array to some file
  • give extension .data to that file
  • launch GIMP, choose "File" -> "Open", select your .data file and click Open button
  • "Load Image from Raw Data" dialog will appear. Here you need to set correct parameters for your bitmap
  • first, choose "Image type" as "RGB Alpha" (most Android resources have this image type, but you may need to experiment with other image types)
  • second, set correct Width and Height for your bitmap (correct dimensions can be found in the memory dump)

At that point you should already observe preview of original image. If you didn't, you can try to change some other parameters in "Load Image from Raw Data" dialog.

NOTE: to get a width and height of image you can look at mWidth and mHeight fields in MAT in attributes section as shown in image in question.

singularity
  • 147
  • 1
  • 11
HitOdessit
  • 7,198
  • 4
  • 36
  • 59
  • 4
    This should be upvoted more. This is the most useful thing an Android developer would want. Brilliant! – farmdve Mar 19 '13 at 00:40
  • 1
    For me, mBuffer copy only values, not bytes. 'Copy' works on byte[xxxxx] . Also, to be clear: width X height its spec of tablet. For exemple: I tested this operation on amazon hd8,9 and I have 1200x1920 . – Sergey Vakulenko Apr 11 '13 at 13:57
  • Edit: it was background image, that why height of tablet was same. For me, its not clear where I can find width and of height of selected byte array – Sergey Vakulenko Apr 11 '13 at 14:09
  • I have the same question as Sergey: how to know the width and height of the selected img? – ElfeXu Jul 01 '13 at 02:06
  • 1
    As you can see at first image in the post - there are two fields `mWidth` and `mHeight` – HitOdessit Jul 01 '13 at 06:57
  • in eclipse mat where is mBuffer field.plz anybody tell me. – Ekanta Swain Aug 10 '13 at 11:58
  • 1
    its in the attributes tab in the Inspector Window. Window>Inspector – user1446988 Sep 09 '13 at 23:48
  • is it possible to open the raw data via the "paint.net" app instead of GIMP ? also, as others have asked, how do you get the width and height of the image? – android developer Oct 22 '13 at 07:57
  • 1
    How did they get the window which specifies the mHeight and mWidth in the question? I couldn't get it in MAT. – venkat Aug 07 '14 at 08:07
  • 2
    For those who can't find the window showing `mWidth`-`mHeight`, Goto `Window->Show View->MAT->Inspector`. Select an object in `dominator tree` and in the inspector window you can see the height-width. – Ashish Tanna Mar 31 '15 at 00:04
6

You can convert memory dumps from MAT to png using with ImageMagick on command line.

In MAT for related Bitmap object right click mBuffer field and select "Copy" -> "Save Value To File", name the file with an .rgba extension.

You need to note bitmap width and height from mWidth and mHeight fields, which you can see in Bitmap object.

Having ImageMagick command line tools installed (for Ubuntu apt-get install imagemagick), you issue convert command with the following parameters.

convert -size 'width'x'height' -depth 8 filename.rgba filename.png

For example

 convert -size 680x1209 -depth 8 phone_decor.rgba phone_decor.png

You can check generated png file via eog, like eog phone_decor.rgba on Ubuntu easily.

auselen
  • 27,577
  • 7
  • 73
  • 114