Upgrade to Pro — share decks privately, control downloads, hide ads and more …

High Performance Android (Big Android BBQ 2015)

mscheel
October 22, 2015

High Performance Android (Big Android BBQ 2015)

Big Android BBQ in Texas October 22, 2015

mscheel

October 22, 2015
Tweet

More Decks by mscheel

Other Decks in Technology

Transcript

  1. @5280mark #perfmark @androidbbq #perfmatters ✤ yeah, you ✤ are you

    on the twitter? ✤ here is some homework ✤ follow @5280mark ✤ tweet about this talk with #perfmark and #perfmatters ✤ you can look for tweet content hints at the bottom of most every slide ✤ thanks Hey You LOOK HERE ON SCREEN FOR TWEET HINTS
  2. @5280mark #perfmark @androidbbq #perfmatters Colt-joke ✤ Let’s kick this off

    with a joke “Ugg, @5280mark very low performance on that joke”
  3. @5280mark #perfmark @androidbbq #perfmatters How do we define performance? ✤

    Test everything, every which way ✤ perfect engineer is the engineer that looks both ways when crossing a one way street ✤ Sphero story - performance isn’t always black and white “a great engineer crosses a one way street and looks both ways”
  4. @5280mark #perfmark @androidbbq #perfmatters Perforamance at the Org. Level ✤

    Incorporate performance into pair programming ✤ Add performance to code review process ✤ Add performance into continuous integration pipelines ✤ Think about user performance, reach performance, engagement performance and operational efficiency performance “a great engineer crosses a one way street and looks both ways”
  5. @5280mark #perfmark @androidbbq #perfmatters Talk Overview ✤ 1. #consistable ✤

    never hear “but it works on my machine” again ✤ 2. select #perfmatters from 100 dev bytes; ✤ 33 hits from 9 hours of content by 50 speakers “wait, @5280mark about to review 9 hours of video content?!”
  6. @5280mark #perfmark @androidbbq #perfmatters Consistable ✤ Consistent + Reliable =

    Consistable ✤ Imagine a world where project code comes with the machine to build it “#consistable … consistent and reliable build systems”
  7. @5280mark #perfmark @androidbbq #perfmatters Reference Implementation ✤ Mac ✤ Vagrant

    (Ubuntu) ✤ Docker ✤ Jenkins “If the top stops spinning am in the Inception?”
  8. @5280mark #perfmark @androidbbq #perfmatters Vagrant ✤ Vagrant is software that

    creates and configures virtual development environments ✤ Vagrantfile is a recipe for your box, a single file that can be checked in and used to recreate the exact same box over and over ✤ vagrantup.com ✤ commands: $ vagrant up $ vagrant ssh $ vagrant halt $ vagrant destroy “Vagrant - awesome tool, bad name though”
  9. @5280mark #perfmark @androidbbq #perfmatters Docker ✤ Initially written in Go

    ✤ Allows you to “Build, Ship, Run” ✤ Containerization ✤ OSX doesn’t support system-level virtualization even though BSD has Jails (similar) so use Ubuntu via Vagrant (alternative would be boot2docker) “Have you seen the whale logo, its tremendous”
  10. @5280mark #perfmark @androidbbq #perfmatters Jenkins ✤ Java based build tool

    (orgins with Hudson and Oracle dispute) ✤ Continuous Integration software ✤ Monitors execution of builds ✤ Makes it easy for anyone to make a build (source into product) “Have I seen Jenkins on TV before?”
  11. @5280mark #perfmark @androidbbq #perfmatters Jenkins ✤ Consistently and repeatedly build

    your project the same way ✤ Great plugin support, can even publish to Google Play Store ✤ Set up slaves as Docker instances, other Pro Tips … “Jenkins plugins make a great tool even more awesome”
  12. @5280mark #perfmark @androidbbq #perfmatters Put them all together ✤ Mac

    is host, has Vagrant inside of it, can see Mac at / vagrant ✤ Docker is inside Vagrant, use host folder to access up: “Super awesome sauce = vagrant + docker + jenkins”
  13. @5280mark #perfmark @androidbbq #perfmatters Consistable ✤ Avoid “Works on my

    machine”, the operational efficiency killer ✤ Vagrant, Docker, Jenkins combine for consistent repeatable build environment, Dev machine same as everywhere else ✤ Incorporate performance measurements? “Works on my machine problems are efficiency killers”
  14. @5280mark #perfmark @androidbbq #perfmatters 100 Days of GoogleDev ✤ 100

    developer videos over 100 days -- the Google Developers channel is here for you beyond I/O!
  15. ✤ Ian Lake ✤ Developer performance - work faster, more

    features, use Android Design Support Library ✤ TextInputLayout lets you handle input errors with less code ✤ CoordinatorLayout is amazing, helps coordinate motions to actions across widgets
  16. ✤ Colt McAnlis ✤ Cache replacement strategy is a 60

    year old Computer Science field ✤ LRU = Least Recently Used ✤ LRU cache helps you figure out which items to get rid of as memory gets low (eg album thumbnails in a gallery) ✤ Best practice: set memory size to a portion of available memory (available by Android call or even better use device profile), 1/8 is a good starting point ✤ Keep calm and profile on
  17. ✤ Magnus Hystten ✤ Topic based subscriptions reduce network usage

    and improve battery life ✤ Reduce GCM messages sent to device ✤ App Invites (performance through downloads!), personal recommendation is better than anonymous corporate messaging
  18. ✤ Low power sensors tell you what the user is

    doing ✤ Walking, driving, biking, running ✤ Ever wonder how Google Now knows where you parked? This is how! ✤ Intent based, better performance than a persistant running service ✤ Activity reporting stops when device is still to save on battery
  19. ✤ Reactive process (consider being proactive) ✤ Static Code Analyzer

    ✤ Correctness, Usability, Security, Accessibility, Performance, Internationalization ✤ Can change specific error levels, maybe change too many views to a warning instead of error because newer devices not an issue
  20. ✤ Maria Moeva ✤ g.co/searchconsole ✤ “before” Google Analytics ✤

    find out how users got to your app ✤ Verify your deep links are working
  21. ✤ 2014: 250 million Android devices around the world at

    a business ✤ BYOD = Bring your own device ✤ Corporate liable = company bought it for you ✤ I.T. Admin can now group purchase licenses for a company, increase your sales performance
  22. ✤ Ian Ni-Lewis ✤ beware drop shadows that have opaque

    and transparent parts ✤ use hasOverlappedRendering to make view draw twice as fast ✤ you can profile app to find out where to make faster, but improvements won’t always make the app faster
  23. [20] New APIs in M for Android M for work

    That’s a lot of “for”’s
  24. ✤ Jessica Hummel ✤ Make I.T. admin performance better ✤

    Read only WiFi configurations ✤ COSU = corporate owned single use (think Kiosk) ✤ Can lock a device to single app with a NFC bump
  25. ✤ Memory churn happens when you allocate a bunch of

    objects, kicking off garbage collection events ✤ Framerate drops, steals battery life, bad! ✤ Drawing libraries are actually wrappers around C++ libraries with destructors/finalizers which are not good for performance ✤ Move allocations out of onDraw, make them static or package scope (even better)
  26. ✤ ANR happens after app stops responding for 5 seconds

    ✤ Usually blocking calls like network ✤ In developer mode can turn on strict mode ✤ Screen border flashes red if a blocking call is made ✤ like a runtime version of LINT ✤ Can code a detection map (what to test for and what penalty to apply) ✤ noteSlowCall
  27. ✤ Use the physical web to solve the ladder problem

    ✤ A user shouldn’t have to climb a ladder to view a QR code on a smoke detector ✤ User performance is an important kind of performance
  28. ✤ Use app backup to improve user performance ✤ Happens

    once a day, only with wifi, power and idle ✤ Uses 25mb portion of Google drive which does not count against user quota
  29. ✤ 70 standard views in Android ✤ Avoid performance pitfalls

    when making custom views ✤ Never draw things that haven’t changed ✤ Never call invalidate() when you don’t have to ✤ don’t use draw methods that are not hardware accelerated (GPU way better than CPU) ✤ don’t make allocations in onDraw ✤ dual cost from init + garbage collection ✤ when a view is animating onDraw is called 60x per second on UI thread, each has 16ms probably only 8 after you remove overhead
  30. ✤ Glenn Kasten ✤ Really long chicken crossing the road

    analogy ✤ If Kernel isn’t timely you will get popcorn (its a sound) ✤ tempting to make larger buffers to fix problems but better to systrace profile and fix root cause in kernel
  31. ✤ Be part of a group when wasting power ✤

    Don’t use exact alarms ✤ SyncAdapter gives you network connectivity checks and automatic retries ✤ Share the cost of the battery
  32. ✤ Dalvik’s Android garbage collector is non compacting ✤ means

    no shuffling of items when something too large for available space comes in, kicks off GC ✤ Bitmaps are largest allocations in app ✤ Use BitmapOptions.config to use different compression types for big wins in memory residency trading off for some CPU usage
  33. ✤ Alex Danilo ✤ Heap Viewer in DDMS ✤ how

    much memory in heap ✤ update heap button starts recording ✤ heap tab has display ✤ click cause GC to force an update ✤ see what types of objects your app has allocated
  34. ✤ Joanna Smith ✤ doesn’t like weird man smells in

    the office ✤ 3 steps: Gather, Insight, Action ✤ Rinse and repeat
  35. ✤ ScriptPNG can help you find sweet spot between compression

    and visual quality ✤ If you don’t need transparency - JPEG ✤ You can separate alpha channel and use 2 JPEGs to take advantage of lossy compression ✤ WebP works on API 17 and above ✤ Data compression helps with data transfer not memory residency, so this video is kind of a filler
  36. ✤ createScaledBitmap() requires image to be loaded and decoded ✤

    inSampleSize is very fast, reads every other pixel for 2, can use with any power of 2 ✤ Perf trick - use both! Get almost right size with inSampleSize then fine tune with createScaledBitmap ✤ Since existing libraries already do this (Glide and Picasso) use these proven libraries for production code
  37. ✤ Better way to use temporarily allocated bitmaps to escape

    performance concerns ✤ Bitmaps represent largest contiguous blocks of memory in your heaps, can lead to contention and GC ✤ Use object pools ✤ use inBitmap property, but all images must be same size ✤ Again, Glide library does this for you and just use library
  38. ✤ You can’t always trust rules on the internet ✤

    Instead profile with tools then adjust ✤ Unit test, espresso, monkeyrunner ✤ focus on problems not prescriptions
  39. ✤ Many maps use large arrays that are sparsely populated

    to avoid collisons ✤ ArrayMap for Android uses two small arrays, 2nd array has location hashes for 1st array ✤ If 100’s of items with frequent access use ArrayMap otherwise HashMap ✤ Optimizing performance is a constant trade off, there is no silver bullet
  40. ✤ Small things can lead to big problems ✤ generic

    collections like java.lang.Integer are not primitives ✤ auto boxing converts from object type to primitive ✤ requires more performance overhead to get underlying value ✤ Allocation Tracker and traceview can find this problem ✤ Colt’s got 99 problems joke
  41. ✤ SparseArrays are like ArrayMaps, using 2 tightly packed arrays

    instead of 1 ✤ Key object is always primitive, not generic ✤ Have to index traverse ✤ Use the right container for the right memory profile
  42. ✤ Most common question Colt gets is about ENUMs ✤

    The previously pictured web page from 2009 caused much confusion ✤ Core platform team on Android does not use ENUMs ✤ @IntDef annotation will optimize for you but … ✤ Long story short, watch out for Gremlins and use ProGuard and any ENUM problems will go away on their own
  43. ✤ developer efficiency and operation efficiency, let you spend more

    time on building stuff instead of packaging it ✤ Containers are built on Linux Kernel and encapsulate your system (file and processor) ✤ Check out Docker and Kubernetes minecraft server blog posts
  44. ✤ Rich Hyndman ✤ API level 23 is for Developer

    Preview 3 and release ✤ Can test doze with ADB commands ✤ $ adb shell dumpsys battery unplug ✤ Can avoid auto backup for apps targeting M and above by using “getnobackup” folder — privacy performance?
  45. ✤ Laurence Moroney ✤ QR codes compact data and are

    high performance storage containers ✤ Laurence not to be confused with this L.M.
  46. ✤ When using fancy features on view layouts you might

    be getting yourself into trouble ✤ Don’t want the layout phase of the rendering pipeline to trigger again ✤ Expensive cascade of layout recalculations ✤ GridLayout is better than RelativeLayout from a performance perspective ✤ SysTrace tool can be used to find problems in measure and layout that might lead to frame rate issues ✤ Hierarchy Viewer can be used to find excessive depth
  47. ✤ Each running app shares resources ✤ Even when in

    background app can keep state information, graphic resources and heap objects ✤ onTrimMemory callback has trim level 1-100, since ICS ✤ allows you to free up memory to avoid getting killed, reactively ✤ to be proactive for low memory situations use isLowRamDevice() (true if under 512MB), can gate access to certain features
  48. @5280mark #perfmark @androidbbq #perfmatters Other Perf Sessions at #BABBQ15 ✤

    2PM today, same room - Android Internal Library Dependency Management with Kelly Shuster ✤ 4PM today, Keynote room - Performant Threading For the Sophisticated BBQr with Colt McAnlis ✤ 9:30AM tomorrow, Keynote room - Android for Java Developers with Chet Haase ✤ 10AM tomorrow, room 1 - Using the NDK Performantly with Daniel Gilpin ✤ 12:30PM tomorrow, Keynote room - Tales of Optimizing for Android Wear with Michael Kwan ✤ 2:30PM tomorrow, Keynote room - Building battery efficient apps with Ran Nachmany ✤ 3PM tomorrow, room 1 - Advanced Android Espresso with Chiu-Ki Chan
  49. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com ✤ airbnb.com/r/mscheel ($25 travel credit from me) #BABBQ15
  50. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com ✤ airbnb.com/r/mscheel ($25 travel credit from me) #BABBQ15
  51. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com ✤ airbnb.com/r/mscheel ($25 travel credit from me) #BABBQ15
  52. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com ✤ airbnb.com/r/mscheel ($25 travel credit from me) #BABBQ15
  53. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com ✤ airbnb.com/r/mscheel ($25 travel credit from me) #BABBQ15
  54. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com #BABBQ15
  55. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com ✤ airbnb.com/r/mscheel ($25 travel credit from me) #BABBQ15
  56. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com ✤ airbnb.com/r/mscheel ($25 travel credit from me) #BABBQ15
  57. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com ✤ airbnb.com/r/mscheel ($25 travel credit from me) #BABBQ15
  58. @5280mark #perfmark @androidbbq #perfmatters Follow up with Mark ✤ @5280mark

    ✤ github.com/mscheel ✤ Open Source contributions to Google Glass and Square ✤ stackoverflow.com/mscheel ✤ speakerdeck.com/mscheel ✤ These slides are available now! ✤ lanyrd.com/mscheel ✤ November 20, 2015: droidcon.fr, Paris, France ✤ December 3, 2015: AnDevCon, Santa Clara, CA (Discount code SCHEEL good for $200 off) ✤ meetup.com/gdg-denver ✤ Visit Denver and speak! You can do it. ✤ www.digitalconstruction.com ✤ www.itriagehealth.com ✤ airbnb.com/c/mscheel ($20 travel credit from me) #BABBQ15