© 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio
Mar 22, 2016
© 2011 Apptio, Inc. All Rights Reserved. 1
Java Performance -War Stories
Paul McLachlanApptio
© 2011 Apptio, Inc. All Rights Reserved. 2
Performance Strategy
Performance tuning is a discipline
– Measure, Don’t (ever) guess– Always attack #1 bottleneck first– Is success possible?
© 2011 Apptio, Inc. All Rights Reserved. 3
Performance Strategies
– Call it less– Make it more efficient– Do it in parallel
– Do it beforehand
or…
Performance Strategy
© 2011 Apptio, Inc. All Rights Reserved. 4
© 2011 Apptio, Inc. All Rights Reserved. 5
“Perceived Performance”
© 2011 Apptio, Inc. All Rights Reserved. 6
Example
Performance Tuning By Example
© 2011 Apptio, Inc. All Rights Reserved. 7
A random bottleneck
This is an example – please ignore the SQL stupidity
(Actually – all of these snippets are for explanation purposes, YAMMV)
© 2011 Apptio, Inc. All Rights Reserved. 8
Add a cache
© 2011 Apptio, Inc. All Rights Reserved. 9
Don’t synchronize on the DB
© 2011 Apptio, Inc. All Rights Reserved. 10
Use ConcurrentHashMap
© 2011 Apptio, Inc. All Rights Reserved. 11
Don’t thrash with simultaneous hits
© 2011 Apptio, Inc. All Rights Reserved. 12
Don’t run out of RAM
© 2011 Apptio, Inc. All Rights Reserved. 13
Soft References
Weak/Soft/Phantom/Final References
http://jeremymanson.blogspot.com/2009/07/how-hotspot-decides-to-clear_07.html
-XX:SoftRefLRUPolicyMSPerMB=1
© 2011 Apptio, Inc. All Rights Reserved. 14
Preload everything beforehand
© 2011 Apptio, Inc. All Rights Reserved. 15
Use less RAM with .intern()
© 2011 Apptio, Inc. All Rights Reserved. 16
How to make HashMap.get() faster?
© 2011 Apptio, Inc. All Rights Reserved. 17
HotCache pattern
© 2011 Apptio, Inc. All Rights Reserved. 18
Example
Memory Optimizations
© 2011 Apptio, Inc. All Rights Reserved. 19
How Much Memory?
© 2011 Apptio, Inc. All Rights Reserved. 20
How Much Memory?
© 2011 Apptio, Inc. All Rights Reserved. 21
How Much Memory?org.apache.commons.collections.map.Flat3Map
© 2011 Apptio, Inc. All Rights Reserved. 22
*How* Much Memory?
-XX:+UseCompressedOops
© 2011 Apptio, Inc. All Rights Reserved. 23
Off Heap
Off Heap &Native Integration Tricks
© 2011 Apptio, Inc. All Rights Reserved. 24
“Unsafe”
http://javasourcecode.org/html/open-source/jdk/jdk-6u23/sun/misc/Unsafe.java.html
© 2011 Apptio, Inc. All Rights Reserved. 25
Off Heap / Mapped Wrapping Classes
© 2011 Apptio, Inc. All Rights Reserved. 26
Example
Build In Instrumentation
© 2011 Apptio, Inc. All Rights Reserved. 27
Visibility
© 2011 Apptio, Inc. All Rights Reserved. 28
Memory Report
© 2011 Apptio, Inc. All Rights Reserved. 29
Visibility
© 2011 Apptio, Inc. All Rights Reserved. 30
Visibility
© 2011 Apptio, Inc. All Rights Reserved. 31
/OutputThreads Servlet
© 2011 Apptio, Inc. All Rights Reserved. 32
Example
Or … Tools
© 2011 Apptio, Inc. All Rights Reserved. 33
Tools / YourKit
yourkit.com:
Sampling Instrumenting Heap & GC
$500 / unlimited
© 2011 Apptio, Inc. All Rights Reserved. 34
How Profilers Work
© 2011 Apptio, Inc. All Rights Reserved. 35
Or … Embed the Profiler
© 2011 Apptio, Inc. All Rights Reserved. 36
Tools / NMon http://nmon.sourceforge.net
© 2011 Apptio, Inc. All Rights Reserved. 37
Example
Reference Material
© 2011 Apptio, Inc. All Rights Reserved. 38
Numbers Everyone Should Knowhttp://www.cs.cornell.edu/projects/ladis2009/talks/dean-keynote-ladis2009.pdf
© 2011 Apptio, Inc. All Rights Reserved. 39
Statistics Relative Standard Deviation =
STDEV(…)/AVERAGE(…) Confidence = 1-TTEST(orig,control,1,2)
Throughput Average latency Max latency 90th Percentile latency
© 2011 Apptio, Inc. All Rights Reserved. 40
Tools
jHiccuphttp://www.azulsystems.com/jHiccup
YourKithttp://yourkit.com/
Nmon for Linuxhttp://nmon.sourceforge.net
© 2011 Apptio, Inc. All Rights Reserved. 41
Libraries
CLHM http://code.google.com/p/concurrentlinkedhashmap/
Guavahttp://code.google.com/p/guava-libraries/
Colthttp://acs.lbl.gov/software/colt/
Trove4Jhttp://trove.starlight-systems.com/
JDK 8 Concurrency Codehttp://gee.cs.oswego.edu/dl/concurrency-interest/index.html
© 2011 Apptio, Inc. All Rights Reserved. 42
Good Books / References
Java Memory Modelhttp://www.cs.umd.edu/~pugh/java/memoryModel/
Java Platform Performancehttp://java.sun.com/docs/books/performance/
Debugging by Thinkinghttp://www.debuggingbythinking.com/
Concurrent Programming in Javahttp://www.amazon.com/Concurrent-Programming-Java-Principles-Pattern/dp/0201310090/
© 2011 Apptio, Inc. All Rights Reserved. 43
JVM Intrinsicshttp://hg.openjdk.java.net/jdk6/jdk6/hotspot/file/6d13fcb3663f/ src/share/vm/classfile/vmSymbols.hpp