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

More Related Content

PDF
Java Performance Tuning
PPT
Java Performance Monitoring & Tuning
PDF
Towards JVM Dynamic Languages Toolchain
PDF
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
PDF
Java Performance & Profiling
PDF
JVM and Garbage Collection Tuning
PDF
JCConf 2020 - New Java Features Released in 2020
PPT
Jvm Performance Tunning
Java Performance Tuning
Java Performance Monitoring & Tuning
Towards JVM Dynamic Languages Toolchain
"What's New in HotSpot JVM 8" @ JPoint 2014, Moscow, Russia
Java Performance & Profiling
JVM and Garbage Collection Tuning
JCConf 2020 - New Java Features Released in 2020
Jvm Performance Tunning

What's hot (20)

PPTX
The Java Memory Model
PDF
JVM JIT-compiler overview @ JavaOne Moscow 2013
PDF
Why GC is eating all my CPU?
PDF
Intrinsic Methods in HotSpot VM
PDF
Using Flame Graphs
ODP
Java Garbage Collection, Monitoring, and Tuning
PPTX
HotSpot JVM Tuning
PPT
An Introduction to JVM Internals and Garbage Collection in Java
PPTX
Java Serialization Facts and Fallacies
PPTX
DIY Java Profiling
PDF
Wait for your fortune without Blocking!
KEY
JavaOne 2011 - JVM Bytecode for Dummies
PPT
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
KEY
Know yourengines velocity2011
PPTX
Lambdas puzzler - Peter Lawrey
PDF
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
PPT
iOS Multithreading
PDF
Software Profiling: Understanding Java Performance and how to profile in Java
PDF
Loom and concurrency latest
The Java Memory Model
JVM JIT-compiler overview @ JavaOne Moscow 2013
Why GC is eating all my CPU?
Intrinsic Methods in HotSpot VM
Using Flame Graphs
Java Garbage Collection, Monitoring, and Tuning
HotSpot JVM Tuning
An Introduction to JVM Internals and Garbage Collection in Java
Java Serialization Facts and Fallacies
DIY Java Profiling
Wait for your fortune without Blocking!
JavaOne 2011 - JVM Bytecode for Dummies
CS6270 Virtual Machines - Java Virtual Machine Architecture and APIs
Know yourengines velocity2011
Lambdas puzzler - Peter Lawrey
"JIT compiler overview" @ JEEConf 2013, Kiev, Ukraine
iOS Multithreading
Software Profiling: Understanding Java Performance and how to profile in Java
Loom and concurrency latest
Ad

Viewers also liked (20)

PPT
Jvm Performance Tunning
PPTX
Java性能调优浅谈
PDF
Hotspot Garbage Collection - The Useful Parts
PPTX
Java performance tuning
PPTX
Sun jdk 1.6内存管理 -使用篇
PPTX
Hands-on Performance Tuning Lab - Devoxx Poland
PPTX
Java profiling Do It Yourself
PDF
Diagnosing Your Application on the JVM
PDF
JavaOne 2014: Java Debugging
PDF
Profiler Guided Java Performance Tuning
PDF
Debugging Your Production JVM
PDF
Performance optimization techniques for Java code
PPTX
Memory leak
PPT
Efficient Memory and Thread Management in Highly Parallel Java Applications
PDF
Memory Leak In java
PPT
Real Life Java EE Performance Tuning
ODP
An Introduction To Java Profiling
PPTX
Benchmarking Solr Performance
PDF
Introduction of Java GC Tuning and Java Java Mission Control
PDF
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
Jvm Performance Tunning
Java性能调优浅谈
Hotspot Garbage Collection - The Useful Parts
Java performance tuning
Sun jdk 1.6内存管理 -使用篇
Hands-on Performance Tuning Lab - Devoxx Poland
Java profiling Do It Yourself
Diagnosing Your Application on the JVM
JavaOne 2014: Java Debugging
Profiler Guided Java Performance Tuning
Debugging Your Production JVM
Performance optimization techniques for Java code
Memory leak
Efficient Memory and Thread Management in Highly Parallel Java Applications
Memory Leak In java
Real Life Java EE Performance Tuning
An Introduction To Java Profiling
Benchmarking Solr Performance
Introduction of Java GC Tuning and Java Java Mission Control
High Performance Solr and JVM Tuning Strategies used for MapQuest’s Search Ah...
Ad

Similar to Java Performance Tuning (20)

PPT
Java programing considering performance
ODP
Best practices in Java
KEY
JavaOne 2012 - JVM JIT for Dummies
PPT
Hs java open_party
PPTX
Java concurrency
PDF
Java Concurrency Gotchas
KEY
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
PDF
Designing and coding Series 40 Java apps for high performance
PPT
Concurrency Antipatterns In IDEA
PPTX
Jvm memory model
PPT
Optimizing your java applications for multi core hardware
PDF
Java Concurrency Gotchas
PPT
Garbage collection in JVM
PDF
Concurrency Concepts in Java
PDF
Concurrency gotchas
PDF
JAVA Interview Questions (1)............pdf
PDF
JAVA Interview Questions (1)..........pdf
PPT
Performance tuning jvm
PDF
Javaoneconcurrencygotchas 090610192215 Phpapp02
PPTX
Concurrency Grabbag
Java programing considering performance
Best practices in Java
JavaOne 2012 - JVM JIT for Dummies
Hs java open_party
Java concurrency
Java Concurrency Gotchas
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Designing and coding Series 40 Java apps for high performance
Concurrency Antipatterns In IDEA
Jvm memory model
Optimizing your java applications for multi core hardware
Java Concurrency Gotchas
Garbage collection in JVM
Concurrency Concepts in Java
Concurrency gotchas
JAVA Interview Questions (1)............pdf
JAVA Interview Questions (1)..........pdf
Performance tuning jvm
Javaoneconcurrencygotchas 090610192215 Phpapp02
Concurrency Grabbag

More from Minh Hoang (7)

PDF
Yolo Family TechTalk
PDF
ElasticSearch Introduction
PDF
Modularize JavaScript with RequireJS
PPT
Zero redeployment with JRebel
PPT
Servlet 3.0
PPT
Regular Expression
PDF
Gatein Presentation
Yolo Family TechTalk
ElasticSearch Introduction
Modularize JavaScript with RequireJS
Zero redeployment with JRebel
Servlet 3.0
Regular Expression
Gatein Presentation

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Electronic commerce courselecture one. Pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Machine learning based COVID-19 study performance prediction
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Advanced IT Governance
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
cuic standard and advanced reporting.pdf
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PPT
Teaching material agriculture food technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Approach and Philosophy of On baking technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Electronic commerce courselecture one. Pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
The AUB Centre for AI in Media Proposal.docx
Per capita expenditure prediction using model stacking based on satellite ima...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Machine learning based COVID-19 study performance prediction
20250228 LYD VKU AI Blended-Learning.pptx
Advanced IT Governance
GamePlan Trading System Review: Professional Trader's Honest Take
cuic standard and advanced reporting.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Teaching material agriculture food technology
Reach Out and Touch Someone: Haptics and Empathic Computing
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Approach and Philosophy of On baking technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
Network Security Unit 5.pdf for BCA BBA.

Java Performance Tuning

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