Become A Guru - Java2Days · •You need to know this so that you can analyse GC •But it’s...
Transcript of Become A Guru - Java2Days · •You need to know this so that you can analyse GC •But it’s...
![Page 1: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/1.jpg)
Confidential - do not distribute
Become A GuruHow To Solve A Memory Leak In Under 10 Minutes
![Page 2: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/2.jpg)
2
What You Will Learn
#hcomtechnology -- presenter: Jack Shirazi -- -- 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
![Page 3: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/3.jpg)
3
Methodology
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
Picture in Hoi An from lwebber28 travelling in Vietnam
https://www.instagram.com/p/BnbyXRVA7Jz/?taken-by=hotelsdotcom
![Page 4: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/4.jpg)
4
A methodology for approaching memory leaks
#hcomtechnology -- presenter: Jack Shirazi -- -- 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) ?
![Page 5: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/5.jpg)
5
OOME
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
Picture of Juanillo Beach from Carla travelling in the Dominican Republic
https://www.instagram.com/p/Bng782cAgNQ/?taken-by=hotelsdotcom
![Page 6: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/6.jpg)
6
A methodology for approaching memory leaks
#hcomtechnology -- presenter: Jack Shirazi -- -- 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) ?
![Page 7: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/7.jpg)
7
You *might* have a leak if you get an OOME
#hcomtechnology -- presenter: Jack Shirazi -- -- 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
![Page 8: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/8.jpg)
8
Two Generation Heap
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
Picture in Plaza Espana from someone travelling in Seville, Spain
https://www.instagram.com/p/BkV5a1kDxRB/?taken-by=hotelsdotcom
![Page 9: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/9.jpg)
9
A methodology for approaching memory leaks
#hcomtechnology -- presenter: Jack Shirazi -- -- 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) ?
![Page 10: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/10.jpg)
10
Young And Old Generation Heaps
#hcomtechnology -- presenter: Jack Shirazi -- -- 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!
![Page 11: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/11.jpg)
11
Young And Old Generation Heaps
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
![Page 12: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/12.jpg)
12
GC logging
#hcomtechnology -- presenter: Jack Shirazi -- -- 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
![Page 13: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/13.jpg)
13
GCViewer & Memory leaks
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
DEMO
![Page 14: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/14.jpg)
14
GC Log Memory Leak Identification
#hcomtechnology -- presenter: Jack Shirazi -- -- 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
![Page 15: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/15.jpg)
15
Class Histogram
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
Picture from Michael Long travelling in Jamaica
https://www.instagram.com/p/BeWLc-yFUMX/?taken-by=hotelsdotcom
![Page 16: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/16.jpg)
16
A methodology for approaching memory leaks
#hcomtechnology -- presenter: Jack Shirazi -- -- 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) ?
![Page 17: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/17.jpg)
17
Class Histogram
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
• jmap –histo:live <pid>
• Most profilers memory analysis histogram
• Heap dump histogram
![Page 18: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/18.jpg)
18
Memory profiling & analysis
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
DEMO
![Page 19: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/19.jpg)
19
Heap Dump
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
Picture from Jonny travelling in Puglia, Italy
https://www.instagram.com/p/BneXJuCD2nG/?taken-by=hotelsdotcom
![Page 20: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/20.jpg)
20
A methodology for approaching memory leaks
#hcomtechnology -- presenter: Jack Shirazi -- -- 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) ?
![Page 21: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/21.jpg)
21
Heap Dump
#hcomtechnology -- presenter: Jack Shirazi -- -- 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>
![Page 22: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/22.jpg)
22
Heap Dump Viewers
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
• Lots of profilers and some utilities
• I’m going to use the most popular: Eclipse MAT
![Page 23: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/23.jpg)
23
Heap dump analysis
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
DEMO
![Page 24: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/24.jpg)
24
Generational Profiling
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
Picture of Corona Arch from Michael travelling in Utah, USA
https://www.instagram.com/p/BneXJuCD2nG/?taken-by=hotelsdotcom
![Page 25: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/25.jpg)
25
A methodology for approaching memory leaks
#hcomtechnology -- presenter: Jack Shirazi -- -- 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)?
![Page 26: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/26.jpg)
26
Memory profiling & analysis
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
DEMO SETUP
![Page 27: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/27.jpg)
27
Generation Count – Short Lived Objects
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
A
0
GC
ID
Age
B
0 C
0D
0
A
1
B
1 C
1D
1
GC
E
0
B
2 C
2D
2
E
1F
0
GC
E
2F
1
G
0
Y
0
W
2
X
1
Z
0
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)
GCGC
![Page 28: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/28.jpg)
28
Generation Count – Long Lived Objects
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
A
0
GC
ID
Age
B
0 C
0D
0
A
1
B
1 C
1D
1
GC
E
0
GC GC1 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
2D
2
E
1
A
3
B
3 C
3D
3
E
2
A
99
B
99 C
99D
99
E
98
GC
![Page 29: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/29.jpg)
29
Generation Count – Leaking Objects
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
A
0
GC
ID
Age
A
1
B
0
GC GC1 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
0
A
3
B
2 C
1D
0
A
99B
98C
97D
96E
95
GCGC
F
94
Y
1Z
0
![Page 30: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/30.jpg)
30
Memory profiling & analysis
#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com
DEMO
![Page 31: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/31.jpg)
31
Tools
#hcomtechnology -- presenter: Jack Shirazi -- -- 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)
![Page 32: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/32.jpg)
32
Who Am I? Jack Shirazi
#hcomtechnology -- presenter: Jack Shirazi -- -- 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
![Page 33: Become A Guru - Java2Days · •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](https://reader033.fdocuments.in/reader033/viewer/2022051811/601e12ab2afba20623363c23/html5/thumbnails/33.jpg)
33#hcomtechnology -- presenter: Jack Shirazi -- -- hotels.com -- expedia.com