corgichu
corgichu

Reputation: 2600

Android Bitmap OutOfMemoryError + How do games handle lots of images?

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!

Upvotes: 1

Views: 889

Answers (1)

acj
acj

Reputation: 4821

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.

Upvotes: 2

Related Questions