Slide 1

Slide 1 text

Finding Your Garbage Dmitry Buzdin

Slide 2

Slide 2 text

Summary • Intro to JVM Memory Areas • Intro to GC principles • Introducing CMS and G1 • Configuration possibilities • Measuring GC

Slide 3

Slide 3 text

Memory Management • In languages like C++ you have to free memory • JVM cleans garbage after you • It does not mean that you can do whatever you want

Slide 4

Slide 4 text

Common Memory Problems • Memory Leaks • “Stop the world” pauses • High CPU consumption by GC • Memory fragmentation

Slide 5

Slide 5 text

Memory Leaks https://plumbr.eu/blog/memory-leaks/memory-leaks-fallacies- and-misconceptions OK Memory Leak

Slide 6

Slide 6 text

Why do you need to know GC? • Understand whats going on under the hood • Interpret monitoring metrics • Understand how to adapt your code to JVM • Don’t optimise prematurely!

Slide 7

Slide 7 text

Lets revisit what goes into heap

Slide 8

Slide 8 text

JVM Data Areas

Slide 9

Slide 9 text

What Goes Where?

Slide 10

Slide 10 text

Heap Size

Slide 11

Slide 11 text

Generational GC

Slide 12

Slide 12 text

Heap Structure “Metaspace” since JDK8

Slide 13

Slide 13 text

Object Promotion Object age is recorded and incremented with each GC cycle

Slide 14

Slide 14 text

What gets Collected? • Garbage = unreachable objects • How do you know if object is unreachable?

Slide 15

Slide 15 text

GC Roots • Local variables of currently executed methods • Active threads • Static field of loaded classes

Slide 16

Slide 16 text

Ideal GC Algorithm • Predictable pauses • Low latency • Supports large heaps • Not CPU intensive • Uses memory efficiently

Slide 17

Slide 17 text

HotSpot GC Algorithms • SerialGC -XX:UseSerialGC • ParallelGC -XX:UseParallelGC • CMS -XX:+UseConcMarkSweepGC • G1 JDK 9 default

Slide 18

Slide 18 text

CMS Algorithm • Mostly Concurrent mark and sweep • Designed to avoid long pauses -XX:+UseConcMarkSweepGC

Slide 19

Slide 19 text

CMS Heap Structure

Slide 20

Slide 20 text

Before GC

Slide 21

Slide 21 text

Young Generation Collection

Slide 22

Slide 22 text

After Young GC

Slide 23

Slide 23 text

Old Gen Collection Occupancy rate is reached -XX:CMSInitiatingOccupancyFraction=75

Slide 24

Slide 24 text

CMS Collection Steps • Initial mark (STW) - collect all GC roots • Concurrent mark - mark all live objects in old gen • Concurrent preclean - mark dirty live objects • Concurrent abortable preclean - continuation of above • Final remark (STW) - finalize marking of live objects • Concurrent sweep - reclaiming space concurrently • Concurrent reset - reset internal data structures

Slide 25

Slide 25 text

Concurrent Marking

Slide 26

Slide 26 text

After Sweeping

Slide 27

Slide 27 text

CMS in JDK9 • CMS is deprecated and to be removed in JDK 10? • Risk is that it outperforms G1 in some cases http://openjdk.java.net/jeps/291

Slide 28

Slide 28 text

G1 Algoritm • Garbage First • Lots of small regions instead of large ones • Predictable “stop-the-world” pauses • Large heap sizes with low latency

Slide 29

Slide 29 text

G1 Heap Structure Fixed Size Memory Regions (1mb - 32mb) ± 2000

Slide 30

Slide 30 text

G1 Step by Step

Slide 31

Slide 31 text

Young GC Also called Evacuation Phase All young regions plus some old ones Stop the World GC

Slide 32

Slide 32 text

End of Young GC

Slide 33

Slide 33 text

Initial Marking Phase G1 GC marks the roots during the initial-mark phase. Happens with Evacuation Pause

Slide 34

Slide 34 text

Concurrent Marking Phase G1 GC looks for reachable objects across the entire heap. This phase happens concurrently.

Slide 35

Slide 35 text

Remark Phase Empty regions are removed Stop the world pause Completes the marking process

Slide 36

Slide 36 text

Copying/Cleanup Phase Stop the world + concurrent

Slide 37

Slide 37 text

After Copying/Cleanup Phase Space has been reclaimed Memory regions compacted

Slide 38

Slide 38 text

Configuring G1 • -XX:MaxGCPauseMillis (realistic soft limitation) • -XX:ParallelGCThreads • -XX:ConcGCThreads • -XX:InitiatingHeapOccupancyPercent • and more

Slide 39

Slide 39 text

G1 Recap • Based on similar concepts as CMS • Additional dimensions with RSets and CSets • Support for humongous objects

Slide 40

Slide 40 text

There is no best GC algorithm • Tradeoff between • Throughput • Latency • Heap size

Slide 41

Slide 41 text

GC Monitoring • JMX and JConsole • Visual VM • GC Logs • GC Heap Dumps

Slide 42

Slide 42 text

JMX JConsole

Slide 43

Slide 43 text

VisualVM

Slide 44

Slide 44 text

GC Logging http://openjdk.java.net/jeps/158

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Heap Dump -XX:-HeapDumpOnOutOfMemoryError

Slide 47

Slide 47 text

How to find the best one for your project • Experiment • Try different settings • Observe metrics • Understand the effect • Try again…

Slide 48

Slide 48 text

Java GC Future • Further focus on G1 and improvements • Shenandoah https://rkennke.wordpress.com/

Slide 49

Slide 49 text

References • http://www.oracle.com/webfolder/technetwork/ tutorials/obe/java/gc01/index.html • http://www.oracle.com/technetwork/tutorials/ tutorials-1876574.html • https://plumbr.eu/blog/memory-leaks/memory- leaks-fallacies-and-misconceptions • http://www.slideshare.net/buzdin/java7-garbage- collector-g1