Your Ad Here

Wednesday, May 30, 2007

J2ME Optimization Tips

Hi Folks,
Optimization is very important while you are programming for small devices , optimization can be broadly divided into two category.
Optimization to reduce JAR Size.
In earlier days there is used to be 64K limit for jar size for most of the nokia devices and most of the carrier , still this is important because market share of these devices still holds significant number.

JAR Size is contributed by two things code + resource , resource includes images and audio , video file , since in most of the cases resource contribute around 60% of jar size start with optimizing it , for this
1  use optimized images , whose size is less but they look decent , there are several tools comes for this e.g. PNG Optimizer etc.
2.    use optimized audio files , midi files are always less size than wav and wav is always less than mp3 , so wherever possible use optimize audio files.
3.    For high optimization some coder create a  compress binary file with all the resources and load them while your application starts up , though this leads to increase the startup time which should not be more than 3 seconds to pass some of the career testing or certification e.g. NSDL testing of BREW devices etc.
Code Optimization
If all your resource is optimized and you have to optimized code then you need to reduce the byte code size , starts with easy one then go for byte code  instrumentation.
1.    Use a good quality Obfuscator , proguard is one of good one.
2.    Minimize the number of classes in your application , each class ads up to 200 Bytes into JAR Size so try to minimize the classes. Though this is against the principal of OOPS which advocate
3.    Individual classes for individual responsibility you need to make trade off with this while programming for small devices.
4.    Don’t use Inner class these leads additional meta data byte code ,reducing inner class will reduce jar size significantly , I have seen the code where people use inner class to implement Runnable or CommandListener avoid these pattern.
5.    Use classes whose size are less e.g. use Short instead of Integer , though I would suggest avoid using Wrapper classes instead of these use primitive.
I would add some more techniques and also write about performance improvement for small devices when I get some time , so keep checking.