JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT...

23
JVM PR DIAGNO DIAGNO Danijel Mita Aleksander Rad ROBLEM OSTICS OSTICS ar, King ICT dovan, King ICT

Transcript of JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT...

Page 1: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

JVM PROBLEM DIAGNOSTICSDIAGNOSTICS

Danijel Mitar, King ICT

Aleksander Radovan, King ICT

JVM PROBLEM DIAGNOSTICSDIAGNOSTICS

Danijel Mitar, King ICT

Aleksander Radovan, King ICT

Page 2: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Contents

■ Top performance problems

– Database

– Memory

■ JVM monitoring

■ Demo: GC algorithms & heap dump analysis

■ JVM tuning flags

■ Performance testing tools

Demo: GC algorithms & heap dump analysis

Page 3: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Top performance problems

■ Database

– N + 1 problem

– Caching

– Connection pools

■ Memory

– STW (Stop-The-World) garbage collections

– Memory leaks

Top performance problems

garbage collections

Page 4: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

N + 1 problem

■ Symptoms:

– Increased load on database, slower response times

■ Troubleshooting:■ Troubleshooting:

– Counters for the number of database calls and number of executed transactions

– Correlation between those numbers

■ Avoiding problem:

– Eager vs lazy?

– SQL JOIN (HQL fetch join)

Increased load on database, slower response times

Counters for the number of database calls and number of executed

Correlation between those numbers

Page 5: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Caching

■ Symptoms:

– Increased CPU overhead and disk I/O rate

■ Troubleshooting:■ Troubleshooting:

– Memory monitoring

– Hit ratio vs miss ratio

■ Avoiding problem:

– Thorough planning

– Proper cache configuration

– Eventual consistency

Increased CPU overhead and disk I/O rate

Page 6: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Connection pools

■ Symptoms:

– Increased response times

– Low/high resource utilization

■ Troubleshooting:

– Waiting for getConnection() call (underutilized)

– Waiting for execute() call (over-utilized)

■ Avoiding problem:

– Tune SQL queries

– Estimate relative balance between various queries

– Load test against database and tune for optimal performance

– Load test application

() call (underutilized)

utilized)

Estimate relative balance between various queries

t database and tune for optimal performance

Page 7: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Stop-The-World garbage

■ Major garbage collection

■ Freeze all threads � mark-sweep collection

– Very effective– Very effective

– Very slow (10 x minor collection)

■ CPU & heap memory spikes

■ Abnormal response times

World garbage collections

sweep collection � compaction

Page 8: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Eden space

Tenure space

Young

generation

TenuredTenure space

Loaded classes (classloader)

Tenured

generation

Permanent

generation

Tenure space

S0 S1

Survivor spaces

Minor GC

Full GCsTenure space

Loaded classes (classloader)

Full GCs

Page 9: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –
Page 10: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Die young or live forever.

Turtle theory, Young

Die young or live forever.

Young generation theory of garbage collection

Page 11: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

GC Algorithms – Serial, Serial, Parallel & CMS

Page 12: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

GC Algorithms – Garbage FirstGarbage First

Page 13: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –
Page 14: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Memory leaks

■ Symptoms:

– Increased memory usage leading to

– Very difficult to differentiate between

■ Troubleshooting:

– Unbounded growth: Collections classes

– Heap dump analysis

■ Avoiding problem:

– Good session management

– Carefull usage of Collections classes

– Memory profiler

to out of memory

between simple out of memory and memory leak

classes

classes

Page 15: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

JVM monitoring

■ jcmd & jmap

– Command line tools

– Very useful for quick analysis

■ JVisualVM

– GUI tool for monitoring heap, CPU and threads

– Visual GC plugin

■ Heap dump analysis

– jhat

– MAT - Memory Analyzer Tool (Eclipse)

– VisualVM launcher (IntelliJ IDEA)

GUI tool for monitoring heap, CPU and threads

Memory Analyzer Tool (Eclipse)

Page 16: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –
Page 17: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

JVM tuning flagsFlagFlagFlagFlag WhatWhatWhatWhat itititit doesdoesdoesdoes

-server Chooses the server

compiler.

-client Chooses the client

compiler.

-XX:+TieredCompilation Uses tiered compilation (both-XX:+TieredCompilation Uses tiered compilation (both

client and server).

WhenWhenWhenWhen to use to use to use to use itititit

For long-running applications

that need fast performance.

For applications where startup

is the most important factor.

compilation (both For applications where you compilation (both For applications where you

want the best possible

performance and have enough

available native memory for

the extra compiled code.

Page 18: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

JVM tuning flagsFlagFlagFlagFlag WhatWhatWhatWhat itititit doesdoesdoesdoes

-XX:+UseSerialGC Uses a simple, single

GC algorithm.

-XX:+UseParallelOldGC Uses multiple threads to

collect the old generation while

application threads are

stopped.stopped.

-XX:+UseParallelGC Uses multiple threads to

collect the young generation

while application threads are

stopped.

WhenWhenWhenWhen to use to use to use to use itititit

Uses a simple, single-threaded For small (100 MB) heaps.

Uses multiple threads to

the old generation while

application threads are

When your application can

tolerate occasional long

pauses and you want to

maximize throughput while maximize throughput while

minimizing CPU usage.

Uses multiple threads to

the young generation

application threads are

Use in conjunction with

UseParallelOldGC.

Page 19: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

JVM tuning flagsFlagFlagFlagFlag WhatWhatWhatWhat itititit doesdoesdoesdoes

-XX:+UseConcMarkSweepGC Uses background thread(s) to

remove garbage from the old

generation with minimal

pauses.

-XX:+UseParNewGC Uses multiple threads to

the young generation whilethe young generation while

application threads are stopped.

-XX:+UseG1GC Uses multiple threads to

the young generation while

application threads are stopped,

and background thread(s) to

remove garbage from the old

generation with minimal

In development for Java 9:

doesdoesdoesdoes WhenWhenWhenWhen to use to use to use to use itititit

Uses background thread(s) to

remove garbage from the old

generation with minimal

When you have available CPU f

the background thread, you do

want long GC pauses, and you

have a relatively small heap.

Uses multiple threads to collect

the young generation while

Use in conjunction with Use

ConcMarkSweepGC.the young generation while

application threads are stopped.

ConcMarkSweepGC.

Uses multiple threads to collect

the young generation while

application threads are stopped,

and background thread(s) to

remove garbage from the old

generation with minimal pauses.

When you have available CPU f

the background thread, you do

want long GC pauses, and you

not have a small heap.

development for Java 9: Shenandoah GC

Page 20: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Performance testing

■ JMeter

■ Gatling

■ The Grinder■ The Grinder

■ Faban

testing tools

Page 21: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

Donald Knuth

Page 22: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

QUESTIONS?QUESTIONS?QUESTIONS?QUESTIONS?QUESTIONS?QUESTIONS?QUESTIONS?QUESTIONS?

Page 23: JVM PROBLEM DIAGNOSTICS - 2018.javacro.hrJVM... · JVM PROBLEM DIAGNOSTICS Danijel Mitar, King ICT Aleksander Radovan, King ICT. Contents Top performance problems – Database –

Resources

Oaks, S. (2014). Java Performance: The Definitive

Oransa, O. (2014). Java EE 7 Performance Tuning and

http://help.eclipse.org/mars/index.jsp?topic=/org.eclipse.mat.ui.help/welcome.html

http://www.vogella.com/tutorials/EclipseMemoryAnalyzer/article.html

http://infoq.com/articles/Diagnosing-Common-Java

https://www.pluralsight.com/courses/understanding

http://www.zabbix.com/documentation.php

http://blog.king-ict.hr/dmitar

Definitive Guide. Sebastopol, Kalifornija: O’Reilly Media, Inc.

Java EE 7 Performance Tuning and Optimization, Birmingham: Packt Publishing Ltd.

org.eclipse.mat.ui.help/welcome.html

www.vogella.com/tutorials/EclipseMemoryAnalyzer/article.html

Java-Database-Performance-Hotspots

www.pluralsight.com/courses/understanding-java-vm-memory-management