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

Presentation for the Mobicode - Minsk 2016

Presentation for the Mobicode - Minsk 2016

Presentation for the Mobicode - Minsk December 2016

Enrique López Mañas

December 05, 2016
Tweet

More Decks by Enrique López Mañas

Other Decks in Programming

Transcript

  1. Android High Performance
    Enrique López Mañas
    Google Developer Expert & IT Consultor

    View full-size slide

  2. How it did start?

    View full-size slide

  3. Performance
    "how well a person,
    machine, etc. does a
    piece of work or an
    activity"
    Cambridge

    View full-size slide

  4. But what is high performance?
    • Strategies to create efficient software
    Layout
    Energy and battery
    Security
    Multithreading Programming
    patterns
    Debugging techniques

    View full-size slide

  5. Why is important?
    • User Engagement
    • Costs
    • Maintenance
    • Quality

    View full-size slide

  6. 3 Important Limits
    0.1s 1s 10s
    [Miller 1968; Card et al. 1991]:

    View full-size slide

  7. What this means in Android?
    ANR

    View full-size slide

  8. ANR
    • No response for input event in 5 seconds
    • BroadCast receiver still executing after 10 seconds

    View full-size slide

  9. How to avoid it?
    Background operations? Show feedback!

    View full-size slide

  10. How to avoid it?
    Splash Screen?

    View full-size slide

  11. How to avoid it?
    Calculations? Worker thread!

    View full-size slide

  12. How to avoid it?
    Systrace! TraceView!

    View full-size slide

  13. Layouts
    TV: 24fps(NTSC), 25fps(PAL)
    New cam-coders / slow motion: 48fps+
    Blur can occur!
    Apps: 60fps!
    every 16 ms!

    View full-size slide

  14. Reusing layouts

    View full-size slide

  15. Reusing layouts

    View full-size slide

  16. Reusing layouts

    View full-size slide

  17. ViewStub
    ViewStub.inflate() View.setVisibility()

    View full-size slide

  18. Hierarchy Viewer

    View full-size slide

  19. Profile GPU Rendering

    View full-size slide

  20. Profile GPU Rendering
    • Blue: draw
    • Purple: prepare
    • Red: process
    • Orange: execute

    View full-size slide

  21. Memory
    “the ability to
    remember information,
    experiences, and
    people"
    Cambridge

    View full-size slide

  22. Memory - Enumerations

    View full-size slide

  23. Memory - Enumerations

    View full-size slide

  24. Memory - Enumerations

    View full-size slide

  25. Memory - Enumerations

    View full-size slide

  26. Memory - Enumerations @IntDef

    View full-size slide

  27. Memory - Enumerations @IntDef

    View full-size slide

  28. Memory - Enumerations Summary
    • Unnecessary allocations
    • AVOID USING THEM
    • Change as much as possible with static final
    integer values

    View full-size slide

  29. Memory - Constants
    Constants in the code should be static and final to take advantage
    of memory savings and to avoid
    initialization in Java compiler

    View full-size slide

  30. Memory - Strings

    View full-size slide

  31. Memory - Strings

    View full-size slide

  32. Memory - Strings

    View full-size slide

  33. Memory Leaks
    “a memory leak is a type of
    resource leak that occurs
    when a computer program
    incorrectly manages
    memory allocations in such
    a way that memory which
    is no longer needed is not
    released.” Wikipedia

    View full-size slide

  34. Memory Leaks - static fields

    View full-size slide

  35. Memory Leaks - static fields

    View full-size slide

  36. Memory Leaks - non-static inner classes

    View full-size slide

  37. Memory Leaks - WeakReference

    View full-size slide

  38. Threading
    “Threading is a method of
    hair removal originating in
    Asia.”
    Wikipedia

    View full-size slide

  39. Threading
    “is the smallest sequence
    of programmed instructions
    that can be managed
    independently by a
    scheduler”
    Wikipedia

    View full-size slide

  40. Threading
    • AsyncTask
    • Loader
    • Threads…
    Know your war!

    View full-size slide

  41. Threading - AsyncTask
    Communicate Worker
    Thread with UI
    Does not need to
    communicate UI? Does not
    need to notify user? DOES
    NOT NEED ASYNCTASK!
    If the AsyncTask has all
    parameters Void, you are
    only implementing
    doInBackground() and you
    do not need an AsyncTask

    View full-size slide

  42. Threading - AsyncTaskLoader
    Use it to fetch data
    Same features as
    AsyncTask…
    …plus the Activity life-cycle
    independency and data
    caching

    View full-size slide

  43. Threading - AsyncTaskLoader

    View full-size slide

  44. Threading - Services
    • Not threads!
    • Executed in UI Thread by default
    • Never start a long running operation!
    • Lifecycle

    View full-size slide

  45. Threading - IntentService
    • Particular implementation of a Service
    • Execute sequential operations in background
    • Don’t need to handle the lifecycle

    View full-size slide

  46. Networking
    “process of communication
    between different terminal
    nodes to exchange data”
    Me

    View full-size slide

  47. Networking - best practices
    Latency Gauging

    View full-size slide

  48. Networking - best practices
    Batch connections
    • Wait, collect and send
    • (Analytics)

    View full-size slide

  49. Networking - best practices
    Pre-fetch
    • Download as much as possible
    • (specially if idle connection)

    View full-size slide

  50. Networking - best practices
    Queuing connections

    View full-size slide

  51. Networking - best practices
    Caching responses

    View full-size slide

  52. Networking - best practices
    Last modified

    View full-size slide

  53. Networking - best practices
    Exponential back-off

    View full-size slide

  54. I want your feedback!
    http://bit.ly/feedbackMobicode
    @eenriquelopez

    View full-size slide