Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

____________________________S

BEST PRACTICES A&D SC WM

WHITE PAPER
BEST PRACTICES
http://www.siemens.com.br/wm

____________________________
04-07-2007 pág. 1/15
____________________________S
BEST PRACTICES A&D SC WM

Summary

Preface/Facts pag.3

Benchmarking/Monitoring pag.5

Code Style pag.6

AT Command pag.9

Memory pag.12

Performance pag.13

Hints & Tips pag.14

Recommended bibliography pag.15

____________________________
04-07-2007 pág. 2/15
____________________________S
BEST PRACTICES A&D SC WM

Preface/Facts

J2ME is intended for devices with limited memory resources and processor power, such as cell
phones and Personal Data Assistants (PDAs).

The configuration is a specification, which relies on features and facts of the device:
- types and amount of memory
- processor type and speed
- type of network connection available to the device

In the figure below we show the Java architecture:

Glossary

KVM à Kilobyte Virtual Machine


CLDC à Connect Limited Device Configuration (for more than 32KB RAM)
CDC à Connect Device Configuration (more than 2 MB RAM)
MIDP à Mobile Information Device Profile
PDAP à Personal Digital Assistant Profile
IMP à Information Module Profile

____________________________
04-07-2007 pág. 3/15
____________________________S
BEST PRACTICES A&D SC WM

CLDC - Connected Limited Device Configuration

Profiles complement a configuration by adding additional classes that provide features


appropriate to a particular type of device or to a specific vertical market segment.

Necessities of CLDC:
- save memory
- structure the code to get it as small as it can be

____________________________
04-07-2007 pág. 4/15
____________________________S
BEST PRACTICES A&D SC WM

Benchmarking/Monitoring

Memory
Available methods in the Runtime class
- freeMemory()
- totalMemory()

Performance/Execution Time
§ System time
- currentTimeMillis()

Check the memory used by an object

Check the execution time of a code fragment

§ Hint: measure multiples and calculate an average

____________________________
04-07-2007 pág. 5/15
____________________________S
BEST PRACTICES A&D SC WM

Code Style
General instructions

§ Set no-longer-used variables referencing big objects to null


§ Run System.gc() at regular intervals, especially after 'nulling' objects
But be careful: gc() might slow down code execution, best to be run when no timing-critical
code is running
§ Try to re-use objects
§ Use static or static final variables where possible
§ Keep in mind that class code and variables are loaded on demand and will not be cleared by
the garbage collector
§ Try to reduce number/size of constants by sorten or compile them out:

§ Try to reduce the count of objects (potentially stored in arrays or vectors) and use one class
working on data directly

instead implement
arrayObject[num].do(<args>);

use
singletonObject.do(num, <args>);

Creating objects / Loop conditions

§ Create only objects you really need


§ Avoid creating new objects, especial in loops
§ Call operations only if needed (performance)

____________________________
04-07-2007 pág. 6/15
____________________________S
BEST PRACTICES A&D SC WM

Bad style:

Better style:

Cleaning UP

§ Set objects to null


§ Use try, catch, finaly to clean up in any case

Bad style:

____________________________
04-07-2007 pág. 7/15
____________________________S
BEST PRACTICES A&D SC WM

Better style:

____________________________
04-07-2007 pág. 8/15
____________________________S
BEST PRACTICES A&D SC WM

ATCommand
Attention
It is extremely important to create only one AT command instance for the entire application

See below an example for the execution of one AT command trough Java:

In case of use various AT commands, the example below is a better style of code:

____________________________
04-07-2007 pág. 9/15
____________________________S
BEST PRACTICES A&D SC WM

Another important recommendation is to insert a delay after ATs command to prevent errors.

____________________________
04-07-2007 pág. 10/15
____________________________S
BEST PRACTICES A&D SC WM

Using the “SyncATConmmand” Siemens class (see below), which synchronizes the AT commands,
avoids the execution of 2 or more AT commands at the same time.

Tip: Take the maximum advantage of the modules using the java in order to organize your projects
in an object-oriented way.

è Join Methods with similar characteristics in the same class.


è Join Classes with similar characteristics in the same package.

Avoid put all your application logic into Execution class (startApp ()). Program in a modular way
facilitates an overview of the project, without counting that in this direction, the possibilities of reuse
the code are much bigger.

____________________________
04-07-2007 pág. 11/15
____________________________S
BEST PRACTICES A&D SC WM

Memory

Strings

§ Strings are the only objects where the "+" operator is overloaded.
§ It is not complete clear when the runtime environment creates new objects and how much of
them.
§ Remember String and StringBuffer both work on character arrays in the background.
So if you want full control on your instances just use character or byte arrays

Vector and Hashtable

§ With these objects it is easy to handle a vary number of objects or data. Remember that these
structures grow larger when needed and this is expensive. New objects are created and a lot
of copy work must be done
§ If you use them try to initialize the size of the instance as correct as possible. Both classes
have constructors where you can set the initial value.
If you do so no resizing or rehashing must be done and you save performance.

____________________________
04-07-2007 pág. 12/15
____________________________S
BEST PRACTICES A&D SC WM

Performance

Reading/writing on streams

§ avoid reading/writing byte by byte


§ use buffered stream methodes

Bad style:

Good style (buffered reading):

Solution:
- a couple of bytes read
- in best case only one loop is done (data < buffer)

____________________________
04-07-2007 pág. 13/15
____________________________S
BEST PRACTICES A&D SC WM

Hints & Tips

Partition your application


- Classes are loaded when they are needed
- Avoid very large classes/objects
- TC65 about 50kb

Use an Obfuscator
- Reduce the size of the class files
- Keeps your application slim
- Makes the execution faster

Use a readable code style

Take care for naming conventions


- Class names starts with a capital letter
- Variable names starts with a small letter
- Constants uses capital letters only

Generate a JavaDoc

Comment the code

Flow charts are useful

____________________________
04-07-2007 pág. 14/15
____________________________S
BEST PRACTICES A&D SC WM

Recommended bibliography

J2ME in a nutshell (O‘REILLY)

Effective Java „Programming language Guide“ (Joshua Bloch)

Wireless Java „Developing with J2ME“ (Jonathan Knudsen)

http://java.sun.com/

http://proguard.sourceforge.net/

____________________________
04-07-2007 pág. 15/15

You might also like