J2ME performance optimization tips & tricks


Published on

Published in: Education, Technology
1 Comment
1 Like
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

J2ME performance optimization tips & tricks

  1. 1. Micro Java<br />Performance Tuning & optimization<br />
  2. 2. Device limitations<br />Runtime memory<br />Processor speed<br />Slow network bandwidth<br />Screen resolutions<br />Restricted storage<br />
  3. 3. Performance Tuning<br />Tips & tricks<br />Good & ugly practice<br />Performance tuning<br />Tools usages<br />Effective usages of design patterns<br />
  4. 4. Tips & tricks<br />Use “Out of memory Exception” handlers on each initialization<br />Avoid inner classes: make the main class implement the required Listener interfaces and handle the call-backs there. <br />Collapse inheritance hierarchy, even it means duplicating code<br />Shorten all name qualifiers (package, class, method, variable names)<br />Minimize the number of array<br />Reduce try-catch blocks to bare minimum<br />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.<br />Scope of variables/classes/methods can impact performance<br />Eliminate unnecessary type casts<br />
  5. 5. Tips & tricks-II<br />Compound operators are faster, because fewer byte codes are generated<br />Define methods that accept reusable objects to be filled in with data, rather than methods that return objects holding that data<br />Inline methods where ever it is possible.<br />Use private and static methods and final classes to encourage in lining by the compiler. <br /> keep synchronized method out of loop, it increases the overhead<br />Avoid synchronization where ever its possible<br />Minimize using the methods use native call<br />Be specific for throwing and handling exceptions<br />
  6. 6. Creating objects in loop<br />Imposes double penalty in terms of performance<br />Each iteration in loop pushes the runtime system closer to garbage collector<br />Restructure code instead creating new processor each time<br />
  7. 7. Good practice<br />Bad practice<br />Object[] inputs = new Object[0];<br />int[] results = new int[0];<br />int length = inputs.length;<br />Processor p = new Processor();<br />for (int i = 0; i < length; i++) {<br />p.setInput(inputs[i]);<br />results[i] = p.calculateResult();<br />}<br />Object[] inputs = new Object[0];<br />int[] results = new int[0];<br />int length = inputs.length;<br />for (int i = 0; i < length; i++) {<br />Processor p = new Processor(inputs[i]);<br />results[i] = p.calculateResult();<br />}<br />
  8. 8. String & Stringbuffer<br />String is only the data type which can overload + operator in java<br />Each time you connate a string it inters creates a string buffer and string buffer creates performs over byte array<br />String buffer in efficient then string, but not always<br />
  9. 9. Performance Tuning<br />
  10. 10. BE clean<br />Release resource<br />Free the used internal data structures<br />Set array references to null after usage<br />Close network connections in finally block<br />
  11. 11. Optimizing User interface<br />Need to be simple, fast, informative and responsive<br />Progress indicator while network / other processing<br />Usage of low resolution images maintaining the minimal quality<br />Use double buffering technique for images where ever it applies <br />Use client caching<br />Use paint or partial paint the screen where ever necessary<br />
  12. 12. Optimizing Application deployment<br />Usage of obfuscator packages<br />Usage of low resolution images maintaining the minimal quality<br />Include only the classes you need<br />Use public variables in your classes, rather than using assessors. It is practically bad practice but reduces app size<br />Use protected methods<br />
  13. 13. Use of obfuscator <br />Reduces .jar size<br />Byte code obfuscator makes the code difficult to decompile<br />Side effect- descriptive method and variable names gets converted to small machine generated names<br />Obfuscators are <br />Retro guard<br />Pro guard<br />IBM JAX<br />
  14. 14. Benchmarking<br />J2SE has many tools for benchmarking<br />In ME we use “java.lang.runtime” class methods for memory test<br />freeMemory()<br />totalMemory()<br />
  15. 15. Knowing the runtime free memory<br />Runtime runtime = Runtime.getRuntime();<br />long before, after;<br />System.gc();<br />before = runtime.freeMemory();<br />Object newObject = new String();<br />after = runtime.freeMemory();<br />long size = before - after;<br />
  16. 16. Knowing execution time for a method<br />Use System.currentTimeMiles() <br />long start, finish;<br />start = System.currentTimeMillis();<br />someMethod();<br />finish = System.currentTimeMillis();<br />long duration = finish - start;<br />
  17. 17. MIDP Performance monitor Tools<br />Collects the statistics of application <br />Tools used in J2ME wireless tool kit are:<br />Profiler<br />Memory monitor<br />Network monitor<br />
  18. 18. Midlet Profiler<br />Collects the statistics of application <br />To monitor the method call and time to execute a method<br />Includes MIDP reference implementation classes<br />
  19. 19. Call Graph<br />
  20. 20. Memory monitor<br />Object Graph<br />
  21. 21. Network Monitor<br />Request Response<br />
  22. 22. Conclusion<br />Only optimize code if you need to and where it counts<br />Always study your code and try to improve the algorithms before using low-level techniques<br />Use setClip() where possible to minimize the drawing area<br />Keep as much stuff as possible out of loops<br />Pre-calculate and cache like crazy<br />Use static final methods where possible and avoid the synchronized modifier<br />Pass as few parameters as possible into frequently-called methods<br />Try to compare to zero instead of any other number<br />Local variables are faster than instance variables<br />