Globalcode – Open4education
Inside the JVM - Performance &
Garbage Collector Tuning in JAVA
Rafael Monteiro e Pereira
Data Scientist – DataLabs Serasa Experian
Globalcode – Open4education
Agenda
Como a JVM funciona ?
Tools for performance analysis
GC analysis
Globalcode – Open4education
The JVM
Fonte: http://www.mauda.com.br/?p=872
Globalcode – Open4education
Java Mission Control
Available in JDK since Java 7 update 40
Two main tools: JMX Console & JFR
Globalcode – Open4education
Java Mission Control
The server VM needs to be started with following
options
-XX:+UnlockCommercialFeatures -XX:+FlightRecorder
Globalcode – Open4education
JMC->JMX Console
Captures and presents live data about:
Garbage Collection (GC) Pauses
Memory (Heap Usage)
CPU Usage
Other attributes exposes via Mbeans
Globalcode – Open4education
JMC->Flight Recorder
A profiling and event collection framework built into
the Oracle JDK
Gather low level information about the JVM and
application are behaving without performance
impact (less than 2%)
Globalcode – Open4education
JMC-> Demo
HotMethods
Latency
Memory Leak
Samples by Marcus Hint
http://hirt.se/blog/?p=611
Globalcode – Open4education
Thread Communication
Just Create a Queue
right ?
You are sure that you
wanna block ?
https://github.com/real-
logic/benchmarks
LinkedBlockingQueue 154
μs
ArrayBlockingQueue 56 μs
ConcurrentLinkedQueue 44 μs
Disruptor 3.0 20 μs
Globalcode – Open4education
Thread Communication
Disruptor (
https://github.com/LMAX-Exchange/disruptor
)
Very fast Inter-Thread
Message Library
Source: https://martinfowler.com/articles/images/lmax/arch-full.png
Globalcode – Open4education
Performance Test
Gattling.io
Globalcode – Open4education
Performance Test
val scn = {
scenario("resource test: host=" + hostUrl + ", users_count=" + usersCount)
.feed(usersDataSource)
.exec(http("getUserId")
.get("/gdc/app/account/bootstrap")
.basicAuth("${username}", "${password}")
.check(jsonPath("$.bootstrapResource.current.loginMD5")
.find.saveAs("userId")))
.repeat(requestsNumber) {
exec(http("processesView GET")
.get("/gdc/app/account/profile/${userId}/dataload/processesView")
.basicAuth("${username}", "${password}"))
.pause(requestPauseSeconds)
}}
Globalcode – Open4education
Performance Test
JMH
Building, running, and analysing
nano/micro/milli/macro benchmarks written in Java
and other languages targetting the JVM.
Globalcode – Open4education
Performance Test
JMH
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
public AddressBookProtos.Person GPBDeserialization() {
try {
return AddressBookProtos.Person.parseFrom(msgGPB);
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
return null;
}
Globalcode – Open4education
GC
Avoid “Stop the world” events
Usually a big problem if latency is a requirement.
G1
CMS
Shenandoah
Globalcode – Open4education
GC-> CMS
Globalcode – Open4education
GC - Tips
Paramters for CMS
-server
-Xms4048m e -Xmx4048m
-XX:+UseParallelGC
-XX:+ParallelOldGC
-XX:+UseConcMarkSweepGC
Globalcode – Open4education
GC->G1
Globalcode – Open4education
GC->G1
Fonte: http://presentations2015.s3.amazonaws.com/40_presentation.pdf
Globalcode – Open4education
GC->G1
Fonte: http://presentations2015.s3.amazonaws.com/40_presentation.pdf
Globalcode – Open4education
GC- TIPS
Parameters G1
-XX:+UseG1GC
-server
-Xms8g –Xmx8g
-XX:MaxGCPauseMillis=200
-XX:ParallelGCThreads=n
-XX:+ParallelRefProcEnabled
Globalcode – Open4education
GC-> Shenandoah
Ultra low latency GC
Fonte: https://wiki.openjdk.java.net/display/shenandoah/Main
Globalcode – Open4education
GC-> Shenandoah
Ultra low latency GC
-XX:+UseShenandoahGC
-XX:+UnlockExperimentalVMOptions
-XX:ShenandoahUncommitDelay=1000
-XX:ShenandoahGuaranteedGCInterval=10000
Fonte: https://wiki.openjdk.java.net/display/shenandoah/Main
Globalcode – Open4education
GC-> Shenandoah
Ultra low latency GC
Fonte: https://dzone.com/articles/choosing-the-right-gc
Globalcode – Open4education
GC-> Shenandoah
Ultra low latency GC->G1
Fonte: https://dzone.com/articles/choosing-the-right-gc
Globalcode – Open4education
GC - Tips
GC Logs
-XX:+PrintGCDetails
-XX:+PrintGCDateStamps
-XX:+PrintGCTimeStamps
Xloggc:gc.log"
Globalcode – Open4education
GC - Log
GC G1
[GC pause (young) (to-space exhausted), 0.0406140
secs] 6229.691:
[Full GC 10G->5813M(12G), 15.7221680 secs]
Globalcode – Open4education
Off-Heap Allocation
Allocates memory outside the Heap
HARD ! Really !
Globalcode – Open4education
Off-Heap Allocation
Copy Object outside the Heap
Globalcode – Open4education
GC - Tips
Use a LIB !
Open HFT -> https://github.com/OpenHFT
https://github.com/OpenHFT/Java-Thread-Affinity
https://github.com/OpenHFT/Chronicle-Queue
https://github.com/OpenHFT/Chronicle-Logger
Some Tips
Be careful with big Heap sizes. Usually is not a good idea to go beyond 8
GB on Hotspot.
Big Heap == Slow GC !!! Consider using off-heap allocation if a lot of
information must be maintained in memory.
Globalcode – Open4education
Referencias
Oracle
Marcus Hirt blog
JMH
OpenJDK->JMH
Globalcode – Open4education
OBRIGADO !
E-mail: rafael@monteiro.io
Twitter: @monteiro_io
Github: github.com/rafamonteiro
LinkedIn: https://bit.ly/2jQQIXj
Slides -> https://bit.ly/2LaFMEd
Inside the JVM - Performance & Garbage Collector Tuning in JAVA

Inside the JVM - Performance & Garbage Collector Tuning in JAVA