(JVM) Garbage Collection - Brown Bag Session
-
Upload
jens-hadlich -
Category
Technology
-
view
291 -
download
1
Transcript of (JVM) Garbage Collection - Brown Bag Session
(JVM) Garbage Collection & Co.
Brown Bag Session
Jens Hadlich 2015-12-08
Garbage Collection
2
Why should I care?
Garbage Collection
4
Can have a huge impact
30 seconds
20 seconds
10 seconds
~16 hours
Outline
• Some Theory
• Practice & real world examples
• Tools & live demos
• Configuration best practices
• Discussion
5
Some Theory
Garbage Collection
7
Garbage Collection
8
Terminology
Garbage collection … is a form of automatic memory management.
Garbage … memory occupied by objects that are no longer used.
9
Pros and Cons
Pros - Programmer does not need to worry about memory (de-)allocation
(leading to fewer memory leaks)
Cons - Performance overhead - Can lead to more complex programming language design
10
More Terminology
Collector Heap Mutator Roots
11
Roots
12
Garbage Collection
Algorithms
13
Tracing Reference Counting
Collection Style Batch Incremental
Cost Per Mutation None High
Throughput High Low
Pause Times Long Short
Real Time? No Yes
Collects Cycles? Yes No
Tracing algorithms
Mark & Sweep Copying
14
Think of a bookshelf
15
Tracing algorithms
Mark & Sweep Copying
16
Mark & Sweep
Mark the books you want to keep
17
Mark & Sweep
Throw away the books that you don’t want to keep
18
Mark & Sweep
Problem: Fragmentation
19
This size book won’t fit!
Tracing algorithms
Mark & Sweep Copying
20
Copying
Need 2 shelves
21
Copying
Move (“copy”) books you want to keep
22
Copying
Throw away books you don’t want to keep
23
Further readings
24
Java
HotSpot JVM Architecture
26
http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html
Java
27
Metaspace (Java 8 and later)
(Java 7 and earlier)
Java
Generational Garbage Collection
28
Hypothesis: Most objects die young
Generational Garbage Collection
29
Tools & Practice
Garbage Collection
Tools (Memory Analyzer) Java VisualVM jClarity Censum Grafana Icinga
31
A little bit of GC Tuning
GC Tuning
Performance Triangle
33
Memory Footprint
Throughput Latency
M
T L
GC Tuning
Better Performance Triangle
34
Compactness
Throughput Responsiveness
C
T R
C x T x R = a Optimization: Increase a
Responsiveness vs. Throughput
35
Collectors
Collectors (new generation) -XX:+UseSerialGC (Copy) -XX:+UseParNewGC (used together with old gen CMS) -XX:+UseG1GC (same for old gen)
36
Collectors
Collectors (old generation) Throughput: -XX:+UseSerialGC (MarkSweepCompact) -XX:+UseParallelGC -XX:+UseParallelOldGC Low-pause: -XX:+UseConcMarkSweepGC -XX:+UseG1GC
37
Concurrent Mark-Sweep
38
Fun with JVM Options
39
Fun with JVM Options
Most important -server Crucial for all long-running server applications. http://stackoverflow.com/questions/198577/real-differences-between-java-server-and-java-client
40
Fun with JVM Options
Most important (cont’d) -Xms (-XX:InitialHeapSize) -Xmx (-XX:MaxHeapSize) Setting Xms = Xmx (slightly) saves some overhead.
41
Fun with JVM Options
Enable GC logs -Xloggc:logs/gc-<app>.log -XX:+PrintGCCause -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=2 -XX:GCLogFileSize=128M
42
Fun with JVM Options
Enable JMX remoting -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=<port> -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
43
Think about your program
https://flink.apache.org/news/2015/05/11/Juggling-with-Bits-and-Bytes.html
44
Best practices
Best practices
“Good” JVM options to start with -server -Xms1g -Xmx1g -XX:+AlwaysPreTouch -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+DisableExplicitGC
46
Best practices
Notes on G1GC -XX:+UseG1GC http://www.oracle.com/technetwork/articles/java/g1gc-1984535.html (G1GC shall become the default collector in Java 9)
47
Best practices
“Good” JVM options to start with (G1GC) -server -Xms1g -Xmx1g -XX:+AlwaysPreTouch -XX:+UseG1GC -XX:+DisableExplicitGC
48
Best practices
Keep the heap size below 30 GB if possible. Why? Compressed oops. https://wiki.openjdk.java.net/display/HotSpot/CompressedOops http://docs.oracle.com/javase/7/docs/technotes/guides/vm/performance-enhancements-7.html (enabled by default since Java 7 for 64-bit)
49
Best practices
Look out for specific tuning guides / recommendations. https://www.elastic.co/guide/en/elasticsearch/guide/current/heap-sizing.html
50
The End?
The End?
Other things that can go wrong.
Linux OOM Killer
53
System (3GB memory)
JVM (Xmx=2g)
/var/log/kern.log jvm.memory.total.used
OOM
54
hs_err_pid<pid>.log
Questions?
Add-on slides
Go
57
Java Profiling
58