Memory Problems
In Android Programming
Trí Phạm – 2013
techcaotri@gmail.com
Agenda
• 1. Memory problems: Why?
• 2. Android Bitmaps: A closer look and
some tips
• 3. Memory leaking: Demos, Detection
...
1. MEMORY PROBLEMS:
WHY?
1.1. Memory problem: Why?
• Limited heap size: 16MB, 32MB, 64MB,..
• Usually in decoding or manipulating bitmaps.
Painful ...
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): N...
2.1. Android Bitmaps (con’t)
• BitmapFactory.java: decodeResource ->.. -> nativeDecodeAsset
• BitmapFactory.cpp: nativeDec...
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):
• Ho...
2.1. Android Bitmaps (con’t)
• Pre-Gingerbread GC (< 2.3):
– Stop all app’s threads – not stop all applications
– Full hea...
2.1. Android Bitmaps (con’t)
• Decode bitmaps bugs on Android Gingerbread and
previous versions ( <= 2.3):
– https://code....
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
Upcoming SlideShare
Loading in...5
×

Memory problems in android programming

698

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
698
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
28
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Memory problems in android programming

  1. 1. Memory Problems In Android Programming Trí Phạm – 2013 techcaotri@gmail.com
  2. 2. Agenda • 1. Memory problems: Why? • 2. Android Bitmaps: A closer look and some tips • 3. Memory leaking: Demos, Detection methods and Solutions.
  3. 3. 1. MEMORY PROBLEMS: WHY?
  4. 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. 5. 2. ANDROID BITMAPS: A CLOSER LOOK AND SOME TIPS
  6. 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. 7. 2.1. Android Bitmaps (con’t) • BitmapFactory.java: decodeResource ->.. -> nativeDecodeAsset • BitmapFactory.cpp: nativeDecodeAsset -> doDecode
  8. 8. 2.1. Android Bitmaps (con’t) • Pixel allocation policy lies within Graphics.cpp:
  9. 9. 2.1. Android Bitmaps (con’t) • Graphics.cpp: createBitmap -> Bitmap.java: Bitmap constructor • Pre-Honeycomb (< 3.0): • Honeycomb and beyond (>= 3.0):
  10. 10. 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.
  11. 11. 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
  12. 12. 2.2. Bitmap cache • Architecture
  13. 13. 2.2. Bitmap cache (con’t) • Small tips: – A better version: • Honeycomb or later: android:largeHeap
  14. 14. 3. MEMORY LEAKING: DEMOS, DETECTION METHODS AND SOLUTIONS
  15. 15. 3.1. Garbage collection • Basic algorithm: Mark and Sweep • Does no compacting • Memory leak ?
  16. 16. 3.2. Demo memory leak 1 • Demo 1 • Detection Method: Code Review and Heuristics • Solution
  17. 17. 3.3. Demo memory leak 2 • Demo 2 • Detection Method: MAT • Solution
  18. 18. 3.4. Common ways to leak • Context Leak • Handler Leak or Inner Class leak Outer • Solution
  19. 19. Q & A
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×