Alex
Alex

Reputation: 1232

Memory issues loading images for app

Describing background, as I may just have a terrible approach to the problem - self learning.

I'm writing an app for android, and testing it on default AVD, which is set to WVGA800 with 512 'device ram size' and 240 'Abstracted LCD density'.

I have some images, and I put them into drawable-hdpi. There are 458 KB (not MB) worth of images in that folder. All images are in PNG format.

The issue is that when I try to load my biggest image (used for background), it throws: java.lang.OutOfMemoryError This is the call to load the image:

BitmapFactory.decodeResource(status.getResources(), R.drawable.background);

This is identical to how I load the rest of images (33 in total).

It makes sense to me, that it would run out of memory on biggest image, but my total size of folder is 458 KB, so I wouldn't expect to run out of 512 MB Ram set on device.

I never unload any images, I keep them loaded, and use as needed. I wrote a different app before, where my total size of images was 563 KB with 82 images total, and I didn't have this issue (using the same AVD). In fact the prior app used to make a couple of copies of each image by flipping it, and still didn't run out of space. Current app is failing on initial load - before much happened.

Could someone point me at what the issue could possibly be? And how I can solve it, or maybe mention if my approach is wrong (self-teaching myself from examples)

Upvotes: 0

Views: 174

Answers (3)

kungfoo
kungfoo

Reputation: 597

Yea, this is a pretty common problem. So in older versions of android OS, the bitmap was loaded into native memory and not the JVM. The garbage collection process would really have 2 cycles. One to clear out the memory in the JVM and the other to clear out the memory in the native memory (for bitmaps). If you want to work on older devices, you will need to handle this situation by either recycling your bitmaps Bitmap.recycle() or calling System.gc()

There are two problems that you might be hitting: 1. You have other bitmaps that are un recycled. 2. You really are running out of memory because that single image is too big. (Make sure the other images are correctly recycled or gc'd so that it doesn't add to the memory footprint). In this case, no much you can do.

ALso, as mehmet suggested, you can read this

Upvotes: 1

WillingLearner
WillingLearner

Reputation: 739

I'll give you some hints on how i manage to lessen that problem

  • If you plan to support all devices, put all your resource into the xhdpi folder. especially the background
  • File size != memory size

Upvotes: 2

aromero
aromero

Reputation: 25761

Keep in mind a few things:

  • Your application has a memory limit (this depends on the android version). You don't get all the device memory. I think that first android version have a memory limit of 16mb.
  • The size of the file doesn't represent the size of the bitmap in memory. For example a 32bit ARGB bitmap will take 32*width*height bits
  • If you are dealing with big images then scale them first. Calculate the size you need (this will probably be the size of your ImageView) and load a resized copy of the bitmap. You can do this using BitmapFactory.Options

Upvotes: 1

Related Questions