2

My application use a little memory, it's about 3.4MB, in some old android devices such as GT-I9001. But when i running it in HTC one, my application use very more memory.

enter image description here

Look, the allocated memory is 26.881MB, it's too big, and the free memory only have 2.940MB. Then i use MAT tool check the memory leak, i find the resource bitmap use mach memory.

I can't explain the reason. My application often out of memory. I think Maybe the problem is caused by high screen resolution. If someone also encountered this problem, please join the discussion, thanks!

I debuged the problem, and found some reason:

enter image description here

The onCreate() function in my start activity, and you can see the breakpoint. The application only use allocated memory 3.4MB before calling the setContentView(R.layout.welcome) to load layout xml. Then the application run to next step, it use allocated memory 19MB. So i think this problem must be caused by loading the layout xml.

I modifed the "welcome.xml" file, deleted all widgets, that only have a "RelativeLayout"

enter image description here

But the program also use 19MB memory. Finally, i deleted the backgroud of RelativeLayout and the program memory return to normal size, it only use 3MB.

The size of pictrue "loading_background.png" is only 21KB, i think that perhaps the high screen resolution of high-end device changed the picture size in memory, i will try to use 9.png picture. If you understand this part of the problem, please join the discussion, thanks!

Community
  • 1
  • 1
Sea turtle
  • 467
  • 1
  • 6
  • 17

4 Answers4

5

It's not a memory leak if you use big image for background.

File size doesn't matter. When it is loaded into memory it takes width * height of the image * 4bytes.

Use small 9-patch images or shape drawables when possible.

MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • 1
    That's crucial to remember, that the file size doesn't matter. For example, I have a 4096x4096 PNG image, which is just **60 KB** on disk. However, if I load it into an ImageView, it uses **64 MB** of memory! This corresponds to `4096 * 4096 * 4 = 67,108,864 bytes = 64 MB`. Such things can be very surprising and puzzling. – weibeld Nov 23 '16 at 15:53
5

This problem can be solved using drawable-nodpi, look this:

Android background image memory usage

Community
  • 1
  • 1
Sea turtle
  • 467
  • 1
  • 6
  • 17
2

Just try to make a bitmap from 3MB PNG file. You will get a 20MB picture. That's why it's beter to convert your images from PNG to JPG. The quality is not so much different actually, but you will profit a lot from memory side.

yobibyte
  • 193
  • 7
1

just add high resolution images to the xxhdpi folder in drawable. this prevent android scale up the image to the ultra sizes

smoothumut
  • 3,423
  • 1
  • 25
  • 35