I'm new in Android programming. I use lots of images in my layout (and sounds also). Recently I usually have OutOfMemory Exception, and now I'm completely stuck. How can I prevent it ? Should I null the references to the unused layout items and let the GC work? Or should I recycle the image resources?
About my application: It would be a game, you can see some proposed screenshots about it below. Basically There is an Activity, and I would like to put about 9-10 inflated layouts within the same activity. The main reason that I want to put these layouts into the same Activity is, that they logically cohere, and I would like to switch swiftly amongst them.
However these layout-views consumes lots of memory resources (because of the pictures and sounds - for example), and the memory is fill up with them. Unfortunatelly, for some reason, the GC doesn't help, though I tried to null the references of the layout-views.
enter image description here
2 Answers 2
We would use more informations to really help you out. OutOfMemoryException is quite unuasual, what does your program? But look at these :
-You might want to look of some infinite loop/condition
-Audio and visual resources can be rather large, be sure to use compressed format.
-Don't load your entire resources when you start the program
-You can use functions to deallocate memory before the GC does, but again, this is unuasual, depending of your application
Please provide more informations about your application
Comments
This is a very broad question, if we don't know the design of your application. Most probably, it's either that your application has memory leaks, or that it simply has large memory needs by design.
If you do have memory leaks, you should by all means try to find them and close them. This is generally done by making sure that your references don't escape to places where they aren't really needed. Make everything as tightly scoped as possible. Setting references to null explicitly is rarely a good idea, because it's generally applicable in two cases: One case is if you're talking about automatic (local) variables, so setting them to null doesn't benefit your application at all (they'll be automatically ignored as references when they go out of scope). The other case if these references are fields in objects, in which case setting them to null may put your objects in invalid states (depending on the case), therefore delegating the state validity checking to every place in the code where you use those objects, making your whole code a mess. Now, to be clear: I'm not saying that setting references to null won't help, I'm saying that if you have to do that, you're likely keeping references that you shouldn't be keeping and so you have some kind of design flaw, not a lack of null assignments.
On the other hand, if your application is such that it actually requires lot of memory (given it doesn't have memory leaks), then you should redesign some functionality to consume a more conservative amount. For example, load in memory only the things that are really needed. Consider having them in a more compact format. If you're caching things but don't want the caching mechanism to keep them alive, consider using weak references.
Comments
Explore related questions
See similar questions with these tags.