J2ME performance optimization tips & tricks

Preview:

Citation preview

Micro Java

PERFORMANCE TUNING & OPTIMIZATION

DEVICE LIMITATIONS• Runtime memory

• Processor speed

• Slow network bandwidth

• Screen resolutions

• Restricted storage

PERFORMANCE TUNING• Tips & tricks

• Good & ugly practice

• Performance tuning

• Tools usages

• Effective usages of design patterns

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

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

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

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

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

PERFORMANCE TUNING

BE CLEAN• Release resource

• Free the used internal data structures

• Set array references to null after usage

• Close network connections in finally block

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

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

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

BENCHMARKING• J2SE has many tools for benchmarking

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

• freeMemory()

• totalMemory()

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;

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

long start, finish;

start = System.currentTimeMillis();

someMethod();

finish = System.currentTimeMillis();

long duration = finish - start;

MIDP PERFORMANCE MONITOR TOOLSCollects the statistics of application

Tools used in J2ME wireless tool kit are:

• Profiler

• Memory monitor

• Network monitor

MIDLET PROFILER• Collects the statistics of application

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

• Includes MIDP reference implementation classes

Call Graph

MEMORY MONITOR Object Graph

NETWORK MONITOR Request Response

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