Java Performance Tuning

2,051 views

Published on

Training Presentation at eXo Platform SEA

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,051
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
174
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Java Performance Tuning

  1. 1. Java Performance Tuning Minh Hoang TO Portal Team
  2. 2. Agenda <ul><li>Performance Measure </li></ul><ul><li>JRE Customization </li></ul><ul><li>Programming Tips </li></ul><ul><li>Reading Suggestions </li></ul>
  3. 3. Performance Measure
  4. 4. Performance Measure <ul><li>Performance Analytical Tools </li></ul><ul><li>Determine problem </li></ul><ul><li>Attack performance problem </li></ul>
  5. 5. Performance Analytical Tools <ul><li>JDK tools: </li></ul><ul><li>JVisualVM (since Java 6), JConsole </li></ul><ul><li>Commercial tools: </li></ul><ul><li>JProfiler </li></ul>
  6. 6. Determine problem <ul><li>Factor to improve? </li></ul><ul><li>Time or Memory </li></ul><ul><li>Quantitative analysis to encircle the root causes </li></ul><ul><li>Use tools to localize memory leak, method call overhead </li></ul><ul><li>Theoretical argument servers for first-step analysis, but tools provide most convincing information </li></ul>
  7. 7. Attack performance problem <ul><li>Improve execution environment </li></ul><ul><li>Examine programming aspect </li></ul>
  8. 8. JRE Customization
  9. 9. JRE Customization <ul><li>JVM Options </li></ul><ul><li>Customize Garbage Collection </li></ul><ul><li>Frequently used JVM options </li></ul>
  10. 10. JVM Options <ul><li>JAVA_HOME/java </li></ul><ul><li>JAVA_HOME/java -X </li></ul>
  11. 11. Customize Garbage Collection <ul><li>JAVA_HOME/java -X </li></ul><ul><li>-Xnoclassgc </li></ul><ul><li>-Xincgc </li></ul>
  12. 12. Frequently used JVM options <ul><li>Heap size options </li></ul><ul><li>-Xms -Xmx </li></ul><ul><li>Permanent Generation options </li></ul><ul><li>-XX:MaxPermSize </li></ul><ul><li>Class sharing </li></ul><ul><li>-Xshare </li></ul>
  13. 13. Programming Tips
  14. 14. Programming Tips <ul><li>Object Creation </li></ul><ul><li>String </li></ul><ul><li>Collection Framework </li></ul><ul><li>Java IO and NIO (for next time) </li></ul><ul><li>Concurrency </li></ul>
  15. 15. Object Creation <ul><li>Object scope </li></ul><ul><li>final modifier </li></ul><ul><li>Interaction with Garbage Collector </li></ul>
  16. 16. Object Scope String wideScope; for(int i =0; i <10000000; i++) { wideScope = “” + i; } vs for(int i = 0; i < 10000000; i++) { String narrowScope = “” + i; }
  17. 17. <ul><li>Heap space is divided into Young Generation and Old Generation spaces </li></ul><ul><li>JAVA_HOME/jconsole </li></ul><ul><li>Garbage Collector has best performance on Young Generation </li></ul><ul><li>Object with narrow scope has more chance to stay in Young Generation </li></ul>
  18. 18. Final modifier <ul><li>Protect variable against reference reassignment (non-reflect code) </li></ul><ul><li>final SafeObject safeObject = new SafeObject(); </li></ul><ul><li>safeObject = null; // compilation error </li></ul><ul><li>Modularize code with anonymous class </li></ul><ul><li>final String displayString = “Hello”; </li></ul><ul><li>Runnable runnable = new Runnable(){ </li></ul><ul><li>public void run() { System.out.println(displayString); } }; </li></ul>
  19. 19. Final modifier <ul><li>Affect of final at execution </li></ul><ul><li>Look at my IntelliJ IDEA 10 </li></ul>
  20. 20. Interaction with Garbage Collector <ul><li>GC (Garbage Collector) reclaims memory occupied by unreferenced object TestObject strongReference = new TestObject(); //variable strongReference //points to memory cells at 0xXXXX </li></ul><ul><li>strongReference = null; //no reference to cells 0xXXXX </li></ul><ul><li>System.gc(); //Run the GC </li></ul><ul><li>GC handles SoftReference , WeakReference and PhantomReference exceptionally </li></ul><ul><li>JavaDoc from package java.lang.ref </li></ul>
  21. 21. <ul><li>//Object is allocated at 0xXXXX TestObject strongReference = new TestObject(); </li></ul><ul><li>SoftReference<TestObject> softReference = new SoftReference<TestObject>(strongReference); </li></ul><ul><li>//Object stored at 0xXXX is reachable from softReference </li></ul><ul><li>strongReference = null; //No more strong reference to object </li></ul><ul><li>Object stored at 0xXXXX is softly reachable from softReference, but automatically cleaned by GC when there is lack of memory </li></ul>
  22. 22. <ul><li>Code from LazyList class of GateIn </li></ul><ul><li>private static class Batch </li></ul><ul><li>{ </li></ul><ul><li>private final Reference<Object[]> elements; </li></ul><ul><li>private Batch(Object[] elements) </li></ul><ul><li>{ </li></ul><ul><li>this.elements = new SoftReference<Object[]>(elements); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  23. 23. String <ul><li>String Thread-safe, expensive handling methods </li></ul><ul><li>StringBuffer </li></ul><ul><li>Thread-safe, much faster than String </li></ul><ul><li>StringBuilder </li></ul><ul><li>Not thread-safe, slightly faster than StringBuffer </li></ul>
  24. 24. <ul><li>String s = new String(); </li></ul><ul><li>for(int i = 0; i < 10000; i++){ s += “a”; } </li></ul><ul><li>VS </li></ul><ul><li>StringBuffer buffer = new StringBuffer(); </li></ul><ul><li>for(int i = 0; i < 10000; i++){ buffer.append(“a”); } </li></ul><ul><li>VS </li></ul><ul><li>StringBuilder builder = new StringBuilder(); </li></ul><ul><li>for(int i = 0; i < 10000; i++){ builder.append(“a”); } </li></ul>
  25. 25. Collection Framework <ul><li>Random Access vs Sequential </li></ul><ul><li>Initial allocation </li></ul><ul><li>HashMap vs TreeMap </li></ul><ul><li>WeakHashMap </li></ul>
  26. 26. Random Access vs Sequential <ul><li>ArrayList<E> </li></ul><ul><li>access element: O(1) add element at the end: O(1) or O(n) sort element: O(n.logn) (sort with Collections.sort()) </li></ul><ul><li>LinkedList<E> access element: O(n) </li></ul><ul><li>add element at both ends: O(1) </li></ul><ul><li>sort element: O(n.logn) + O(n) (sort with Collections.sort()) </li></ul>
  27. 27. Initial Allocation <ul><li>Code snippet recurring in eXo products </li></ul><ul><li>List<String> list = new ArrayList<String>(3); </li></ul><ul><li>Why hard-coded size? </li></ul><ul><li>Is it better than </li></ul><ul><li>List<String> list = new LinkedList<String>(); </li></ul>
  28. 28. HashMap vs TreeMap <ul><li>HashMap<K,V> </li></ul><ul><li>fetch <K,V> entry: O(1) </li></ul><ul><li>put <K,V> entry: O(n) in worst case sort <K,V> entries based on key: Not supported </li></ul><ul><li>TreeMap<K,V> </li></ul><ul><li>fetch <K,V> entry: O(1) </li></ul><ul><li>Put <K,V> entry: O(n) in worst case </li></ul><ul><li>support sorting entries based on key </li></ul>
  29. 29. WeakHashMap private static class Entry<K,V> extends WeakReference<K> implements Map.Entry<K,V> { private V value; private final int hash; private Entry<K,V> next; … . }
  30. 30. Java IO and NIO <ul><li>In many cases: gainOf( improve_on_programming_logic ) </li></ul><ul><li><<<< </li></ul><ul><li>gainOf( improve_on_input_output ) </li></ul><ul><li>Java coder should have knowledge on Java NIO </li></ul>
  31. 31. Concurrency <ul><li>Concurrency concepts </li></ul><ul><li>Intrinsic lock </li></ul><ul><li>volatile keyword </li></ul><ul><li>Lock & Lock-free </li></ul>
  32. 32. Concurrency concepts <ul><li>Atomicity </li></ul><ul><li>Code executing on a thread is not inteferenced with other threads Ex: Code incrementing a counter </li></ul><ul><li>Visibility </li></ul><ul><li>Change on shared object is visible to multiple threads Ex: Flag variable </li></ul>
  33. 33. <ul><li>Atomicity yields Visibility </li></ul><ul><li>Atomicity problems are more complex than Visibility ones </li></ul><ul><li>Figure out your concurrency problem ( Atomicity or Visibility ) before typing synchronized </li></ul>
  34. 34. Intrinsic lock - synchronized <ul><li>Ensure Atomicity </li></ul><ul><li>Overhead of synchronized </li></ul><ul><li>Double-checked pattern </li></ul><ul><li>'intrinsic lock extension' in java.util.concurrent.locks </li></ul>
  35. 35. <ul><li>private int counter; </li></ul><ul><li>private void increaseCounter(){ counter++;} private synchronized void increaseCounterUsingIntrinsicLock(){ counter++;} public static void main(String[] args) { int n = 100000; //Print time to call increaseCounter n times //Print time to call increaseCounterUsingIntrinsicLock n times } </li></ul>
  36. 36. Double-checked pattern <ul><li>synchronized( locked_object ) { if( flag_variable ){ //do something } } VS if( flag_variable ){ synchronized( locked_object ) </li></ul><ul><li>{ if( flag_variable ){ //do something } } } </li></ul>
  37. 37. <ul><li>Double-checked reduces the scope of synchronized block </li></ul><ul><li>Useful in concurrent initialization problem </li></ul><ul><li>Requires Visibility on flag_variable </li></ul>
  38. 38. <ul><li>Code from RootContainer class in eXo Kernel </li></ul><ul><li>public static RootContainer getInstance() </li></ul><ul><li>{ </li></ul><ul><li>RootContainer result = singleton_; </li></ul><ul><li>if (result == null) </li></ul><ul><li>{ </li></ul><ul><li>synchronized (RootContainer.class) {….} } } </li></ul>
  39. 39. Code from Component class in WebUI Framework public Event getUIComponentEventConfig(String eventName) throws Exception { if(eventMap == null) { synchronized(this) { if(eventMap == null) {…} } } }
  40. 40. Volatile keyword <ul><li>Ensure Visibility </li></ul><ul><li>Work only with atomic operations </li></ul><ul><li>Usage in double-checked pattern </li></ul><ul><li>'volatile extension' in java.util.concurrent.atomic </li></ul>
  41. 41. private volatile long counter; private void nonAtomicOperation(){ counter++;} private void atomicOperation(){ counter = 281283;}
  42. 42. Usage in double-checked pattern <ul><li>Java Memory Model permutes execution order in multi-thread </li></ul><ul><li>world (JVM Specification) </li></ul><ul><li>Visibility on flag_variable is mandatory to double-checked pattern </li></ul><ul><li>volatile boolean flag_variable; //Assumed the type is boolean //Double-checked block </li></ul>
  43. 43. Lock & Lock-free <ul><li>Lock Package java.util.concurrent.locks </li></ul><ul><li>Lock-free </li></ul><ul><li>Package java.util.concurrent.atomic </li></ul>
  44. 44. Reading Suggestions
  45. 45. <ul><li>Java Concurrency in Practice – Brian Goetz </li></ul><ul><li>Java IO </li></ul><ul><li>Java NIO </li></ul>
  46. 46. Thank you!

×