JVM: need for speed

Andrey Adamovich, Aestas/IT

About me

Andrey Adamovich

Contact details

What's in this presentation?

The tools

-

The game

-

Rules

  • Picture is shown
  • You guess what the next topic is
  • First one to guess gets a sticker!

Let's try!

-

Java's (Hash)Map

-

-

BigData

-

Let the games begin!

-

GC Pauses

-

What are the good parameters?

So many options....

So many options....

> java -XX:+PrintFlagsFinal -version | wc -l
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
714

So many options....

> java -XX:+PrintFlagsFinal -version | wc -l
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
668

So many options....

> java -XX:+PrintFlagsFinal -version | wc -l
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
670

So many options....

> java -XX:+PrintFlagsFinal -version | grep GC | wc -l
65

Use cases

  • Web application
  • Streaming application
  • Background job

Memory tuning?

-Xms<heap size>[g|m|k] 
-Xmx<heap size>[g|m|k]
-XX:PermSize=<perm gen size>[g|m|k] 
-XX:MaxPermSize=<perm gen size>[g|m|k]
-Xmn<young size>[g|m|k]
-XX:SurvivorRatio=<ratio>

Leverage multi-core

-XX:+UseConcMarkSweepGC 
-XX:+UseCMSInitiatingOccupancyOnly 
-XX:+ScavengeBeforeFullGC 
-XX:+CMSScavengeBeforeRemark
-XX:+CMSConcurrentMTEnabled
-XX:CMSInitiatingOccupancyFraction=70

GC logging

-XX:+PrintGCDetails 
-XX:+PrintGCDateStamps 
-Xloggc:<file-path> 
-XX:+PrintGCApplicationConcurrentTime 
-XX:+PrintSafepointStatistics 
-XX:+PrintHeapAtGC

Log analysis

GC

Open source: GCViewer

GC

Comercial: Censum

GC

Next riddle

-

Memory dumps

-

How to take?

On out of memory:

-XX:+HeapDumpOnOutOfMemoryError 
-XX:HeapDumpPath=<path to dump>`date`.hprof

Or manually:

jmap -dump:file=<path to dump> <java process id>

jhat

-

Eclipse MAT

oql

OQL

oql

Useful characteristics

  • Object Retained Size
  • Object Shallow Size
  • Stale Objects (not connected to GC roots)

Flight Recording

fr

Comercial

  • YourKit
  • jProfiler

Next riddle

-

Thread dumps

-

Situations

  • Dead locks
  • 100% CPU load
  • Hanging I/O

jstack

jstack <java process pid>

JMX

  • Use MBean with name java.lang:type=Threading
  • Call getAllThreadIds
  • Loop through all retrieved IDs and call getThreadInfo

The cheapest profiler

for ((a=1; a <= 60; a++))
do 
  jstack 7789 > thread-dump.$a
  sleep 1
  echo $a
done

Thread dump analysis

Thread dump structure

TDA

Thread states I

A thread can be in one of the following states:

  • NEW A thread that has not yet started is in this state.
  • RUNNABLE A thread executing in the Java virtual machine is in this state.
  • BLOCKED A thread that is blocked waiting for a monitor lock is in this state.

Thread states II

  • WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
  • TIMED_WAITING A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
  • TERMINATED A thread that has exited is in this state.

TDA

TDA

Samurai

TDA

IBM TMDA

TDA

Use custom scripts

Next riddle

-

Debugging

-

We need it when we have...

  • Non-reproducable issues
  • Hard-to-understand code

Enable debugging

-agentlib:jdwp=transport=dt_socket,server=y,address=1337

Use your IDE (Eclipse)

eclipse

Use your IDE (Intellij IDEA)

idea

jdb

jdb

jdb

YouDebug

YouDebug

YouDebug is a non-interactive debugger scripted by Groovy to assist remote troubleshooting and data collection to analyze failures.

YouDebug

YouDebug (buggy class)

public class SubStringTest {
  public static void main(String[] args) {
    String s = someLengthComputationOfString();
    System.out.println(s.substring(5));
  }
  private static String someLengthComputationOfString() {
    ...;
  }
}

YouDebug (SubStringMonitor.ydb)

breakpoint("com.acme.SubStringTest",3) {
  println "s="+s;
}

YouDebug (run debugger)

> java -jar youdebug.jar -socket 1337 SubStringMonitor.ydb
s=test

Next riddle

-

Decompiling

-

Java Decompiler

JD

Java Decompiler

JD

Issues

  • Lack of support for latest JVM features
  • Lack of support for other JVM langauges
  • Obfuscation is always an issue

Next riddle

-

Profiling

-

Types of profiling

  • Memory allocation profiling
  • CPU profiling
  • Lock profiling

VisualVM

vm

Comercial

  • YourKit
  • jProfiler

Next riddle

-

Benchmarks

-

JMH

MH is a Java harness for building, running, and analysing nano/micro/milli/macro benchmarks written in Java and other languages targetting the JVM.

JMH: example

package org.openjdk.jmh.samples;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

JHM: example

public class JMHSample_01_HelloWorld {
  @Benchmark
  public void wellHelloThere() {
    // this method was intentionally left blank.
  }

JMH: example

  public static void main(String[] args) throws RunnerException {
    Options opt = new OptionsBuilder()
            .include(JMHSample_01_HelloWorld.class.getSimpleName())
            .forks(1)
            .build();
    new Runner(opt).run();
  }
}

JMH: example

> mvn clean install
> java -jar target/benchmarks.jar JMHSample_01_HelloWorld

JMH: annotations

@Benchmark @BenchmarkMode @CompilerControl 
@Fork @Group @GroupThreads
@Level @Measurement @Mode
@OperationsPerInvocation @OutputTimeUnit
@Param @Scope @Setup @State
@TearDown @Threads @Timeout @Warmup

JMH: output

jmh

Load testing tools

  • jMeter
  • gatling

Next riddle

-

Monitoring

-

JMX is your friend!

VisualVM (again)

vm

Mission Control

mc

Metrics collection

-

JMXTrans

This is effectively the missing connector between speaking to a JVM via JMX on one end and whatever logging / monitoring / graphing package that you can dream up on the other end.

jHiccup

jHiccup is a non-intrusive instrumentation tool that logs and records platform "hiccups" - including the JVM stalls that often happen when Java applications are executed and/or any OS or hardware platform noise that may cause the running application to not be continuously runnable.

jHiccup

> java -javaagent:jHiccup.jar MyProgram

jHiccup

jmh

Drop Wizard Metrics

Metrics provides a powerful toolkit of ways to measure the behavior of critical components in your production environment.

Netflix Hysterix

sm

Stagemonitor

sm

-

Single JVM?

-

Really?

-

-

Dead JVM

-

-

Many JVMs

-

Open-source tools

  • Graphite
  • Graphana
  • Logstash
  • Kibana
  • Graylog2
  • Fluentd

Comercial tools

  • NewRelic
  • Plumbr
  • AppDynamics
  • Dyntrace
  • Takipi
  • Hyperic

Final riddle

-

Links

-

Links: GC

Links: GC

Links: GC

Links: Thread dumps

Links: Tools

Links: Tools

Links: Tools

Links: Tools

Reading material

Java Performance

1

Java Performance: The Defenetive Guide

2

Java Performance And Scalability

3

Devternity

  • 1st of December, 2015, Riga
  • Software Craftsmanship Conference
  • 3 tracks, 30 speakers
  • http://devternity.com

RigaDevDay

This is it!

Questions?

Thank you!