Your SlideShare is downloading. ×
MELJUN CORTES Jedi course notes mobile application devt-lesson08-optimizations
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

MELJUN CORTES Jedi course notes mobile application devt-lesson08-optimizations

78
views

Published on

MELJUN CORTES Jedi course notes mobile application devt-lesson08-optimizations

MELJUN CORTES Jedi course notes mobile application devt-lesson08-optimizations


0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
78
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. J.E.D.I. Optimizations1 ObjectivesAfter finishing this lesson, the student should be able to:• know the different techniques in optimizing mobile applications2 OptimizationBefore actually doing any optimizations on your program, you should make sure thatyour software package is of good quality. You should put optimizations last in youragenda. Some techniques discussed in this section should be helpful in avoiding someprogramming mistakes.3 Program Execution3.1 Use StringBuffer instead of StringYou should remember that in Java, String objects are immutable. Using the Stringmethods creates separate String objects. The simple String concatenations createmultiple String objects (unless the strings are constants and the compiler is smartenough to concatenate them at compile time). Using a StringBuffer not only optimizesyour programs runtime (less object creation runtime), it also optimizes the use ofmemory (less String objects to create).Mobile Application Development 1
  • 2. J.E.D.I. String StringBufferString a, b, c; String a, b, c;... ...String message = StringBuffer message = new StringBuffer(255); "a=" + a + "n" message.append("a="); + "b=" + b + "n" message.append(a); + "c=" + c + "n"; message.append("n"); message.append("b="); message.append(b); message.append("n"); message.append("c="); message.append(c); message.append("n");3.2 Use a clipping area when drawingUsing Graphics.setClip() will reduce execution time because you will only be drawing theoptimal number of pixels on the screen. Remember that drawing graphics on the screencosts a lot in terms of execution time. Reducing the number of pixels to be drawn wouldgreatly affect your programs runtime performance.Graphics g;int x1, y1, x2, y2;...g.setClip(x1, y1, x2, y2);g.drawString("JEDI", x, y, Graphics.TOP | Graphics.HCENTER);// more drawing operations...3.3 Avoid synchronized modifierUsing the synchronized modifier will take a hit on your programs execution speed as itwill have to do some extra measures so that it will not be accessed concurrently.Mobile Application Development 2
  • 3. J.E.D.I.3.4 Pass as few parameters as possibleWhen calling a method, the interpreter will push all the parameters on to the executionstack. Passing many parameters will affect execution speed and the use of the HeapMemory.3.5 Reduce method callsMethod calls costs heap memory and execution time. See previous item.3.6 Delay all initializationsTo speed up application start time, delay all costly initializations until they are needed.Do not put initializations in your MIDlets constructor or startApp method. Speeding upthe application load time will add to the usability of your application. Most users wouldshy away from applications that take a long time to start up. Remember that yourapplications load time directly affects the users first impression of your program.3.7 Use arrays instead of CollectionAccessing arrays are faster than using a Vector.3.8 Use local variablesIt is faster to access local variables than accessing instance variables.4 JAR Size4.1 Use an obfuscatorThe obfuscators original intention is to mangle the compiled class files so that it wouldbe difficult to decompile. But the process of obfuscating also reduces the application size.One of the methods employed by the obfuscator is to rename the classes into singleletter names. For the obfuscator to do this, it relies on the modifier of the methods. Ifthe method has a private or protected modifier, then it can safely assume that thismethod can not be used by other packages and can therefore be renamed.Mobile Application Development 3
  • 4. J.E.D.I.Netbeans and Mobility Pack comes with an obfuscator. It is not enabled by default. Openthe applications property tab and click on the "Obfuscating" branch:There are ten levels of obfuscation, from no obfuscation up to the most aggressiveobfuscation:Mobile Application Development 4
  • 5. J.E.D.I.4.2 Compress your JAR filesMake sure that before distributing your application, you compress the final JAR file fordistribution. A JAR file is a ZIP archive, and a ZIP archive has several levels ofcompression (including no compression). Netbeans does not support the level ofcompression.To set the JAR compression option, open the applications property page and select the"Creating JAR" branch. Check the radio box "Compress JAR" to compress your projectsJAR file. Do not forget to rebuild your project.Mobile Application Development 5
  • 6. J.E.D.I.4.3 Avoid creating (unnecessary) classesThis might seem contradictory to object-oriented principles, but do you know that asimple empty class such as:public class EmptyClass { public EmptyClass(){}}could be compiled to a class file with as much as 250kb (uncompressed) file size?You can try compiling this empty class and see for yourself. Netbeans stores thepackaged JAR file in the dist folder under the project folder. You can rename the .jar fileinto a .zip file and open with you favorite ZIP compression program to see the sizes ofyour compiled class files.Mobile Application Development 6
  • 7. J.E.D.I.4.4 Avoid creating interfacesThis technique is related with the previous technique. Having more classes and interfacesadds more (kilo)bytes to your application.4.5 Avoid inner and anonymous classesSame as above. Inner classes are classes all the same. Anonymous classes may have nonames, but they take up the same space for class definitions.4.6 Use a single Listener for multiple objectsThis will reduce the number of classes in your application. Making your MIDlet implementthe CommandListener interface will help you trim your package by one class (thats250+ bytes less!).4.7 Use "default" package (no name package)In our quest for small package size, shortening (and not using) package namescontributes in byte reduction.4.8 Limit use of static initializersUsing static initializations like:int[] tones = { 64, 63, 65, 76, 45, 56, 44, 88 };would be compiled by the Java compiler into these statements:tones[0] = 64;tones[1] = 63;tones[2] = 65;tones[3] = 76;tones[4] = 45;tones[5] = 56;tones[6] = 44;Mobile Application Development 7
  • 8. J.E.D.I.tones[7] = 88;This example illustrates only eight array members. Imagine initializing hundreds ofvalues using separate statements. That would mean a lot of overhead on yourapplications size.As an alternative, you can use the method getResourceAsStream() to get values from afile or use a single string to store your array values.4.9 Combine images into one fileImages compress better when grouped into a single image file. Thats because thecompression of the image format (say PNG) is more specific to images than thecompression method of JAR archiving. There are techniques on getting the specific imagefrom the larger image like clipping.4.10 Experiment with image compressionCompression methods are not created equal. Some may compress better on some imagetypes but have poor compression ratio on other image types. Choose an image formatthat would improve the compression ratio of your images. Sometimes, the compressionratio is also affected by the image software you are using. Experiment with differentimage manipulation programs to come up with better image sizes.4.11 Use pre-installed classesDo not re-invent the wheel. Use all applicable classes available on the platform you areusing. Making your own will would not only add to your applications size but alsodecrease your applications stability.5 Networking5.1 Use a separate threadUse a separate thread for your networking functions to avoid screen lockups.Mobile Application Development 8
  • 9. J.E.D.I.5.2 Compress network dataUse compressed data to lessen the network traffic of your application. This would requirethat your client and server use the same protocol and compression method.Compressing XML will give a better ratio because XML data is represented in text format.5.3 Reduce network trafficSince network communications is slow and costly, try as much as possible to put into asingle network request several commands. This will reduce the overhead imposed by thenetwork protocols.6 Memory Usage6.1 Use more compact data structuresUse memory friendly data structures. Sparse arrays may be represented in other wayswithout consuming the same amount of memory.There is a tradeoff when optimizing for size and speed. Using complex data structuresmight affect your programs execution speed.6.2 Release unused objects for garbage collectionRelease unused objects for garbage collection – screen, network connections, RMSRecords. Setting the variables pointing to the unused object to null would give a hint tothe garbage collector that this object is safe to unload from memory.6.3 Create seldomly used screens on-the-flyCreating seldomly used Screen objects (like help and about screens) on-the-fly will freeup your much needed heap memory, although you have to pay the price of slowerloading for these particular screens. The heap memory these screens would supposedlyoccupy while they are not being used might help in our resolve for memory conservation.Mobile Application Development 9
  • 10. J.E.D.I.public void commandAction(Command c, Displayable d) { if (c == helpCommand) { display.setCurrent(new HelpForm()); }}Mobile Application Development 10
  • 11. J.E.D.I.7 Exercises7.1 Other Optimization IdeasDiscuss other optimization ideas you have in mind or techniques you have developed.Encourage the class to discuss more optimization techniques.Mobile Application Development 11