trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity...

48
Copyright 2018 Kirk Pepperdine MEMORY THE TROUBLE WITH

Transcript of trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity...

Page 1: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

MEMORYTHE TROUBLE WITH

Page 2: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

jClarity

▸ Kirk Pepperdine

▸ Authors of jPDM, a performance diagnostic model

▸ Co-founded jClarity

▸ Building the smart generation of performance diagnostic tooling

▸ Bring predictability into the diagnostic process

▸ Co-founded JCrete

▸ The hotest unconference on the planet

▸ Java Champion(s)

OUR MARKETING SLIDE

Page 3: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

jClarity

What is your performance trouble spot

Page 4: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

jClarity

> 70% of all applications are bottlenecked on memory

Page 5: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

jClarity

and no,Garbage Collection

is not a fault!!!!

Page 6: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

Page 7: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

Spring Boot

Page 8: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

Cassandra

Page 9: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

Cassandraor any big nosql solution

Page 10: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

Apache Spark

Page 11: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

Apache Sparkor any big data framework

Page 12: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

Log4J

Page 13: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

Log4Jor any Java logging framework

Page 14: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

JSON

Page 15: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

JSONWith almost any Marshalling protocol

Page 16: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

ECom caching products

Page 17: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

ECom caching productsHibernate

Page 18: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

ECom caching productsHibernateand so on

Page 19: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

ECom caching productsHibernateand so onand so on

Page 20: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

DO YOU USE jC

larity

ECom caching productsHibernateand so onand so onand so on

Page 21: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

jClarity

then you are very likely in this 70%

Page 22: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

Page 23: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

PROBLEMS▸ High memory churn rates

▸many temporary objects

▸ Large live data set size

▸ inflated live data set size

▸ loitering

▸ Unstable live data set size

▸memory leak

jClarity

Page 24: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

WAR STORIES▸ Reduced allocation rates from 1.8gb/sec to 0

▸ tps jumped from 400,000 to 25,000,000!!!

▸ Stripped all logging our of a transactional engine

▸Throughput jumped by a factor of 4x

▸Wrapped 2 logging statements in a web socket framework

▸Memory churn reduced by a factor of 2

jClarity

Page 25: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

ALLOCATION SITE jC

larity

Foo foo = new Foo();

forms an allocation site

0: new #2 // class java/lang/Object2: dup4: invokespecial #1 // Method java/lang/Object."<init>":()V

▸ Allocation will (mostly) occur in Java heap

▸ fast path

▸slow path

▸small objects maybe optimized to an on-stack allocation

Page 26: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

JAVA HEAP jC

larity

Eden Survivor (to) Tenured

Java Heap

▸ Java Heap is made of;

▸Eden - nursery

▸Survivor - intermediate pool designed to delay promotion

▸Tenured - to hold long lived data

▸Each space contributes to a different set of problems

▸All affect GC overhead

Page 27: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

EDEN ALLOCATIONS jC

larity

top of heap pointer

Page 28: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

OBJECT ALLOCATION jC

larity

top of heap pointer

Foo foo = new Foo();Bar bar = new Bar();byte[] array = new byte[N];

Page 29: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

OBJECT ALLOCATION jC

larity

top of heap pointer

Foo foo = new Foo();Bar bar = new Bar();byte[] array = new byte[N];

Foo

Page 30: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

OBJECT ALLOCATION jC

larity

top of heap pointer

Foo foo = new Foo();Bar bar = new Bar();byte[] array = new byte[N];

Foo Bar

Page 31: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

OBJECT ALLOCATION jC

larity

top of heap pointer

Foo foo = new Foo();Bar bar = new Bar();byte[] array = new byte[N];

Foo Bar byte[]

Page 32: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

OBJECT ALLOCATION jC

larity

top of heap pointer

Foo Bar byte[]

▸ In multi-threaded apps, top of heap pointer must be surrounded by barriers

▸single threads allocation

▸hot memory address

▸ solved by stripping (Thread local allocation blocks)

Page 33: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

TLAB ALLOCATION jC

larity

top of heap pointer

▸ Assume 2 threads

▸each thread will have it’s own (set of) TLAB(s)

TLAB TLAB

TLAB pointerTLAB pointer

Page 34: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

TLAB ALLOCATIONS jC

larity

▸ Thread 1 -> Foo foo = new Foo(); byte[] array = new byte[N];

▸byte[] doesn’t fit in a TLAB

▸ Thread 2 -> Bar bar = new Bar();

byte[]TLAB TLABFoo Bar

TLAB pointerTLAB pointertop of heap pointer

Page 35: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

TLAB WASTE % jC

larity

▸ Allocation failure to prevent buffer overflow

▸waste up to 1% of a TLAB

FooFoo Foo Foo Foo Foo Foo Foo

Page 36: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

TLAB WASTE % jC

larity

▸ Allocation failure to prevent buffer overflow

▸waste up to 1% of a TLAB

Foo Foo Foo Foo Foo Foo Foo FooFoo

Page 37: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

TENURED SPACE jC

larity

▸ Allocations in tenured make use of a free list

▸ free list allocation is ~10x the cost of bump and run

▸Data in tenured tends to be long lived

▸amount of data in tenured do affect GC pause times

Bar Bar Foo Foo

Free List

Page 38: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

PROBLEMS▸ High memory churn rates

▸many temporary objects

jClarity

Page 39: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

PROBLEMS▸ High memory churn rates

▸many temporary objects

jClarity

▸Quickly fill Eden

▸ frequent young gc cycles

▸ speeds up aging

▸ premature promotion

▸more frequent tenured cycles

▸ increased copy costs

▸ increased heap fragmentation

▸Allocation is quick

▸ quick * large number = slow

Page 40: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

REDUCING ALLOCATIONS jC

larity

> 1gb/sec

< 300mb/sec

size

of g

ain

Page 41: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

PROBLEMS▸ High memory churn rates

▸many temporary objects

▸Large live data set size

▸ inflated live data set size

▸ loitering

jClarity

Page 42: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

PROBLEMS▸ High memory churn rates

▸many temporary objects

▸Large live data set size

▸ inflated live data set size

▸ loitering

jClarity

▸ inflated scan for root times

▸ reduced page locality

▸ Inflated compaction times

▸ increase copy costs

▸ likely less space to copy too

Page 43: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

PAUSE VS OCCUPANCY jC

larity

Page 44: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

PROBLEMS▸ High memory churn rates

▸many temporary objects

▸Large live data set size

▸ inflated live data set size

▸ loitering

▸Unstable live data set size

▸memory leak

jClarity

Page 45: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

PROBLEMS▸ High memory churn rates

▸many temporary objects

▸Large live data set size

▸ inflated live data set size

▸ loitering

▸Unstable live data set size

▸memory leak

jClarity

▸Eventually you run out of heap space

▸ each app thread throws an OutOfMemoryError and terminates

▸JVM will shutdown with all non-daemon threads terminate

Page 46: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

jClarity

Escape Analysis

Page 47: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

Demo time

jClarity

Page 48: trouble with memory - QCon · 2021. 8. 4. · Send us a Java 11 GC log or tweet about @jclarity #QConSF and #censum and get a free Censum License. Title: trouble_with_memory.key Author:

Copyright 2018 Kirk Pepperdine

TEXT

TITLE TEXT

▸ Body Level One

▸ Body Level Two

▸ Body Level Three

▸ Body Level Four

▸ Body Level Five

Ask out my Java Performance Tuning Workshop

Send us a Java 11 GC log or tweet about @jclarity

#QConSF and #censum and

get a free Censum License