fixing memory leaks in android applications using ddms & mat
DESCRIPTION
Fixing Memory Leaks in Android Applications using DDMS & MAT. Presented by Sravankumar Reddy Javaji CS286 Mobile Programming. Evolution of Devices. HTC Dream (2008) 192 MB Ram 320×480 display. Nexus 5 (2013) 2 GB Ram 1920x1080 display. Heap Size. Heap size is limited and device dependent - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/1.jpg)
Fixing Memory Leaks in Android Applications using DDMS & MAT
Presented by Sravankumar Reddy Javaji
CS286 Mobile Programming
![Page 2: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/2.jpg)
HTC Dream (2008)192 MB Ram
320×480 display
Nexus 5 (2013)2 GB Ram
1920x1080 display
Evolution of Devices
![Page 3: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/3.jpg)
Heap Size
• Heap size is limited and device dependent G1 = 16 Mb
Droid = 24 Mb
Nexus One = 32 Mb
Xoom = 48 Mb
GalaxyTab = 64 Mb.
• ActivityManager.getMemoryClass()
![Page 4: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/4.jpg)
• Pre-defined heap-size settings are specified in /system/build.prop file for each device.
dalvik.vm.heapstartsize=5mdalvik.vm.heapgrowthlimit=48mdalvik.vm.heapsize=128mdalvik.vm.heaptargetutilization=0.75dalvik.vm.heapminfree=512kdalvik.vm.heapmaxfree=2m
• http://stackoverflow.com/questions/20011424/query-on-properties-in-system-build-prop
Heap Size ..contd..
![Page 5: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/5.jpg)
Honeycomb adds “largeHeap” option in AndroidManifest.xml:- Degrades performance! Use only if you understand why you need it.
<applicationandroid:name="com.example.foobar"android:largeHeap="true"...
</application>ActivityManager.getLargeMemoryClass()
Large Heaps
![Page 6: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/6.jpg)
• A larger memory heap Allows more objects to be created Takes longer to fill Allows the application to run longer between garbage collection (GC) events
• A smaller memory heap Holds fewer objects Fills more quickly Is garbage collected more frequently (but the pauses are shorter) May lead to out-of-memory errors
The more memory we use, the more work GC has, the more work GC has the bigger pause will be.
Battery life
larger heap VS smaller heap
![Page 7: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/7.jpg)
• Finding out when the Garbage Collection is run? - Search for 'GC' or 'gc' in LogCat
• How to trigger the Garbage Collection manually? - System.gc();
• Mark and sweep is one of the earliest and best-known garbage collection algorithms.
Garbage Collection
![Page 8: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/8.jpg)
Mark and Sweep Algorithm
GC Roots A
E
D G H
B F
C
![Page 9: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/9.jpg)
Mark and Sweep Algorithm
GC Roots A
E
D G H
B F
C
![Page 10: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/10.jpg)
Mark and Sweep Algorithm
GC Roots A
E
D G H
B F
C
![Page 11: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/11.jpg)
Mark and Sweep Algorithm
GC Roots A
E
D G H
B F
C
![Page 12: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/12.jpg)
Changes in Garbage Collection
Pre-Gingerbread GC:– Stop-the-world– Full heap collection– Large Pause Times. Pause times often > 100ms
Gingerbread and beyond:– Advantage of multi-core support. Concurrent (mostly)– Partial collections– Smaller pause times. Pause times usually < 5ms
![Page 13: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/13.jpg)
Bitmaps
Bitmaps take up a lot of memory, especially for rich images like photographs.
For example, the camera on the Galaxy Nexus takes photos up to 2592x1936 pixels (5 megapixels). Loading this image into memory takes about 19MB of
memory (2592*1936*4 bytes). If you're not careful, bitmaps can quickly consume your available
memory budget leading to an application crash due to the dreaded exception:java.lang.OutofMemoryError: bitmap size exceeds VM budget.
![Page 14: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/14.jpg)
Managing Bitmap Memory
Old way (pre-Honeycomb):– Bitmaps are saved in Native memory. Freed via recycle() or finalizer– hard to debug– full, stop-the-world GCs
New way:– Bitmaps are saved in Managed (Heap) memory. Freed synchronously by GC– easier to debug– concurrent & partial GCs
![Page 15: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/15.jpg)
Understanding heap usage
– DDMS (Dalvik Debug Monitor Server)– Eclipse Memory Analyzer (MAT)
![Page 16: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/16.jpg)
Debugging a memory leak
Let's walk through an example using the Honeycomb Gallery sample app from the Android SDK.
We're going to deliberately add a memory leak to this app in order to demonstrate how it could be debugged.
Lets implement a cache in this app which holds recently-viewed images by making a few small changes to ContentFragment.java.
![Page 17: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/17.jpg)
Debugging a memory leak ..contd..
At the top of the class, let's add a new static variable:private static HashMap<String,Bitmap> sBitmapCache = new
HashMap<String,Bitmap>();This is where we'll cache the Bitmaps that we load.
Now we can change the updateContentAndRecycleBitmap() method to check the cache before loading, and to add Bitmaps to the cache after they're loaded.void updateContentAndRecycleBitmap(int category, int position) { if (mCurrentActionMode != null) { mCurrentActionMode.finish(); }
![Page 18: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/18.jpg)
Debugging a memory leak ..contd..
// Get the bitmap that needs to be drawn and update the ImageView.
// Check if the Bitmap is already in the cache String bitmapId = "" + category + "." + position; mBitmap = sBitmapCache.get(bitmapId); if (mBitmap == null) { // It's not in the cache, so load the Bitmap and add it to the cache. // DANGER! We add items to this cache without ever removing any. mBitmap = Directory.getCategory(category).getEntry(position) .getBitmap(getResources()); sBitmapCache.put(bitmapId, mBitmap); } ((ImageView) getView().findViewById(R.id.image)).setImageBitmap(mBitmap);}
![Page 19: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/19.jpg)
Debugging a memory leak ..contd..
What is Memory Leak here?We added Bitmaps to the cache without ever removing them. In a real app, we'd probably want to limit the size of the cache in some way.
![Page 20: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/20.jpg)
Examining heap usage in DDMS
Select the process com.example.android.hcgallery in the left pane, and then click the Show heap updates button in the toolbar. Then, switch to the VM Heap tab in DDMS. It shows some basic stats about our heap memory usage, updated after every GC. To see the first update, click the Cause GC button.
We can see that our live set (the Allocated column) is a little over 8MB. Now flip through the photos, and watch that number go up. Since there are only 13 photos in this app, the amount of memory we leak is bounded. In some ways, this is the worst kind of leak to have, because we never get an OutOfMemoryError indicating that we are leaking.
![Page 21: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/21.jpg)
1
23
4
Examining heap usage in DDMS
![Page 22: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/22.jpg)
1
2
Creating a heap dump
![Page 23: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/23.jpg)
Click on Histogram view
Analyzing heap dumps using MAT
![Page 24: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/24.jpg)
Analyzing heap dumps using MATThe Histogram view shows a list of classes sortable by the number of instances,
the shallow heap or the retained heap.
![Page 25: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/25.jpg)
Analyzing heap dumps using MAT
If we sort by shallow heap, we can see that instances of byte[] are at the top. As of Android 3.0 (Honeycomb), the pixel data for Bitmap objects is stored in byte arrays (previously it was not stored in the Dalvik heap), and based on the size of these objects, it's a safe bet that they are the backing memory for our leaked bitmaps.
Right-click on the byte[] class and select List Objects > with incoming references. This produces a list of all byte arrays in the heap, which we can sort based on Shallow Heap usage.
Pick one of the big objects, and drill down on it. This will show you the path from the root set to the object -- the chain of references that keeps this object alive. Lo and behold, there's our bitmap cache!
![Page 26: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/26.jpg)
Analyzing heap dumps using MAT
Culprit
![Page 27: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/27.jpg)
Comparing heap dumps with MAT
We need to create two separate HPROF files.Open the first HPROF file (if it is not already opened)Open the Histogram view.Open the second HPROF file.Switch to first HPROF file's Histogram view and click “compare to
another Heap Dump” Icon.Then a window will be prompted to select a heap dump from the
open editors as baseline. Select second HPROF file and press OK.
![Page 28: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/28.jpg)
Comparing heap dumps with MAT..contd..
![Page 29: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/29.jpg)
Downloading MAT
MAT for Eclipse IDE:- http://download.eclipse.org/mat/1.3/update-site
Standalone MAT:- http://www.eclipse.org/mat/
![Page 30: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/30.jpg)
References
http://dubroy.com/blog/google-io-memory-management-for-android-apps/
https://developer.android.com/training/displaying-bitmaps/manage-memory.html
http://macgyverdev.blogspot.com/2011/11/android-track-down-memory-leaks.html
http://www.brpreiss.com/books/opus5/html/page424.html https://sites.google.com/site/pyximanew/blog/
androidunderstandingddmslogcatmemoryoutputmessages http://www.vogella.com/articles/EclipseMemoryAnalyzer/article.html http://rockgrumbler.blogspot.com/2011/02/common-memory-leak-
causes-in-java.html
![Page 31: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/31.jpg)
Queries. ?
![Page 32: Fixing Memory Leaks in Android Applications using DDMS & MAT](https://reader035.vdocuments.mx/reader035/viewer/2022062221/5681454c550346895db21d6d/html5/thumbnails/32.jpg)
Thank You. !