问题
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?
回答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.
回答2:
follow how to display bitmaps efficiently, http://developer.android.com/training/displaying-bitmaps/index.html
回答3:
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
回答4:
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.
来源:https://stackoverflow.com/questions/10358637/android-vm-out-of-memory-error