Memory problems in android programming
-
Upload
aiti-education -
Category
Technology
-
view
674 -
download
0
Transcript of Memory problems in android programming
Agenda
• 1. Memory problems: Why? • 2. Android Bitmaps: A closer look and
some tips• 3. Memory leaking: Demos, Detection
methods and Solutions.
1. MEMORY PROBLEMS: WHY?
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
2. ANDROID BITMAPS:A CLOSER LOOK AND SOME TIPS
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
2.1. Android Bitmaps (con’t)• BitmapFactory.java: decodeResource ->.. ->
nativeDecodeAsset
• BitmapFactory.cpp: nativeDecodeAsset -> doDecode
2.1. Android Bitmaps (con’t)• Pixel allocation policy lies within Graphics.cpp:
2.1. Android Bitmaps (con’t)• Graphics.cpp: createBitmap -> Bitmap.java: Bitmap
constructor• Pre-Honeycomb (< 3.0):
• Honeycomb and beyond (>= 3.0):
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.
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
2.2. Bitmap cache• Architecture
2.2. Bitmap cache (con’t)• Small tips:
– A better version:
• Honeycomb or later: android:largeHeap
3. MEMORY LEAKING: DEMOS, DETECTION METHODS AND SOLUTIONS
3.1. Garbage collection• Basic algorithm: Mark and Sweep
• Does no compacting• Memory leak ?
3.2. Demo memory leak 1• Demo 1• Detection Method: Code Review and
Heuristics• Solution
3.3. Demo memory leak 2• Demo 2• Detection Method: MAT• Solution
3.4. Common ways to leak• Context Leak• Handler Leak or Inner Class leak
Outer
• Solution
Q & A