0

Possible Duplicate:
OutOfMemoryError: bitmap size exceeds VM budget :- Android

I've researched this problem up and down and I can't find anything about my specific problem. I use .PNG files located in my drawable folder for button backgrounds. The buttons are large (about 150p x 150 p) and use images and gradiants to look nice. The backgrounds change upon pressing the button as defined in individual .xml files. The solutions i find in google seem to be for images called programmaticly and not images that are specified in layouts. When I constantly exit and enter different activities with different layouts and different buttons I will eventually get the out of memory error

04-27 22:18:46.227: E/dalvikvm-heap(512): 396900-byte external allocation too large for this process.

04-27 22:18:46.427: E/AndroidRuntime(512): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

My question is how do I allocate more memory when my images are specified in layouts and are not added programaticlly?

Community
  • 1
  • 1
John P.
  • 4,358
  • 4
  • 35
  • 47
  • Do you have large image backgrounds too? 150px x 150px images are not that big. 150 * 150 * 4 (4 bytes per pixel assuming a 32-bit bitmap) = 90,000 bytes which is far less than 396,900 bytes. – kabuko Apr 27 '12 at 22:40
  • I have 6 of these images per screen and my background is a simple hex color, no large images at all. In my desktop if I right click one of my .PNG's it says it is 40 kb's. When I remove this button image I do not have any issues. Is this image simply too big? The next biggest size is 20kb – John P. Apr 28 '12 at 01:32
  • Did you try using bitmap.isrecycled() – codeskraps Apr 27 '12 at 22:58

4 Answers4

2

follow how to display bitmaps efficiently, http://developer.android.com/training/displaying-bitmaps/index.html

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Longerian
  • 723
  • 5
  • 13
1

The following can help in figuring out what's eating your memory:

Add an UncaughtExceptionHandler and dump the heap when you get the OOM Exception. Use Debug.dumpHprofData() to dump the heap. After this, you convert the file with hprof-conv and check the resulting file using Eclipse Memory Analyzer.

Watch out for wrapped exceptions in the UncaughtExceptionHandler, sometimes the OOM Exception is wrapped in a RuntimeException.

Szabolcs Berecz
  • 4,081
  • 1
  • 22
  • 21
1

If you have multiple instances of the same Activity being created as you navigate around the app this could be creating your problem. Use the SINGLE_TOP or SINGLE_TASK Flags to fix this issue. Note though, that there appears to be a bug in Android as to the functionality of SINGLE_TOP. You will need to declare your Activity as SINLGE_TOP and launch the Activity using the SINLGE_TOP Flag. Here is the documentation: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

Swifty McSwifterton
  • 2,637
  • 1
  • 30
  • 37
0

Does it crash right away, or after a while? A few images shouldn't crash your app, even big ones. I've found in the past that lots of rotations leak memory until the app goes pop; in fact, that's where I see OOM issues the most. In case you have the same problem, it's because you keep a pointer to the context, directory or by keeping a pointer to a View.

Yusuf X
  • 14,513
  • 5
  • 35
  • 47