Become A Guru How T o Solve A Memory Leak In Under 10 Minutes - - PowerPoint PPT Presentation

become a guru
SMART_READER_LITE
LIVE PREVIEW

Become A Guru How T o Solve A Memory Leak In Under 10 Minutes - - PowerPoint PPT Presentation

Become A Guru How T o Solve A Memory Leak In Under 10 Minutes Confidential - do not distribute What You Will Learn A methodology for approaching memory leaks Understanding the generational heap Understanding generational aging to


slide-1
SLIDE 1

Confidential - do not distribute

Become A Guru

How T

  • Solve A Memory Leak In Under 10 Minutes
slide-2
SLIDE 2

2

What You Will Learn

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • A methodology for approaching memory leaks
  • Understanding the generational heap
  • Understanding generational aging to find leaks
  • Using various tools to identify and analyze leaks
  • A step-by-step approach so you don’t need to remember techniques
  • Great places to go on holiday
slide-3
SLIDE 3

3

Methodology

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com Picture in Hoi An from lwebber28 travelling in Vietnam https://www.instagram.com/p/BnbyXRVA7Jz/?taken-by=hotelsdotcom

slide-4
SLIDE 4

4

A methodology for approaching memory leaks

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • 1. Do I have a leak (that needs fixing) ?
  • 2. What is leaking (which classes) ?
  • 3. What is keeping objects alive (an instance in the app) ?
  • 4. Where is it leaking from (code where the objects are created and/or assigned) ?
slide-5
SLIDE 5

5

OOME

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com Picture of Juanillo Beach from Carla travelling in the Dominican Republic https://www.instagram.com/p/Bng782cAgNQ/?taken-by=hotelsdotcom

slide-6
SLIDE 6

6

A methodology for approaching memory leaks

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • 1. Do I have a leak (that needs fixing) ?
  • 2. What is leaking (which classes) ?
  • 3. What is keeping objects alive (an instance in the app) ?
  • 4. Where is it leaking from (code where the objects are created and/or assigned) ?
slide-7
SLIDE 7

7

You *might* have a leak if you get an OOME

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • IMPORTANT! Read the OOME Message, it tells you specifically which space caused

the leak

  • You probably have a leak, BUT
  • Maybe your heap is just too small for your application, so check if a larger heap works
  • The next section on GCViewer will help you work out if it’s a leak
slide-8
SLIDE 8

8

Two Generation Heap

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com Picture in Plaza Espana from someone travelling in Seville, Spain https://www.instagram.com/p/BkV5a1kDxRB/?taken-by=hotelsdotcom

slide-9
SLIDE 9

9

A methodology for approaching memory leaks

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • 1. Do I have a leak (that needs fixing) ?
  • 2. What is leaking (which classes) ?
  • 3. What is keeping objects alive (an instance in the app) ?
  • 4. Where is it leaking from (code where the objects are created and/or assigned) ?
slide-10
SLIDE 10

10

Young And Old Generation Heaps

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • You need to know this so that you can analyse GC
  • But it’s pretty straightforward for memory leak analysis
  • Objects are created in the Young generation and last a while there
  • Then if they stay alive long enough, they move to the old generation

–Old generation GCs take a long time – Young generation GCs are quick

  • That’s it!
slide-11
SLIDE 11

11

Young And Old Generation Heaps

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

slide-12
SLIDE 12

12

GC logging

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • Turn on GC logging

– Before Java 9 – -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps - Xloggc:[file] -XX:+PrintReferenceGC -XX:+PrintTenuringDistribution - XX:+PrintGCApplicationStoppedTime -XX:+UseGCLogFileRotation - XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=10M – Java 9+ – -Xlog:gc*,gc+ref=debug,gc+age=trace,gc+heap=debug:file=gc%p%t.log: tags,uptime,time:filecount=10,filesize=10m

slide-13
SLIDE 13

13

GCViewer & Memory leaks

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

DEMO

slide-14
SLIDE 14

14

GC Log Memory Leak Identification

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • Really simple
  • Look at the heap used AFTER each Old Generation GC (Full GC)
  • If that heap size is continually increasing, you have a leak
  • Can also get sudden spike causing OOME – GCViewer will show that too
  • GCViewer only shows the heap, not other spaces, so this doesn’t help identify native

memory exhaustion leaks – Sorry, that’s another talk

slide-15
SLIDE 15

15

Class Histogram

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com Picture from Michael Long travelling in Jamaica https://www.instagram.com/p/BeWLc-yFUMX/?taken-by=hotelsdotcom

slide-16
SLIDE 16

16

A methodology for approaching memory leaks

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • 1. Do I have a leak (that needs fixing) ?
  • 2. What is leaking (which classes) ?
  • 3. What is keeping objects alive (an instance in the app) ?
  • 4. Where is it leaking from (code where the objects are created and/or assigned) ?
slide-17
SLIDE 17

17

Class Histogram

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • jmap –histo:live <pid>
  • Most profilers memory analysis histogram
  • Heap dump histogram
slide-18
SLIDE 18

18

Memory profiling & analysis

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

DEMO

slide-19
SLIDE 19

19

Heap Dump

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com Picture from Jonny travelling in Puglia, Italy https://www.instagram.com/p/BneXJuCD2nG/?taken-by=hotelsdotcom

slide-20
SLIDE 20

20

A methodology for approaching memory leaks

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • 1. Do I have a leak (that needs fixing) ?
  • 2. What is leaking (which classes) ?
  • 3. What is keeping objects alive (an instance in the app) ?
  • 4. Where is it leaking from (code where the objects are created and/or assigned) ?
slide-21
SLIDE 21

21

Heap Dump

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • -XX:+HeapDumpOnOutOfMemoryError
  • jmap -dump:live,file=<file-path> <pid>

– Or without “live,” if you want to see dead objects that have not yet been GCed, “live,” forces a GC before the dump

  • JMX: com.sun.management.HotSpotDiagnostic.dumpHeap()

– Eg from jconsole, visualvm, even programmatically

  • jcmd <pid> GC.heap_dump <file-path>
slide-22
SLIDE 22

22

Heap Dump Viewers

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • Lots of profilers and some utilities
  • I’m going to use the most popular: Eclipse MAT
slide-23
SLIDE 23

23

Heap dump analysis

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

DEMO

slide-24
SLIDE 24

24

Generational Profiling

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com Picture of Corona Arch from Michael travelling in Utah, USA https://www.instagram.com/p/BneXJuCD2nG/?taken-by=hotelsdotcom

slide-25
SLIDE 25

25

A methodology for approaching memory leaks

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • 1. Do I have a leak (that needs fixing) ?
  • 2. What is leaking (which classes) ?
  • 3. What is keeping objects alive (an instance in the app) ?
  • 4. Where is it leaking from (code where the objects are created and/or

assigned)?

slide-26
SLIDE 26

26

Memory profiling & analysis

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

DEMO SETUP

slide-27
SLIDE 27

27

Generation Count – Short Lived Objects

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

A GC ID Age B C D A 1 B 1 C 1 D 1 GC E B 2 C 2 D 2 E 1 F GC E 2 F 1 G Y W 2 X 1 Z 1 generation (all aged 0) 2 generations (aged 0 and 1) 3 generations (aged 0, 1 and 2) 3 generations (aged 0, 1 and 2) 3 generations (aged 0, 1 and 2) GC GC

slide-28
SLIDE 28

28

Generation Count – Long Lived Objects

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

A GC ID Age B C D A 1 B 1 C 1 D 1 GC E GC GC 1 generation (all aged 0) 2 generations (aged 0 and 1) 2 generations (aged 1 and 2) 2 generations (aged 2 and 3) 2 generations (aged 98 and 99) A 2 B 2 C 2 D 2 E 1 A 3 B 3 C 3 D 3 E 2 A 99 B 99 C 99 D 99 E 98 GC

slide-29
SLIDE 29

29

Generation Count – Leaking Objects

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

A GC ID Age A 1 B GC GC 1 generation (all aged 0) 2 generations (aged 0 and 1) 3 generations (aged 0, 1 and 2) 4 generations (aged 0,1, 2 and 3) 100 generations (aged 0 to 99) A 2 B 1 C A 3 B 2 C 1 D A 99 B 98 C 97 D 96 E 95 GC GC F 94 Y 1 Z

slide-30
SLIDE 30

30

Memory profiling & analysis

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

DEMO

slide-31
SLIDE 31

31

Tools

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com

  • GC Logging

– Suitable for production – GC logs remain after JVM terminates

  • GCViewer

– Suitable for production – views GC logs

  • Heap Dumping

–Suitable for production: but freezes JVM so only when necessary – log remains

  • Eclipse MAT

– Suitable for production – views Heap Dumps

  • VisualVM (use ‘profiler’ with allocation stack traces recording on)

– NOT Suitable for production – needs a live JVM and can crash it (all too often)

slide-32
SLIDE 32

32

Who Am I? Jack Shirazi

#hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com Picture from Alessia travelling in Naples, Italy https://www.instagram.com/p/BkS_yvDlbxd/?taken-by=hotelsdotcom

  • Working in Performance and Reliability

Engineering Team at Hotels.com

– Part of Expedia Group, handling over $100billion in bookings annually – World’s largest travel agency

  • Founder of JavaPerformanceTuning.com
  • Author of Java Performance Tuning

(O'Reilly)

  • Published over 60 articles on Java

Performance Tuning & a monthly newsletter for 15 years & around 10 000 tuning tips

slide-33
SLIDE 33

33 #hcomtechnology -- presenter: Jack Shirazi -- slides: fasterj.com/jaxlondon2018.zip -- hotels.com -- expedia.com