© 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

43
© 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio

Transcript of © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

Page 1: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 1

Java Performance -War Stories

Paul McLachlanApptio

Page 2: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 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?

Page 3: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 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

Page 4: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 4

Page 5: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 5

“Perceived Performance”

Page 6: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 6

Example

Performance Tuning By Example

Page 7: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 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)

Page 8: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 8

Add a cache

Page 9: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 9

Don’t synchronize on the DB

Page 10: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 10

Use ConcurrentHashMap

Page 11: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 11

Don’t thrash with simultaneous hits

Page 12: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 12

Don’t run out of RAM

Page 13: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 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

Page 14: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 14

Preload everything beforehand

Page 15: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 15

Use less RAM with .intern()

Page 16: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 16

How to make HashMap.get() faster?

Page 17: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 17

HotCache pattern

Page 18: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 18

Example

Memory Optimizations

Page 19: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 19

How Much Memory?

Page 20: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 20

How Much Memory?

Page 21: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 21

How Much Memory?

org.apache.commons.collections.map.Flat3Map

Page 22: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 22

*How* Much Memory?

-XX:+UseCompressedOops

Page 23: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 23

Off Heap

Off Heap &Native Integration Tricks

Page 25: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 25

Off Heap / Mapped Wrapping Classes

Page 26: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 26

Example

Build In Instrumentation

Page 27: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 27

Visibility

Page 28: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 28

Memory Report

Page 29: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 29

Visibility

Page 30: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 30

Visibility

Page 31: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 31

/OutputThreads Servlet

Page 32: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 32

Example

Or … Tools

Page 33: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 33

Tools / YourKit

yourkit.com:

Sampling Instrumenting Heap & GC

$500 / unlimited

Page 34: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 34

How Profilers Work

Page 35: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 35

Or … Embed the Profiler

Page 36: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 36

Tools / NMon

http://nmon.sourceforge.net

Page 37: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 37

Example

Reference Material

Page 38: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 38

Numbers Everyone Should Know

http://www.cs.cornell.edu/projects/ladis2009/talks/dean-keynote-ladis2009.pdf

Page 39: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 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

Page 40: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 40

Tools

jHiccuphttp://www.azulsystems.com/jHiccup

YourKithttp://yourkit.com/

Nmon for Linuxhttp://nmon.sourceforge.net

Page 42: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 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/

Page 43: © 2011 Apptio, Inc. All Rights Reserved. 1 Java Performance - War Stories Paul McLachlan Apptio.

© 2011 Apptio, Inc. All Rights Reserved. 43

JVM Intrinsics

http://hg.openjdk.java.net/jdk6/jdk6/hotspot/file/6d13fcb3663f/ src/share/vm/classfile/vmSymbols.hpp