Skip to content

J2ME memory optimization tip. Multidimensional arrays.

Mobile phone is a resource constrained device, that imposes some challenges for application developers.

Quite often it is very convenient to use multidimensional arrays, though when doing so, we have to keep in mind that there is an overhead price to pay, that is, for every array object created, overhead == 16 bytes.
int array of size:0 uses 16 bytes of heap
int array of size:1 uses 20 bytes of heap
int array of size:2 uses 24 bytes of heap

For large data structures built with multidimensional arrays, you can oftentimes reduce the extra dimension overhead by an easy indexing change: convert every int[dim1][dim2] instance to an int[dim1*dim2] instance and change all expressions like a[i][j] to a[i*dim2 + j]. Of course, you pay a price from the lack of index-range checking on dim1 dimension (which also boosts performance).

For more tips on memory optimization, see javaworld’s java tip 130.

technorati tags:

4 Comments

  1. pompeybob44uk wrote:

    I believe the formula for mapping expressions from the form a[i][j] is incorrect. Should it not be a[i*dim2 + j]?

    Wednesday, February 14, 2007 at 2:55 am | Permalink
  2. alexey wrote:

    Thank you Bob.
    I’ve corrected the article to reflect your fix.

    Wednesday, February 14, 2007 at 11:16 am | Permalink
  3. bhushan wrote:

    Hello I want to know how to optimize the heap size in J2ME.
    What are the most favoured techniques of optimization

    Sunday, August 12, 2007 at 10:07 pm | Permalink
  4. naisioxerloro wrote:

    Hi.
    Good design, who make it?

    Wednesday, November 28, 2007 at 2:13 pm | Permalink

Post a Comment

You must be logged in to post a comment.