Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.
How to calculate memory used by images in Java ME
In the Forum several people encounter the "Out of Memory" Exception when running their MIDlets containing many Images and/or a single big Image.
Most of the time (even the big Images) the file-size of the Images wouldn't necessarily be expected to be a problem within the application Memory Usage. 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, right ? Well, unfortunately, this is a wrong assumption !
Internally the OS will create a Bitmap-type mapping of the Image to be able to actually display it. This means that the mentioned 240x320 pixels Image will, depending on the screens color bit depth :
width * height * (color depth/8)
Let's say we got a 16 bit screen and a 320 x 240 pixels Image, this will result in :
320 * 240 * 2 = 153600 bytes ! Exactly 150 Kilobyte, which 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 !