10

This happens when the app loads from Splash screen to Main page. It happens only on the device not on simulator:

05-17 08:10:16.627: I/dalvikvm-heap(14021): Grow heap (frag case) to 20.580MB for     2424256-byte allocation
05-17 08:10:16.666: D/dalvikvm(14021): GC_FOR_ALLOC freed 1K, 3% free 21000K/21511K, paused 21ms
05-17 08:10:16.697: D/dalvikvm(14021): GC_CONCURRENT freed 116K, 3% free 20885K/21511K, paused 2ms+2ms
05-17 08:10:16.720: D/dalvikvm(14021): GC_FOR_ALLOC freed 44K, 4% free 20841K/21511K, paused 10ms
05-17 08:10:16.728: I/dalvikvm-heap(14021): Grow heap (frag case) to 24.533MB for 4310896-byte allocation

I used Ecplise MAT - the byte allocation resolved - Android.Graphics.Bitmap $preloaded images...

The device I am using is Google Nexus Prime, Android 4.0

Has anyone encountered the same? Can someone throw some expertise....

Ashwin
  • 262
  • 2
  • 5
  • 13

2 Answers2

14

I have experienced the same issue and found a solution to it.

Are you loading your bitmaps from resource? if so try placing them in corresponding drawable folders instead of keeping them in "drawable" or "drawable-mdpi".

When you have your image resources in plain drawable folder, to the system it means drawable-mdpi. Therefore devices with high or extra high dpi (Galaxy Nexus I believe is Extra high) will expand that resource to match the dpi of the device.

If you only have 1 size for your resources, put them in drawable-nodpi and it will be used as is. However some of your layouts may be affected by this, so I kept certain images in the drawable folder (like button images etc.) and moved backgrounds and other bigger resources into drawable-nodpi.

Hope this helps ;)

hjm
  • 421
  • 2
  • 8
  • I am placing all my images in the drawable-hdpi folder. But I have another xml files in the drawable folder which is for stateChangingStyles for the buttons. Those xml files contains images... Will that be a cause of concern? – Ashwin May 18 '12 at 12:57
  • @Ashwin having xml files in the drawable file shouldn't be an issue. how big are the bitmaps stored in the hdpi folder? I suggest you try putting them in xhdpi or no-dpi and see if you get the same kind of error. – hjm May 21 '12 at 05:11
  • I changed the image to the suggested folders but no effect. And all the images I have are in png format, the largest image I have in the entire application is only 77kb. This doesn't make sense because it is allocating 24 MB during the initial launch. – Ashwin May 22 '12 at 02:21
  • 3
    @Ashwin hmmm, i guess the problem lies somewhere else. by the way, PNG is just a way of compressing images so that they can be transferred easier. When it needs to be displayed on screen, it needs to get decompressed. so what matters is not the size in kb, but the dimension of the image. 1 pixel takes up a certain amount of memory, depending on the bitmap settings but say if you use ARGB_8888 it will take up 32bits per pixel. Using PNG will only help your apk file to be smaller. make sure your images aren't too large. – hjm May 22 '12 at 03:59
  • @Ashwin Try checking out this answer.http://stackoverflow.com/questions/7247049/android-grow-heap-frag-case – hjm May 22 '12 at 04:01
  • @hjm- Thanks I will check the resolution of the images. What is the optimal resolution. Just want to make sure before I make any changes. Just to confirm once more that this doesn't take place in simulator – Ashwin May 22 '12 at 12:50
  • I verified the images, the sizes were 960 X 640 pixels. Can you please suggest the optimal way.. I am using this in as a background in a relative layout which is root node. – Ashwin May 22 '12 at 16:52
  • @Ashwin 960x640 is an acceptable size. however it should be put in the drawable-xhdpi folder. (its kind of in between xhdpi and hdpi). do you have a different image loaded on the splash screen? also how many images are you loading? just to make sure it is not a resource placement issue, try putting all your images into the drawable-xhdpi folder. if you haven't tried that already. – hjm May 23 '12 at 07:29
  • @hjm.. All my images are in drawable-hdpi folder. I have just one background image and a progress bar(circle) in the center on the splash screen and I making number of web service calls. I am not able to get hold off the resource which is causing this issue – Ashwin May 23 '12 at 18:34
  • I can't believe it! I was already desperate from all of those memory problems.. and you saved me! – user1787773 Dec 16 '12 at 12:03
10

You are probably trying to decode a very big Bitmap which results in an OutOfMemory exception. It means that the operation you are trying to achieve is exceeding the VM Budget allowed for each application on your device, in terms of heap memory consumption (which appears to be 24 MB on your device, probably more on your emulator which is why it doesn't happen there!).

Try to sample your Bitmapby a factor of two for instance:

BitmapFactory.Options o = new BitmapFactory.Options();
o.inSampleSize = 2;
Bitmap b = BitmapFactory.decodeFile(pathToBitmap, o);
Amokrane Chentir
  • 29,907
  • 37
  • 114
  • 158