J2ME performance optimization tips & tricks

22
Micro Java PERFORMANCE TUNING & OPTIMIZATION

Transcript of J2ME performance optimization tips & tricks

Page 1: J2ME performance optimization tips & tricks

Micro Java

PERFORMANCE TUNING & OPTIMIZATION

Page 2: J2ME performance optimization tips & tricks

DEVICE LIMITATIONS• Runtime memory

• Processor speed

• Slow network bandwidth

• Screen resolutions

• Restricted storage

Page 3: J2ME performance optimization tips & tricks

PERFORMANCE TUNING• Tips & tricks

• Good & ugly practice

• Performance tuning

• Tools usages

• Effective usages of design patterns

Page 4: J2ME performance optimization tips & tricks

TIPS & TRICKS• Use “Out of memory Exception” handlers on each initialization

• Avoid inner classes: make the main class implement the required Listener interfaces and handle the call-backs there.

• Collapse inheritance hierarchy, even it means duplicating code

• Shorten all name qualifiers (package, class, method, variable names)

• Minimize the number of array

• Reduce try-catch blocks to bare minimum

• Use lower versions of JDK while packaging JAR files, JAR File size created using JDK 1.3 is 5% to 20% lesser than those created with JDK1.4.

• Scope of variables/classes/methods can impact performance

• Eliminate unnecessary type casts

Page 5: J2ME performance optimization tips & tricks

TIPS & TRICKS-II• Compound operators are faster, because fewer byte codes are generated

• Define methods that accept reusable objects to be filled in with data, rather than methods that return objects holding that data

• Inline methods where ever it is possible.

• Use private and static methods and final classes to encourage in lining by the compiler.

• keep synchronized method out of loop, it increases the overhead

• Avoid synchronization where ever its possible

• Minimize using the methods use native call

• Be specific for throwing and handling exceptions

Page 6: J2ME performance optimization tips & tricks

CREATING OBJECTS IN LOOP

• Imposes double penalty in terms of performance

• Each iteration in loop pushes the runtime system closer to garbage collector

• Restructure code instead creating new processor each time

Page 7: J2ME performance optimization tips & tricks

Object[] inputs = new Object[0];

int[] results = new int[0];

int length = inputs.length;

Processor p = new Processor();

for (int i = 0; i < length; i++) {

p.setInput(inputs[i]);

results[i] = p.calculateResult();

}

Object[] inputs = new Object[0];

int[] results = new int[0];

int length = inputs.length;

for (int i = 0; i < length; i++) {

Processor p = new Processor(inputs[i]);

results[i] = p.calculateResult();

}

Bad practice Good practice

Page 8: J2ME performance optimization tips & tricks

STRING & STRINGBUFFER

• String is only the data type which can overload + operator in java

• Each time you connate a string it inters creates a string buffer and string buffer creates

performs over byte array

• String buffer in efficient then string, but not always

Page 9: J2ME performance optimization tips & tricks

PERFORMANCE TUNING

Page 10: J2ME performance optimization tips & tricks

BE CLEAN• Release resource

• Free the used internal data structures

• Set array references to null after usage

• Close network connections in finally block

Page 11: J2ME performance optimization tips & tricks

OPTIMIZING USER INTERFACE• Need to be simple, fast, informative and responsive

• Progress indicator while network / other processing

• Usage of low resolution images maintaining the minimal quality

• Use double buffering technique for images where ever it applies

• Use client caching

• Use paint or partial paint the screen where ever necessary

Page 12: J2ME performance optimization tips & tricks

OPTIMIZING APPLICATION DEPLOYMENT• Usage of obfuscator packages

• Usage of low resolution images maintaining the minimal quality

• Include only the classes you need

• Use public variables in your classes, rather than using assessors. It is practically bad practice but reduces app size

• Use protected methods

Page 13: J2ME performance optimization tips & tricks

USE OF OBFUSCATOR • Reduces .jar size

• Byte code obfuscator makes the code difficult to decompile

• Side effect- descriptive method and variable names gets converted to small machine generated names

• Obfuscators are

• Retro guard

• Pro guard

• IBM JAX

Page 14: J2ME performance optimization tips & tricks

BENCHMARKING• J2SE has many tools for benchmarking

• In ME we use “java.lang.runtime” class methods for memory test

• freeMemory()

• totalMemory()

Page 15: J2ME performance optimization tips & tricks

KNOWING THE RUNTIME FREE MEMORYRuntime runtime = Runtime.getRuntime();

long before, after;

System.gc();

before = runtime.freeMemory();

Object newObject = new String();

after = runtime.freeMemory();

long size = before - after;

Page 16: J2ME performance optimization tips & tricks

KNOWING EXECUTION TIME FOR A METHOD• Use System.currentTimeMiles()

long start, finish;

start = System.currentTimeMillis();

someMethod();

finish = System.currentTimeMillis();

long duration = finish - start;

Page 17: J2ME performance optimization tips & tricks

MIDP PERFORMANCE MONITOR TOOLSCollects the statistics of application

Tools used in J2ME wireless tool kit are:

• Profiler

• Memory monitor

• Network monitor

Page 18: J2ME performance optimization tips & tricks

MIDLET PROFILER• Collects the statistics of application

• To monitor the method call and time to execute a method

• Includes MIDP reference implementation classes

Page 19: J2ME performance optimization tips & tricks

Call Graph

Page 20: J2ME performance optimization tips & tricks

MEMORY MONITOR Object Graph

Page 21: J2ME performance optimization tips & tricks

NETWORK MONITOR Request Response

Page 22: J2ME performance optimization tips & tricks

CONCLUSION• Only optimize code if you need to and where it counts

• Always study your code and try to improve the algorithms before using low-level techniques

• Use setClip() where possible to minimize the drawing area

• Keep as much stuff as possible out of loops

• Pre-calculate and cache like crazy

• Use static final methods where possible and avoid the synchronized modifier

• Pass as few parameters as possible into frequently-called methods

• Try to compare to zero instead of any other number

• Local variables are faster than instance variables