tips and tricks to attack memory problem in android programming

18
Tips and Tricks to attack memory problem in Android Programming Phạm Cao Trí – [email protected] Lead Engineer – Zalo team – Zing

Upload: zaloapp

Post on 11-May-2015

2.710 views

Category:

Technology


1 download

TRANSCRIPT

  • 1.Tips and Tricks to attack memory problem in Android Programming Phm Cao Tr [email protected] Lead Engineer Zalo team Zing

2. Agenda 1. Memory problems: Why? 2. Android Bitmaps: A closer look and some tips 3. Memory leaking: Demos, Detection methods and Solutions. 3. 1. Memory problems: Why? 4. 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 5. 2. Android Bitmaps: A closer look and some tips 6. 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 7. 2.1. Android Bitmaps (cont) BitmapFactory.java: decodeResource ->.. -> nativeDecodeAsset BitmapFactory.cpp: nativeDecodeAsset -> doDecode 8. 2.1. Android Bitmaps (cont) Pixel allocation policy lies within Graphics.cpp: 9. 2.1. Android Bitmaps (cont) Graphics.cpp: createBitmap -> Bitmap.java: Bitmap constructor Pre-Honeycomb (< 3.0): Honeycomb and beyond (>= 3.0): 10. 2.1. Android Bitmaps (cont) Pre-Gingerbread GC (< 2.3): Stop all apps 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. 11. 2.2. Bitmap cache Architecture 12. 2.2. Bitmap cache (cont) Small tips: A better version: Honeycomb or later: android:largeHeap 13. 3. Memory leaking: Demos, Detection methods and Solutions 14. 3.1. Garbage collection Basic algorithm: Mark and Sweep Does no compacting Memory leak ? 15. 3.2. Demo memory leak Demo Detection Method: Code Review, Log and Heuristics 16. 3.3. Demo memory leak Detection Method: MAT Solution 17. 3.4. Common ways to leak Context Leak Handler Leak or Inner Class leak Outer Solution