facebook tricks for image handling in android

27

Upload: tyrone-nicholas

Post on 10-Sep-2014

687 views

Category:

Software


1 download

DESCRIPTION

One of the hardest parts of writing an Android application is dealing with images. They take up a lot of memory, and are a leading cause of crashes and slowdowns. This talk goes into the architecture of Android’s image processing libraries and how they have evolved over the years. It presents the different options available in the API for better image handling – bitmap recycling and pooling most prominently. I then go into more detail into work currently underway at Facebook to improve image handling. I discuss some poorly documented features in the Android SDK and NDK that offer keys to faster and safer image handling.

TRANSCRIPT

Page 1: Facebook tricks for image handling in Android
Page 2: Facebook tricks for image handling in Android

Tips and Tricks for handling images in Android

Tyrone NicholasSoftware Engineer8 May 2014

Page 3: Facebook tricks for image handling in Android

Memory is Scarce

▪~ 1/10 device RAM

▪480 x 800 = 1.5 MB

Page 4: Facebook tricks for image handling in Android
Page 5: Facebook tricks for image handling in Android

java.lang.OutOfMemoryError

Page 6: Facebook tricks for image handling in Android

Android’s solutions

Page 7: Facebook tricks for image handling in Android

Android heaps

Dalvik

▪JDK

▪Limited

▪Slow

▪Safe

Other

▪Certain system calls

▪Like native

Native

▪NDK

▪Unlimited

▪Fast

▪Unsafe

Page 8: Facebook tricks for image handling in Android

Put bitmaps in the native heap!

Bitmap.java:

Called from Bitmap.createBitmap()

▪ Eliminated in Android 3.0

Android’s Solution #1

Page 9: Facebook tricks for image handling in Android

Android’s Solution #2 Need memory

?

Look for unpinne

d memory

Free it

Ashmem

• ashmem_create_region

• ashmem_pin_region

• ashmem_unpin_region

Page 10: Facebook tricks for image handling in Android

Android’s Solution #2

Draw Allocate Pin Decod

e

Stop drawing

Unpin

Purgeable bitmaps

Page 11: Facebook tricks for image handling in Android

Android’s Solution #2Howto

Page 12: Facebook tricks for image handling in Android

Android’s “Solution” #2 • Er, wait a minute…

Page 13: Facebook tricks for image handling in Android

Android’s Solution #3Bitmap pooling

▪ When doing a decode, specify an old Bitmap object to re-use

Page 14: Facebook tricks for image handling in Android

Android’s Solution #3Hang on…

Page 15: Facebook tricks for image handling in Android

Our solution

Page 16: Facebook tricks for image handling in Android

From the Android NDK…Screw purging. Just screw it.

Page 17: Facebook tricks for image handling in Android

How to do it (1)

Page 18: Facebook tricks for image handling in Android

How to do it (2)

Page 19: Facebook tricks for image handling in Android

Docs can lie

Page 20: Facebook tricks for image handling in Android

Without a GC…

Page 21: Facebook tricks for image handling in Android

Caveats• When are you ‘done’ with an image?

• Application-specific

• Nontrivial

• Scrolling

• Caches

Page 22: Facebook tricks for image handling in Android

ListViews and GridViews

• Beware of flickering!

Page 23: Facebook tricks for image handling in Android

Other tips

Page 24: Facebook tricks for image handling in Android

An Image Pipeline

RequestMemoryCacheRead

DiskCacheRead

Network

DiskCacheWrite

Decode

MemoryCacheWrite

Render Transform

WebP

BMP

BMP JPG

JPG

JPG

UI thread Non-UI threads

Page 25: Facebook tricks for image handling in Android

Using native memory“Naïve native”

• Can’t use for decodes!

Page 26: Facebook tricks for image handling in Android

▪Devices suck. Servers don’t

▪Consider

Resizing, scaling, and cropping

Page 27: Facebook tricks for image handling in Android

(c) 2009 Facebook, Inc. or its licensors.  "Facebook" is a registered trademark of Facebook, Inc.. All rights reserved. 1.0

[email protected]