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

Android Performance Optimisation

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Android Performance Optimisation

This presentation covers practical aspects of performance optimisations for Android apps, including UI and Layouts performance, batching and caching, memory performance, battery optimisations.

Avatar for Sergii Kozyrev

Sergii Kozyrev

May 30, 2018
Tweet

More Decks by Sergii Kozyrev

Other Decks in Programming

Transcript

  1. www.web-academy.com.ua Agenda • UI –UI performance issues and tuning –GPU

    overdraw localisation –Solving with layout changes –Solving with canvas api • Computational speed and performance –Finding bottlenecks - using Traceview and systrace –Ways to solve speed performance issue • Memory issues –Identifying memory issues - memory monitor, heap viewer, allocation tracker –Few hints to minimize number of objects • Battery issues –Battery drain problems with battery historian –How to save some battery
  2. Android UI - how it works • ui update +

    draw() -> every 16 ms (60 FPS) • this is due to 1000ms/60Hz = 16.6 ms per frame • all drawing logic in 16 ms frame - if You miss - “dropped frame :( ” - leads to non smooth animation
  3. www.web-academy.com.ua Small issue -> big problem • Multiple dropped frames

    could lead to much bigger problems • Especially in collection views such as list view
  4. www.web-academy.com.ua Rendering flow • CPU • measure • layout •

    record • execute • GPU • Rasterization Layouts and Invalidations Overdraw
  5. www.web-academy.com.ua Rasterization • High level object to pixels • CPU

    responsible to feed polygons and textures to GPU (Open GLES process) • Preparing on CPU and uploading to GPU - both are not fastest processes • Android does a lot to optimise - example is loading of all resources as one texture to the GPU
  6. www.web-academy.com.ua Overdraw • “Repainting of wall” - wasting Your time

    :) • How many times a pixel on the screen has been redrawn in a single frame • Example - deck of cards widget • Turn overdraw debug in settings!
  7. www.web-academy.com.ua Two main ways to go to GOOD side •

    First - remove unneeded backgrounds from XML • Mark areas of the screen that overlaps manually - use API
  8. www.web-academy.com.ua Layouts and invalidation • DL - Display List contains

    • UI Object -> Create DL -> Execute DL • If view unchanged - re-executing same display list • Invalidate -> recreate all DL • change size - measure of view changes -ask each view in hierarchy about new sizes • change position - layout phase traversing the hierarchy
  9. www.web-academy.com.ua Slow function performance • Most of the time depends

    on how You write code • Almost doesn’t depend on hardware
  10. www.web-academy.com.ua Two Flavors • Single slow performance function • “Death

    of a thousand cuts” - each function is a bit slower • Slow performance func - when it runs slower then You expected
  11. www.web-academy.com.ua Batching and Caching • It is all about caching

    in computer (inside and outside) • CPU-CPU(cache) <-> RAM <-> Hard Drive • Laptop <-> local data center <-> photo of a cat in SF
  12. www.web-academy.com.ua Batching and Caching • Fast algorithms already in place

    in language • Find lines of code that can be done just once • Find architecture solution that enable caching
  13. www.web-academy.com.ua Blocking UI thread • Network access • File system

    access • Database access • Time consuming computation
  14. www.web-academy.com.ua Performance is not free • Java Vector (LinkedList): •

    push O(1) • pop O(1) • insert O(n) • remove O(n) • search O(n)
  15. www.web-academy.com.ua Other examples • Hashtable > “fastest” • HashMap >

    • LinkedHashMap > • IdentityHashMap > • ConcurrentHashMap > • HashSet “slowest”
  16. www.web-academy.com.ua Understanding memory • GC • Find objects that can’t

    be accessed • Reclaim the resources • Questions: • Heap vs Stack memory? • What GC collects? • How leaks happens?
  17. www.web-academy.com.ua Memory Leaks • It’s not only about OutOfMemoryError •

    It reduces available memory and causes GC run more frequent - which is bad
  18. www.web-academy.com.ua Memory Leaks • Simple - “handler case”, or every

    circular referenced objects • Complex - holding a handle to the ClassLoader objects, load in onCreate and don’t check if classes already loaded (no class unload procedure)
  19. www.web-academy.com.ua Deferred tasks • Some tasks may be deferred: •

    Upload photos • Apply filters • Import data from datasource • Sync media files • Send instant message