0

I’m learning java as I learn how to program in Android. I was under the impression that java handles all memory management for you, but this does not seem to be true. My small apps ran fine, but now I’m working on a puzzle app for toddlers that has a lot of graphics. I’ve been getting a lot of out of memory exceptions after the app runs for a while. Currently it runs fine, but I solved the issue by setting the bitmap pointers to null when I’m done with them. At the end of each service, I set all the bitmap objects to null and its working.

It works, but I don't understand if I’m supposed to free the pointers in java?

jvdhooft
  • 657
  • 1
  • 12
  • 33
Ted pottel
  • 6,869
  • 21
  • 75
  • 134
  • 1
    you need to call `recycle()` on bitmaps when you are done with them to free up the memory – tyczj Aug 08 '13 at 16:04
  • possible duplicate of [Android: out of memory exception in Gallery](http://stackoverflow.com/questions/3238388/android-out-of-memory-exception-in-gallery) and http://stackoverflow.com/questions/2298208/how-to-discover-memory-usage-of-my-application-in-android?rq=1 – Simon Aug 08 '13 at 16:12
  • [Displaying Bitmaps Efficiently](http://developer.android.com/training/displaying-bitmaps/index.html) just as a good start to improve your understanding – WarrenFaith Aug 08 '13 at 16:31

1 Answers1

0

Setting the bitmap to null will allow the garbage collector to run and clean it up (you can speed up the process by calling recycle first). If you don't need the image, it is best to do this otherwise holding onto lots of large images will quickly use all the memory allowed for your app to run (android doesn't give you a whole lot).

If the image goes out of scope this will happen automatically (but again calling recycle will speed this process along), but if you have a lot of global static images that hold on to a lot of images, that is when you will get into trouble.

Kevin DiTraglia
  • 25,746
  • 19
  • 92
  • 138