OutOfMemoryException in the Emulator OutOfMemoryException in the Emulator android android

OutOfMemoryException in the Emulator


Obviously I haven't seen the bitmaps you're loading, but my guess would be that they're a lot larger than they need to be for the space you're displaying them on. If you download, for example, a 3000 x 2000 image and then assign it as the source of an ImageView that's only 300 x 200, the original Bitmap size is still stored in memory and just displayed smaller. It's important that, when downloading an image from the web, that you resize the received Bitmap to the display size before you set it as the source of the ImageView. Not doing so will consume huge chunks of memory.

This is quite a lot of code as you need to guard against lots of various error conditions during this process. The easiest solution I can offer you is to try the Picasso library. You can pass it the URL you want to download with code similar to that shown below and it will download it on a background thread, resize it and put it into the ImageView when it's ready. It will also cache the resized version so that, if you need to download the same image, it uses the cached version if it is suitable for the ImageView you're loading it for. This means you don't waste network traffic as well. If you need to do some transformations to the image before it is placed in the ImageView, you can do that as well by providing a Transform object. You can also set a placeholder image, which you should have in your drawable folders and it will display that until the actual image has been downloaded.

Picasso.with(context)       .load("http://i.imgur.com/DvpvklR.png")       .into(imageView);


Basic steps

According to this answer, there are some basic steps you should follow in this case:

  • On Android 3.0+, use inBitmap on the BitmapOptions that you pass to BitmapFactory, to reuse existing memory as opposed to allocating new memory
  • recycle() your Bitmap objects when you are done with them
  • Be generally careful about your memory allocations, as Android's garbage collector is non-compacting, so eventually you will be incapable of allocating large blocks of memory again
  • Use MAT to see if you are leaking memory somewhere that is contributing to your problem.

Some posts that might be helpful

See this post about Managing Bitmap Memory, and also this about Loading Large Bitmaps. These might help you.

Also you should handle your view recycling on your ListView. See this post about Handling ListView Recycling.

Hope this helps you.


I think the problem is that you're leaking the current Activity. You're passing this as the first parameter, which in this case is a Context. So, you're using the current activity as the context and the next thing you do is finish it. The activity gets closed, but its memory is leaked because you passed a reference to the intent.

Try this:

Intent intent = new Intent(getApplicationContext(), SS3GamesActivity.class);