Memory problems in android programming

19
Memory Problems In Android Programming Trí Phạm – 2013 [email protected]

Transcript of Memory problems in android programming

Page 1: Memory problems in android programming

Memory ProblemsIn Android

Programming

Trí Phạm – [email protected]

Page 2: 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.

Page 3: Memory problems in android programming

1. MEMORY PROBLEMS: WHY?

Page 4: Memory problems in android programming

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

2. ANDROID BITMAPS:A CLOSER LOOK AND SOME TIPS

Page 6: Memory problems in android programming

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

2.1. Android Bitmaps (con’t)• BitmapFactory.java: decodeResource ->.. ->

nativeDecodeAsset

• BitmapFactory.cpp: nativeDecodeAsset -> doDecode

Page 8: Memory problems in android programming

2.1. Android Bitmaps (con’t)• Pixel allocation policy lies within Graphics.cpp:

Page 9: Memory problems in android programming

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

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

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

2.2. Bitmap cache• Architecture

Page 13: Memory problems in android programming

2.2. Bitmap cache (con’t)• Small tips:

– A better version:

• Honeycomb or later: android:largeHeap

Page 14: Memory problems in android programming

3. MEMORY LEAKING: DEMOS, DETECTION METHODS AND SOLUTIONS

Page 15: Memory problems in android programming

3.1. Garbage collection• Basic algorithm: Mark and Sweep

• Does no compacting• Memory leak ?

Page 16: Memory problems in android programming

3.2. Demo memory leak 1• Demo 1• Detection Method: Code Review and

Heuristics• Solution

Page 17: Memory problems in android programming

3.3. Demo memory leak 2• Demo 2• Detection Method: MAT• Solution

Page 18: Memory problems in android programming

3.4. Common ways to leak• Context Leak• Handler Leak or Inner Class leak

Outer

• Solution

Page 19: Memory problems in android programming

Q & A