Save 10% off ANY FITC event with discount code 'slideshare'
See our upcoming events at www.fitc.ca
OVERVIEW
As most Android developers know, dealing with the extreme degree of fragmentation in the Android ecosystem is often challenging. Among the more difficult challenges is managing memory usage, as devices that are in the market today can have as little as 13MB of memory. Now imagine the pains that developers go through when faced with the headache of having massive bitmaps eat up memory in a millisecond.
In this presentation, James Halpern will talk about the complexities of image and memory management in Android and walk you through the creation of a successful, powerful and open source image management utility. Come to this presentation to learn about techniques that will help you optimize the performance of your apps. Learn about Android’s memory limitations and the role the garbage collector plays in your app’s performance and complexity. Learn how to communicate android graphics issues to developers, and how good design can create fewer bugs. James will conclude this presentation by briefly walking you through his open sourced image management solution that gracefully handles most of these issues in a simple to use package.
14. Introduction
• 2+ years of Android
• 6 product launches
• 2 Google Play apps
• 3+ Android libraries
• 20+ Android projects impacted by tools
development, and counting
24. Lists that just work
Resources that adapt to
screen size and resolution
Lazy loading that does not
interfere with list items
Optimizations for
ImageView size
Images that are available
before you scroll to them
Smooth, flawless
scrolling, across all devices
Anticipate the user’s next
action
37. Approximating Bitmap Sizes
Bit Depth * Number of Pixels = Approximate Size
Examples of bit depth:
ARGB-8888 4 bytes/pixel
RGB-565 2 bytes/pixel
38. Big Image, Big Problem
What happens if an image is too big?
53. The Density Independent Pixel
px = dp * (dpi / 160)
1dp ~= 1/160th of an inch (but not really)
1dp may be smaller than a pixel on low density
phones!
54. Forgetting Pixels
This is not iPhone!
Don’t think in pixels!
Pixels are different sizes on different phones
68. Summary
• Memory limits
• Scrolling performance
• Multiple densities
• Multiple resolutions
• ImageView references
• Image Variety
• Pixel Formats
• Image Scaling
• Performance
• And on and on…
The problem is not limited
to engineering!
78. Calculating Scaling
• Fit for largest size
– Crop
• Fit for smallest size
– Fit
• Always smaller than view
– Saves memory, drops quality
• Round to closest scaling
– May hurt quality
100. | CAPABILITIES
Stack Traces Tell
White Lies
Problems Tools API Debugging
@james_halpern | james.halpern@xtremelabs.com
101. Let’s start with a Stack Trace!
01-25 05:05:49.877: ERROR/dalvikvm-heap(3896): 6291456-byte external allocation too large for this
process.
01-25 05:05:49.877: ERROR/(3896): VM wont let us allocate 6291456 bytes
01-25 05:05:49.877: ERROR/AndroidRuntime(3896): Uncaught handler: thread main exiting due to
uncaught exception
01-25 05:05:49.917: ERROR/AndroidRuntime(3896): java.lang.OutOfMemoryError: bitmap size
exceeds VM budget
01-25 05:05:49.917: ERROR/AndroidRuntime(3896): at
android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-25 05:05:49.917: ERROR/AndroidRuntime(3896): at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:304)
01-25 05:05:49.917: ERROR/AndroidRuntime(3896): at
android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:149)
01-25 05:05:49.917: ERROR/AndroidRuntime(3896): at
android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:174)
102. Let’s start with a Stack Trace!
• java.lang.OutOfMemoryError: bitmap size
exceeds VM budget
• at:
android.graphics.BitmapFactory.decodeStream
118. Tips for Hunting Leaks
• Reduce image quality significantly!
– Sample Size = 128
• Remove tested, big objects
• Repeat the offending action
• Use the “Leak Suspects Report”
119. DDMS – Threads and Performance
• Debug number of threads running
– Are you creating too many threads?
– Threads lingering beyond their lifetime?
– Threads eating up performance/hanging?
UI Thread -- Are we blocking it?
----- Meeting Notes (2013-05-02 14:18) -----HTC Status -- Challenging16MB of memorySlowWeird screen resolution
----- Meeting Notes (2013-05-02 14:18) -----Four key areas with problemsBugs -- Flickering, wrong view, OOMDesign -- Scaling, aspect ratio
----- Meeting Notes (2013-05-02 14:18) -----Available on github
----- Meeting Notes (2013-05-02 14:18) -----Designers -- How design impacts devDevelopers -- How to solveProduct -- Why it takes devs a whileWalk you through problem to solution
----- Meeting Notes (2013-05-02 14:18) -----Less of a problem on iOSStill a problem on iOSThis solution -- Relevant to iOS
----- Meeting Notes (2013-05-02 14:18) -----Less RAM than Disk!Yet bigger in RAM!
----- Meeting Notes (2013-05-02 14:18) -----Will crash the HTC Status & replenish
----- Meeting Notes (2013-05-02 14:18) -----Same image fits in different views
----- Meeting Notes (2013-05-02 14:18) -----Same view can require different sizes