Slide 1

Slide 1 text

Introduction Into Java Profiling

Slide 2

Slide 2 text

Two Reasons For Profiling

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Quiz 1 ArrayList arrayList = new ArrayList<>(); 2 arrayList.add("Coding Serbia");

Slide 6

Slide 6 text

Quiz /** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return true (as specified by {@link Collection#add}) */ public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }

Slide 7

Slide 7 text

Attach Source

Slide 8

Slide 8 text

Profiling

Slide 9

Slide 9 text

We Can O Speed up code O Learn how code works O Have fun

Slide 10

Slide 10 text

Becoming a Cheeta O Execute less code O Waste less memory O Spend less time waiting

Slide 11

Slide 11 text

Code O Code execution consumes CPU cycles O Common issues O Frequent invocation O Long algorithm O Unnecessary code

Slide 12

Slide 12 text

Java is clever O javac optimizes sourcecode O HotSpot optimizes bytecode

Slide 13

Slide 13 text

javac private static final boolean PRINT = false; public static void main(String[] args) { if (PRINT) { System.out.println("Hello Serbia"); } }

Slide 14

Slide 14 text

HotSpot boolean print = false; public void doStuff() { for (int i = 0; i < 10_000_000; i++) { if (print) { System.out.println("Hello Serbia"); } } }

Slide 15

Slide 15 text

Memory O Java has automatic memory management O Common issues are O Garbage Collection O Memory Leaks

Slide 16

Slide 16 text

Garbage Collection O Limit Waste Long count = 0L; for (int i = 0; i < 10_000_000; i++) { count += i; }

Slide 17

Slide 17 text

Garbage Collection O Limit Waste Long count = 0L; for (int i = 0; i < 10_000_000; i++) { count = new Long(count.longValue()+ i); }

Slide 18

Slide 18 text

Memory Leaks O Forgotten reference keeps objects alive Map cache = new HashMap<>(); Double key = Math.random(); String value = "Coding Serbia"; cache.put(key, value); key = null;

Slide 19

Slide 19 text

Measuring

Slide 20

Slide 20 text

Time Wall Clock Elapsed Real CPU User + System

Slide 21

Slide 21 text

Unknown Code

Slide 22

Slide 22 text

Code t main() foo() bar() baz()

Slide 23

Slide 23 text

Sampling

Slide 24

Slide 24 text

t main() foo() bar() baz()

Slide 25

Slide 25 text

t main() foo() bar() baz()

Slide 26

Slide 26 text

t main() foo() bar()

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

Instrumentation

Slide 29

Slide 29 text

t main() foo() bar() baz()

Slide 30

Slide 30 text

t main() foo() bar() baz()

Slide 31

Slide 31 text

Sample First Instrument Later

Slide 32

Slide 32 text

Write your own Code Profiler https://github.com/CodingFabian/SamplingVsInstrumentation http://blog.codecentric.de/en/2011/10/measure-java- performance-sampling-or-instrumentation/

Slide 33

Slide 33 text

Write your own Memory Leak Hunter https://github.com/CodingFabian/JavaCollectionAnalyzer https://blog.codecentric.de/en/2012/01/find-java-memory-leaks- at-runtime-act-5/

Slide 34

Slide 34 text

Profiling Demo

Slide 35

Slide 35 text

@CodingFabian https://github.com/CodingFabian https://speakerdeck.com/CodingFabian