17.8 MiB heap allocation for a simple "Hello World" project?
Asked Answered
P

3

6

I am guessing there is an obvious answer here... I am left confused with this one:

Why do I get 17.8 MiB heap memory allocated when all I have done is:

  1. Created a simple "Hello World" project with Eclipse's New Project option.
  2. And added a 56 KiB background image.

If I take out the android:background="@drawable/background4" line, the allocated memory goes down to 11.9 MiB.

  • Is this normal for the system to allocate this much memory? Should I worry about it?
  • What would take up this much of the heap?

I also ran a MAT report on it, but I am not sure what conclusion to draw from it:

Thanks in advance,

Poyssick answered 14/3, 2013 at 8:43 Comment(3)
you can move your background image to the drawable-nodpi folder and it will take only 1.5mb of memoryIntendance
True, I did that and the memory allocation was 13.4MiB. After googling it I am guessing this is because the image does not get scaled by the system, right? How did you work out the 1.5MB?Poyssick
Yes, images under drawable-nodpi folder are not scaled by android. About bitmap size... you have a 480x800 image. Each pixel consumes 4 bytes. 480 * 800 * 4 = 1536000 bytes ~ 1.5 mbIntendance
C
4

And added a 56 KiB background image.

No, you added a 56 KiB file that you are using as a background image.

The actual heap space consumed by a bitmap is three bytes per pixel. With a ~6MB bitmap (per your MAT screen), you are running your app on a fairly high resolution device or emulator (1080p should result in ~8MB, IIRC).

Culpable answered 14/3, 2013 at 11:16 Comment(3)
Thanks it makes sense now, however did you mean to say "4 bytes per pixel" instead? (Only because: 1080p is 1920 x 1280 and then 1920 x 1280 x 4 = ~ 8.3M )Poyssick
Also, I am testing on a Samsung Galaxy S3 (1280 x 720), and I am not sure how I should get ~6MB as a result. Thanks againPoyssick
@Crocodile: You're probably right -- I was forgetting the alpha channel. As to why you are getting a 6MB heap allocation for an S3, that I can't say.Culpable
A
1

AFAIK Android will convert the image in your layout to byte Array when it inflate your layout.

It needs background image to expand to fit into your screen, doing that takes extra memory..so that 's why it allocate that much memory. I may not be right but let me know if I am wrong.

Ahner answered 14/3, 2013 at 9:3 Comment(0)
H
0

Ok, I could be wrong on the number here, but anything above Android 3 will have 24mb (16mb before that and varies from device to device) limit for the memory heap. And as with every JVM, the memory will be allocated almost in full, so your application won't need to do reallocations during its life-cycle.

That's not to say your application is actually running at 24mb, but has this allocated if it needs that.

I know you can also increase the amount of heap memory your application could use, but I'd suggest revisiting your code in case you feel the need to do so.

Obviously all that will get garbage collected as soon as your application is closed.

Update Here's how you would increase the heap if needed:

android:largeHeap="true"
Heliopolis answered 14/3, 2013 at 9:4 Comment(2)
How can I tell how much of the heap memory the application is 'actually running at'/using at the time?Poyssick
You will need to profile your application and see how much the memory grows. I don't have an IDE in front of me right now, but I seem to remember a graph that actually shows how much memory is allocated, and how much of it you're using with different line coloursHeliopolis

© 2022 - 2024 — McMap. All rights reserved.