1

Hi I have a memory leak for an image of mine. I am not sure why it is a leak because the image itself is not larger than any other image in the folder and when I remove that image the memory allocation drops from 90mb to 40mb. The image its self is only 75kb, I am a little confused.

Attached is the image. Please help me if you can.

Thank you

enter image description here

enter image description here

Edon Freiner
  • 338
  • 2
  • 17

1 Answers1

0

You're seeing the difference between the compressed image size, and the uncompressed image size.

Your PNG file is compressed, meaning tricks have been used to store the data in a smaller way. When loading it into a bitmap, it gets uncompressed, for every pixel memory is needed.

You can read more about determining the size of a bitmap here, and how to use less memory for a bitmap, at the cost of losing color depth.

Let's look at an over-simplified example:

Using your example image, PNG might store the data like

rectangle x1,y1 to x2,y2 is white, rectangle x3, y3 ...

This would mean every region in your image would only take 4 coordinates and a color. However, in a bitmap, it would be stored as

pixel x1, y1 is white, pixel x2, y2 is white, ...

As you can see, a lot more data is needed for this representation. The advantage is that it can be accessed directly, instead of needing to decompress it first.

Bertware
  • 297
  • 8
  • 16
  • Ok thank you for that, but how can I go ahead and reduce the size of the image to allow more use for memory? Thnaks – Edon Freiner Apr 09 '18 at 23:17
  • Your image is 2700x4800 pixels. A lower resolution would lower your memory usage. Half as much pixels would only use half the memory space. Note that you're inspecting an xxxhdpi image, which is supposed to be a high resolution. Lower density versions will have a lower resolution. Ensure that your app also has mdpi, hdpi, ... version of this image. See https://developer.android.com/training/multiscreen/screendensities.html#TaskProvideAltBmp – Bertware Apr 09 '18 at 23:32
  • yes, they do, but I need this to be a specific size because it is a grid on which sections of the app lay on in 6 different sections. So I cannot change the size of the image. is there anything else I can try? – Edon Freiner Apr 09 '18 at 23:37
  • As long as you set the image to fullscreen, android will pick the best resolution, where xxxhdpi will only be used for tablets. Wouldn't you be better of by creating the grid using an XML layout instead of an image? E.g. linearlayout could do this easily using layout_weight – Bertware Apr 10 '18 at 00:04
  • @Betware Thanks. I cannot use the layout because the image is more aesthetically pleasing. There is no other way to make the image have a smaller memory print? – Edon Freiner Apr 10 '18 at 04:50