1

I started getting the OutOfMemoryError for my game. I thought it was because I was creating new Bitmaps for each animation (so there were multiple Bitmaps of the same image), but then I created an ImageLoader class to only load each Bitmap once then the objects that need the images just use a reference to them.

However, there are a lot of images in my game for animations, background, items, etc. I just added a few more items and now I keep getting the error.

How do other games handle having a lot of images? I'm sure many other games have more images than mine do and can still avoid this error.

Is there a different/better way to implement an ImageLoader than what I've been doing?

Thanks a lot, guys!

corgichu
  • 2,580
  • 3
  • 32
  • 46
  • The issue is more likely your ImageLoader than your quantity of images. I'd say post your ImageLoader code. – FThompson Oct 03 '12 at 03:01

1 Answers1

2

Game developers solve this problem by spending a lot of time optimizing image sizes and making use of memory caches.

It's important to know how much memory your app has to work with, and not to exceed it. You can set up an LRU (Least-Recently Used) cache that will ensure that the total memory footprint of your bitmaps stays under a certain size. You just insert the bitmaps into the cache, and once the cache reaches its maximum allowed size (something that you specify), it will begin to unload bitmaps that haven't been used in a while.

There some very good info, including code snippets, in this article from the Android documentation.

acj
  • 4,821
  • 4
  • 34
  • 49
  • thanks! but all the Bitmaps I'm loading so far are needed since I'm using them for my sprites' animation (which pop up randomly on the screen). =/ – corgichu Oct 03 '12 at 03:57
  • ah, ok. In that case, it might be helpful (after setting up a cache!) to check out the Allocation Tracker in ADT ([some explanation](http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html)). There might be some views/layouts that are using more memory than you expect. Yet another option is to [downsample your bitmaps](http://stackoverflow.com/questions/10081008/outofmemoryerror-in-game-with-many-small-images), but that will carry a loss of quality. Best of luck! – acj Oct 03 '12 at 12:36