eclipse memory analyzer tool
DESCRIPTION
This presentation is a basic introduction to MAT. Talks about the basic concepts in Heap Analysis.TRANSCRIPT
![Page 1: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/1.jpg)
Eclipse Memory Analyzer Tool
Lakshman Kakkiralalittle eye labs
![Page 2: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/2.jpg)
Agenda
● Memory Analysis● Some Definitions● An example leak code● Demo of MAT and some basic functionality
![Page 3: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/3.jpg)
Prereqs
● Android app development● Basic Heap and Garbage Collection
Disqualification● Advanced knowledge of MAT :-)
![Page 4: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/4.jpg)
CT Scanner for your apphttp://www.littleeye.co
littleEye appInsight
![Page 5: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/5.jpg)
Why Memory Analysis?
● Identify Leaks● Reduce memory footprint
![Page 6: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/6.jpg)
Typical problems
● Holding Context object (an Activity, a View or a Drawable)which actually happens to be an Activity
● Non-static inner classes● Caches● Aggressive Caching
![Page 7: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/7.jpg)
Some definitions
● Shallow Size● Retained Size● Dominator● Dominator Tree ● GCRoots
![Page 8: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/8.jpg)
Example - Object Graph
D 100 100
B100300
C 100 200
A 100 600
E 100 100
F 100 100
![Page 9: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/9.jpg)
Example (contd) - Dominator Tree
D
B
C
A
E
F
![Page 10: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/10.jpg)
Example - Memory Leakpublic class MainActivity extends Activity { private static LeakClass leak;
// *NOT A STATIC* class LeakClass { }
@Override public void onCreate(Bundle savedInstanceState) { ....
if (leak == null) { leak = new LeakClass(); } ... }}
![Page 11: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/11.jpg)
Investigation Tools - LogCat
● D/dalvikvm( 9050 ): GC_CONCURRENT freed 2049K, 65% free 3571K/9991K, external 4703K/5261K, paused 2ms+2ms
![Page 12: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/12.jpg)
Heapdump
● What - Snapshot of all the objects in the process○ Fields, References, Primitive Values○ Classes, Class Loaders
● How to generate○ DDMS view - "Dump HPROF file" icon ○ android.os.Debug.dumpHprofData(<filename>)○ signal 10
● Convert to standard HPROF format○ hprof-convert (only required if using standalone)
![Page 13: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/13.jpg)
MAT aka Eclipse Memory Analyzer Tool
● Standalone - Download http://www.eclipse.org/mat
● Pros○ Visible Primitive Data ○ Powerful selection model○ No setup
● Cons○ Large dump files○ Cannot answer code/temporal questions
■ Who/Where/When - an object is created■ When - an object is garbage collected
![Page 14: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/14.jpg)
Demo
Covering● View Dominator Tree● Inspector
○ look at the field values○ browse through entries in a Collection
● Class Histograms● Group by Value
![Page 15: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/15.jpg)
Demo (contd...)
● Immediate Dominators● Path from GC roots● Retained Set● Object Query Language
![Page 16: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/16.jpg)
References
● Google IO 2011 - "Memory Management for Android apps" http://www.google.com/events/io/2011/sessions/memory-management-for-android-apps.html
● Java Memory Analysishttp://vimeo.com/21356498
● Markus Kohler's Java Performance Bloghttp://kohlerm.blogspot.com
● Memory Analyzer Bloghttp://memoryanalyzer.blogspot.in/
![Page 17: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/17.jpg)
![Page 18: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/18.jpg)
Backup Slides
Application Attributes:<application android:largeHeap="true">
![Page 19: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/19.jpg)
Garbage Collection
Pre Gingerbread:GC: stop the world gc, >100ms
Gingerbread and beyond:concurrent gc,two pauses - at the beginning and at the end < 5mspartial collections
![Page 20: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/20.jpg)
Bitmap
pre-honeycomb:● pixel data was stored in native mem● recycle() or finalize()● invisible to MAT
honeycomb and after:● pixel data inside dalvik heap● visible to MAT● works with partial and concurrent GC
![Page 21: Eclipse Memory Analyzer Tool](https://reader031.vdocuments.net/reader031/viewer/2022020720/5478d47eb37959582b8b45aa/html5/thumbnails/21.jpg)
Typical Garbage Collections
GC_CONCURRENTGC_FOR_MALLOCGC_EXTERNAL_ALLOC (pre-honeycomb)GC_HPROF_DUMP_HEAP GC_EXPLICIT - app calling System.gc()