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?

3
  • 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. Commented Apr 27, 2012 at 22:40
  • Did you try using bitmap.isrecycled() Commented Apr 27, 2012 at 22:58
  • 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 Commented Apr 28, 2012 at 1:32

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.