How to calculate memory used by images in Java ME
This article explains how to determine the "real" memory consumed when loading an image in Java ME.
It is not uncommon for developers to get an "Out of Memory" Exception when running MIDlets containing many Images and/or a single big Image.
Often this is unexpected - the file-size of the Images wouldn't appear to put the app in danger of going over app memory limits. This is because people assume that a 7 kilobyte Image which is a 240x320 pixels plain white Image shouldn't create any memory problem, after all it's just 7 kilobyte!
Unfortunately, this assumption is incorrect!
Internally the OS will create a Bitmap-type mapping of the Image to be able to actually display it. This means that a 240x320 pixels Image will consume:
width x height x (color depth/8)
Let's say we got a screen with 16 bit colour-depth and a 320 x 240 pixels Image. This will result in: 320 x 240 x 2 = 153600 bytes. 150 Kb is a lot more than the 7 Kilobyte of the original Image file-size !
So now that you know what the size and memory usage of an Image means for you as a developer you will be more aware on how to use Images and know what the impact of an Image might be on the memory usage of your application.