Memory problems in android programming
-
Upload
aiti-education -
Category
Technology
-
view
671 -
download
0
Transcript of Memory problems in android programming
![Page 2: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/2.jpg)
Agenda
• 1. Memory problems: Why? • 2. Android Bitmaps: A closer look and
some tips• 3. Memory leaking: Demos, Detection
methods and Solutions.
![Page 3: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/3.jpg)
1. MEMORY PROBLEMS: WHY?
![Page 4: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/4.jpg)
1.1. Memory problem: Why?• Limited heap size: 16MB, 32MB, 64MB,..• Usually in decoding or manipulating bitmaps.
Painful in solving memory problem for low-end devices
![Page 5: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/5.jpg)
2. ANDROID BITMAPS:A CLOSER LOOK AND SOME TIPS
![Page 6: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/6.jpg)
2.1. Android Bitmaps• Java object• Decode in native via Skia library (SkBitmap)• Allocation:
– Pre Honeycomb (< 3.0): Native heap but “report” size to Dalvik VM.
– Honeycomb and beyond (>= 3.0): VM/Java heap
![Page 7: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/7.jpg)
2.1. Android Bitmaps (con’t)• BitmapFactory.java: decodeResource ->.. ->
nativeDecodeAsset
• BitmapFactory.cpp: nativeDecodeAsset -> doDecode
![Page 8: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/8.jpg)
2.1. Android Bitmaps (con’t)• Pixel allocation policy lies within Graphics.cpp:
![Page 9: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/9.jpg)
2.1. Android Bitmaps (con’t)• Graphics.cpp: createBitmap -> Bitmap.java: Bitmap
constructor• Pre-Honeycomb (< 3.0):
• Honeycomb and beyond (>= 3.0):
![Page 10: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/10.jpg)
2.1. Android Bitmaps (con’t)• Pre-Gingerbread GC (< 2.3):
– Stop all app’s threads – not stop all applications– Full heap collection– Pause times often > 100ms
• Gingerbread and beyond (>= 2.3):– Mostly concurrent– Partial collections– Pause times usually < 5ms
• Garbage collection in Android does no compacting.
![Page 11: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/11.jpg)
2.1. Android Bitmaps (con’t)• Decode bitmaps bugs on Android Gingerbread
and previous versions ( <= 2.3):– https://code.google.com/p/android/issues/detail
?id=8488– If the native Bitmap allocation size plus the
current HeapSize (NOT the actually allocated size) exceeds the limits, native Bitmap allocation will always fail
![Page 12: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/12.jpg)
2.2. Bitmap cache• Architecture
![Page 13: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/13.jpg)
2.2. Bitmap cache (con’t)• Small tips:
– A better version:
• Honeycomb or later: android:largeHeap
![Page 14: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/14.jpg)
3. MEMORY LEAKING: DEMOS, DETECTION METHODS AND SOLUTIONS
![Page 15: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/15.jpg)
3.1. Garbage collection• Basic algorithm: Mark and Sweep
• Does no compacting• Memory leak ?
![Page 16: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/16.jpg)
3.2. Demo memory leak 1• Demo 1• Detection Method: Code Review and
Heuristics• Solution
![Page 17: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/17.jpg)
3.3. Demo memory leak 2• Demo 2• Detection Method: MAT• Solution
![Page 18: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/18.jpg)
3.4. Common ways to leak• Context Leak• Handler Leak or Inner Class leak
Outer
• Solution
![Page 19: Memory problems in android programming](https://reader035.fdocuments.in/reader035/viewer/2022081507/554d27a6b4c905ca208b4ffd/html5/thumbnails/19.jpg)
Q & A