×
Namespaces

Variants
Actions

Java ME code optimization

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: vivartpandey (10 Jan 2010)
Last edited: hamishwillee (29 Jul 2013)

J2ME applications usually do not run like native C++ applications. The performance is always penalized in terms of speed and memory footprint (RAM). For performance tuning of any J2ME application certain factors can be considered while writing programs(coding) like I/O, threading etc. This article contains tips discussing how to improve performance of a J2ME application by considering many such factors.

Contents

Tips

Creating and Discarding Objects

Object creation is very costly with respect to memory and performance overhead. new objects in Java can be created using the new keyword. So create objects only when its needed, don’t create objects in loop like

for (int i = 0; i < length; i++) {
ClassName p = new ClassName ();
results[i] = p.calculateResult();
}

Strings and StringBuffers
Whenever you are using substring or “+” it’s always creating a new string objects. So if you are manipulating strings in your code like substring, string concatenation, use StringBuffers or char array.

Optimize Loops

1. Use “for” loop over “while”: 2. Don’t calculate anything in condition of loop:

//bad
for (int i = 0; i < v.size(); i++) {
// Do something
}
//good
int size = v.size();
for (int i = 0; i < size; i++) {
 
// Do something
}
//better
for (int i = 0,n = v.size(); i < n; i++) {
 
// Do something
}
/*if the order in which you iterate through items is not important, you can iterate backward to avoid the extra local variable on the stack. Comparing against zero is almost always more efficient in any language*/
 
for( int i = v.size() - 1; i >= 0; i-- ) {
// Do something
}

Partition Your Application

The MIDP runtime environment loads classes as they are needed. Divide your big classes into small classes.

Only Include Classes You Need: Don’t use wild character imports like java.util.* instead of this use like java.util.Date;

Have public variables if its possible: Have variables public if possible, because if member variables are private to access them getter and setter methods are needed, calling them is a performance overhead, so if no validation is needed while modifying a variable have it public so that it can be easily accessed.

Avoid Threads

Whenever possible avoid threads in the midlet which are memory and CPU overheads. The activities like I/O are thread based only so its not possible to avoid threads then in such cases try to avoid I/O itself. For example if persistent storage is needed then use RecordStores instead of files.

When threads are used sometimes synchronization is also required which slows down the performance by at least 2/3 times, as maintenance of variables and their state is necessary as per the thread access.

Avoid Frequent opening of Recordstores

If using Recordstores, group the read and writes at one place as much as possible, this will require careful application design. Spreading RecordStore read and writes across the application will hamper the performance.

Also RecordStore opening and closing operations needs to be minimized with proper design again.

Reuse GUI components

Reuse GUI components like Forms,TextBox etc. instead of creating them for every form if possible, this helps in keeping memory footprint low.

Use Appropriate type for variables

Though not much possible in J2ME but try to use appropriate types for variables. Using appropriate type of variable avoide wastage of RAM.

Reuse Variables

Try to reuse variables as far as possible, allocating to many auto variable slow downs the performance and consumes memory. Frequent allocation and deallocation of variables is an overhead. Bur reusing variables means making them global by allocating them in class, this should be carefully done without compromising on class design.

Array-spreading

Divide your multidimensional arrays into single dimensional arrays. Multidimensional array access is always a performance penalty, try to avoid them.

Calculations

Wherever possible try to use shift operator for mathematical calculation. int a = 11<< 1 instead of int a = 11* 2

Also avoid operations like multiplication and division as far as possible, they are costlier than addition and subtraction. Try to replace multiplication and division based computations using addition and subtraction as much as possible.

Use Native Methods

Like String.indexOf(), String.lastIndexOf()

Inner Classes

For inner classes choose package scope with static.

User obfuscater for j2me applications;

Don't call garbage collector in your code

System.gc();

Avoid synchronization, if you can Its very costly performance wise.

Managing loops whenever possible

Loops are computation overheads, though they make the life easy, so manage loops wherever possible. If the number of times the loop will be iterated is known in advanced then repeat few lines of code instead of having the loop to do that.

For example

for(int i = 0; i < 20; i++)
{
j += 10;
}

Above can be done as

for(int i == 0; i < 5; i++)
{
j += 10;
j += 10;
j += 10;
j += 10;
}

Try to keep Polymorphism away

In Java polymorphism really helps in designing a great code ( recall all design patterns ), but polymorphic behavior of class and method calls are the costliest calls in Java. The most efficient calls are static method calls, of course but not all methods can be static.

This page was last modified on 29 July 2013, at 09:11.
101 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×