Java GC, Off-heap workshop Moisieienko Valerii
2 Intro GC tuning Off-heap Discussion Agenda 1. Little bit theory: ● GC tuning principles ● GC algorithms: Serial, Parallel, CMS, G1 2. GC logs and tuning 3. Play with off-heap: ● LinkedList vs ArrayList ● ByteBuffer/Direct ByteBuffer/Unsafe ● MapDB
3 Intro GC tuning Off-heap Discussion What is the GC? Garbage Collection(GC) is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects
4 Intro GC tuning Off-heap Discussion 80% of GC problems are easy to fix Nikita Salnikov-Tarnovski
5 Intro GC tuning Off-heap Discussion GC tuning problem -XX:+PrintFlagsFinal 700+ JVM flags, 200+ of them are realted to GC
6 Intro GC tuning Off-heap Discussion Java 7 Memory Model Java Memory Heap PermGen Young Generation Thread 1...N Old/Tenured Generation Eden Space From Space (Survivor1) To Space (Survivor2)
7 Intro GC tuning Off-heap Discussion Java 8 Memory Model Java Memory Heap PermGen Young Generation Thread 1...N Old/Tenured Generation Eden Space From Space (Survivor1) To Space (Survivor2) VM Metaspace
8 Intro GC tuning Off-heap Discussion GC basis GC Roots Reachable Objects Non-reachable Objects -> Garbage
9 Intro GC tuning Off-heap Discussion GC overhead ● CPU overhead ● Affects app throughput ● Good throughput > 90%
10 Intro GC tuning Off-heap Discussion GC latency ● Duration of one GC pause ● Affect end-user expirience ● More important than throughput
11 Intro GC tuning Off-heap Discussion GC duration ● GC duration does not depend on heap size ● GC duration does not depend on total objects ● Depends on live objects
12 Intro GC tuning Off-heap Discussion GC tuning goals Overhead HeapLatency
13 Intro GC tuning Off-heap Discussion GC algorithms ● Serial GC ● Parallel GC ● Concurrent Mark-Sweep GC ● Garbage First GC ● C4 (Azul) ● Shenandoah (OpenJDK)
14 Intro GC tuning Off-heap Discussion Serial GC Аpp thread GC thread Pause
15 Intro GC tuning Off-heap Discussion Serial ● -XX:+UseSerialGC ● For 100MBs heaps and single core machines ● The smallest overhead
16 Intro GC tuning Off-heap Discussion Parallel GC Аpp thread GC thread Pause
17 Intro GC tuning Off-heap Discussion Parallel ● -XX:+UseParallelGC ● Good throughput ● Long pause can occur
18 Intro GC tuning Off-heap Discussion ConcurrentMarkSweep GC Аpp thread GC thread RemarkConc Mark, PrecleanInitial Sweep, Reset
19 Intro GC tuning Off-heap Discussion ConcurrentMarkSweep ● Parallel in Young, mostly Concurrent in Old ● Old is fragmenting ● The goal is to collect fast enough ● On fails, single threaded Full GC kicks in
20 Intro GC tuning Off-heap Discussion G1 O E OO O O E OO S O O OE E O O E S S S O EO E O OO O O E S Old Generation Survivor Space Eden Space
21 Intro GC tuning Off-heap Discussion G1 ● -XX:+UseG1GC ● Completely different layout ● Concurrent and parallel ● Low pause replacement for CMS ● Use in jdk8 only ● There are plans to make it default in jdk9
22 Intro GC tuning Off-heap Discussion GC log flags ● Main: -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:gc.log ● Optionaly: -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=<n> -XX:GCLogFileSize=<k>M
23 Intro GC tuning Off-heap Discussion How to start ● Define the goal ● Define the way of check (load test) ● Remove all JVM GC flags ● Add GC logs flags ● Run load test ● Check logs and compare to goals
24 Intro GC tuning Off-heap Discussion Let's tune it!
25 Intro GC tuning Off-heap Discussion Results Throughput Largest Pause Full GC pauses Parallel 1G 87% 0,05s 1023 Parallel 2G 97% 0,07s 2 CMS 1G 90% 0,08s 5 CMS 2G 93% 0,05s 0 G1 1G (jdk7) 83% 0,1s 0 G1 2G (jdk7) 97% 0,1s 0 G1 1G (jdk8) 89% 0,03s 0 G1 2G (jdk8) 99% 0,06s 0
26 Intro GC tuning Off-heap Discussion CMSInitiatingOccupancyFraction Throughput Largest Pause Default (92%) 91,8% 4,7s Updated(65%) 93,4% 0,17s -XX:CMSInitiatingOccupancyFraction=<n>
27 Intro GC tuning Off-heap Discussion Off-heap, pro et contra Pro Contra No Heap Complexity No GC Inflexibility No Pauses Hand memory managment No Overhead
28 Intro GC tuning Off-heap Discussion Ways to Off-heap ● ByteBuffer ● Direct ByteBuffer ● Unsafe
29 Intro GC tuning Off-heap Discussion Test app ● Weather sensors produce events ● 20 000 000 events per day (for example) ● We need to know min temp, max temp, avg temp, min pressure, max pressure, avg pressure ● We want to use data only from in order sensors
30 Intro GC tuning Off-heap Discussion WeatherMetricEvent private int sensorId; private double pressure; private double temperature; private boolean ok;
31 Intro GC tuning Off-heap Discussion Test plan ● LinkedList ● ArrayList ● ByteBuffer ● Direct ByteBuffer ● Unsafe ● MapDB
32 Intro GC tuning Off-heap Discussion WeatherMetricEvent in memory int double double bool private int sensorId; private double pressure; private double temperature; private boolean ok; Heap object size: 40 bytes Off-heap object size: 21 bytes
33 Intro GC tuning Off-heap Discussion Thank you Write me: valeramoiseenko@gmail.com

Java GC, Off-heap workshop

  • 1.
    Java GC, Off-heapworkshop Moisieienko Valerii
  • 2.
    2 Intro GC tuningOff-heap Discussion Agenda 1. Little bit theory: ● GC tuning principles ● GC algorithms: Serial, Parallel, CMS, G1 2. GC logs and tuning 3. Play with off-heap: ● LinkedList vs ArrayList ● ByteBuffer/Direct ByteBuffer/Unsafe ● MapDB
  • 3.
    3 Intro GC tuningOff-heap Discussion What is the GC? Garbage Collection(GC) is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects
  • 4.
    4 Intro GC tuningOff-heap Discussion 80% of GC problems are easy to fix Nikita Salnikov-Tarnovski
  • 5.
    5 Intro GC tuningOff-heap Discussion GC tuning problem -XX:+PrintFlagsFinal 700+ JVM flags, 200+ of them are realted to GC
  • 6.
    6 Intro GC tuningOff-heap Discussion Java 7 Memory Model Java Memory Heap PermGen Young Generation Thread 1...N Old/Tenured Generation Eden Space From Space (Survivor1) To Space (Survivor2)
  • 7.
    7 Intro GC tuningOff-heap Discussion Java 8 Memory Model Java Memory Heap PermGen Young Generation Thread 1...N Old/Tenured Generation Eden Space From Space (Survivor1) To Space (Survivor2) VM Metaspace
  • 8.
    8 Intro GC tuningOff-heap Discussion GC basis GC Roots Reachable Objects Non-reachable Objects -> Garbage
  • 9.
    9 Intro GC tuningOff-heap Discussion GC overhead ● CPU overhead ● Affects app throughput ● Good throughput > 90%
  • 10.
    10 Intro GC tuningOff-heap Discussion GC latency ● Duration of one GC pause ● Affect end-user expirience ● More important than throughput
  • 11.
    11 Intro GC tuningOff-heap Discussion GC duration ● GC duration does not depend on heap size ● GC duration does not depend on total objects ● Depends on live objects
  • 12.
    12 Intro GC tuningOff-heap Discussion GC tuning goals Overhead HeapLatency
  • 13.
    13 Intro GC tuningOff-heap Discussion GC algorithms ● Serial GC ● Parallel GC ● Concurrent Mark-Sweep GC ● Garbage First GC ● C4 (Azul) ● Shenandoah (OpenJDK)
  • 14.
    14 Intro GC tuningOff-heap Discussion Serial GC Аpp thread GC thread Pause
  • 15.
    15 Intro GC tuningOff-heap Discussion Serial ● -XX:+UseSerialGC ● For 100MBs heaps and single core machines ● The smallest overhead
  • 16.
    16 Intro GC tuningOff-heap Discussion Parallel GC Аpp thread GC thread Pause
  • 17.
    17 Intro GC tuningOff-heap Discussion Parallel ● -XX:+UseParallelGC ● Good throughput ● Long pause can occur
  • 18.
    18 Intro GC tuningOff-heap Discussion ConcurrentMarkSweep GC Аpp thread GC thread RemarkConc Mark, PrecleanInitial Sweep, Reset
  • 19.
    19 Intro GC tuningOff-heap Discussion ConcurrentMarkSweep ● Parallel in Young, mostly Concurrent in Old ● Old is fragmenting ● The goal is to collect fast enough ● On fails, single threaded Full GC kicks in
  • 20.
    20 Intro GC tuningOff-heap Discussion G1 O E OO O O E OO S O O OE E O O E S S S O EO E O OO O O E S Old Generation Survivor Space Eden Space
  • 21.
    21 Intro GC tuningOff-heap Discussion G1 ● -XX:+UseG1GC ● Completely different layout ● Concurrent and parallel ● Low pause replacement for CMS ● Use in jdk8 only ● There are plans to make it default in jdk9
  • 22.
    22 Intro GC tuningOff-heap Discussion GC log flags ● Main: -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:gc.log ● Optionaly: -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=<n> -XX:GCLogFileSize=<k>M
  • 23.
    23 Intro GC tuningOff-heap Discussion How to start ● Define the goal ● Define the way of check (load test) ● Remove all JVM GC flags ● Add GC logs flags ● Run load test ● Check logs and compare to goals
  • 24.
    24 Intro GC tuningOff-heap Discussion Let's tune it!
  • 25.
    25 Intro GC tuningOff-heap Discussion Results Throughput Largest Pause Full GC pauses Parallel 1G 87% 0,05s 1023 Parallel 2G 97% 0,07s 2 CMS 1G 90% 0,08s 5 CMS 2G 93% 0,05s 0 G1 1G (jdk7) 83% 0,1s 0 G1 2G (jdk7) 97% 0,1s 0 G1 1G (jdk8) 89% 0,03s 0 G1 2G (jdk8) 99% 0,06s 0
  • 26.
    26 Intro GC tuningOff-heap Discussion CMSInitiatingOccupancyFraction Throughput Largest Pause Default (92%) 91,8% 4,7s Updated(65%) 93,4% 0,17s -XX:CMSInitiatingOccupancyFraction=<n>
  • 27.
    27 Intro GC tuningOff-heap Discussion Off-heap, pro et contra Pro Contra No Heap Complexity No GC Inflexibility No Pauses Hand memory managment No Overhead
  • 28.
    28 Intro GC tuningOff-heap Discussion Ways to Off-heap ● ByteBuffer ● Direct ByteBuffer ● Unsafe
  • 29.
    29 Intro GC tuningOff-heap Discussion Test app ● Weather sensors produce events ● 20 000 000 events per day (for example) ● We need to know min temp, max temp, avg temp, min pressure, max pressure, avg pressure ● We want to use data only from in order sensors
  • 30.
    30 Intro GC tuningOff-heap Discussion WeatherMetricEvent private int sensorId; private double pressure; private double temperature; private boolean ok;
  • 31.
    31 Intro GC tuningOff-heap Discussion Test plan ● LinkedList ● ArrayList ● ByteBuffer ● Direct ByteBuffer ● Unsafe ● MapDB
  • 32.
    32 Intro GC tuningOff-heap Discussion WeatherMetricEvent in memory int double double bool private int sensorId; private double pressure; private double temperature; private boolean ok; Heap object size: 40 bytes Off-heap object size: 21 bytes
  • 33.
    33 Intro GC tuningOff-heap Discussion Thank you Write me: valeramoiseenko@gmail.com