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

Android High Performance - Droidcon IT 2017

Android High Performance - Droidcon IT 2017

Presentation for the Android Droidcon IT 2017

Enrique López Mañas

April 08, 2017
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

  2. Ego Slide • Indie Dev • Google Developer Expert •

    @eenriquelopez
  3. How it did start?

  4. Performance "how well a person, machine, etc. does a piece

    of work or an activity" Cambridge
  5. But what is high performance? • Strategies to create efficient

    software Layout Energy and battery Security Multithreading Programming patterns Debugging techniques
  6. Why is important? • User Engagement • Costs • Maintenance

    • Quality
  7. 3 Important Limits 0.1s 1s 10s [Miller 1968; Card et

    al. 1991]:
  8. What this means in Android? ANR

  9. ANR • No response for input event in 5 seconds

    • BroadCast receiver still executing after 10 seconds
  10. How to avoid it? Background operations? Show feedback!

  11. How to avoid it? Splash Screen?

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

  13. How to avoid it? Systrace! TraceView!

  14. Debug DDMS

  15. Networking

  16. Layouts TV: 24fps(NTSC), 25fps(PAL) New cam-coders / slow motion: 48fps+

    Blur can occur! Apps: 60fps! every 16 ms!
  17. Layouts

  18. Layouts

  19. Reusing layouts

  20. Reusing layouts

  21. Reusing layouts

  22. ViewStub ViewStub.inflate() View.setVisibility()

  23. ViewStub

  24. Hierarchy Viewer

  25. Profile GPU Rendering

  26. Profile GPU Rendering • Blue: draw • Purple: prepare •

    Red: process • Orange: execute
  27. Memory “the ability to remember information, experiences, and people" Cambridge

  28. Memory - Enumerations

  29. Memory - Enumerations

  30. Memory - Enumerations

  31. Memory - Enumerations

  32. Memory - Enumerations @IntDef

  33. Memory - Enumerations @IntDef

  34. Memory - Enumerations Summary • Unnecessary allocations • AVOID USING

    THEM • Change as much as possible with static final integer values
  35. Memory - Constants Constants in the code should be static

    and final to take advantage of memory savings and to avoid initialization in Java compiler
  36. Memory - Strings

  37. Memory - Strings

  38. Memory - Strings

  39. 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
  40. Memory Leaks - static fields

  41. Memory Leaks - static fields

  42. Memory Leaks - non-static inner classes

  43. Memory Leaks - WeakReference

  44. Threading “Threading is a method of hair removal originating in

    Asia.” Wikipedia
  45. Threading “is the smallest sequence of programmed instructions that can

    be managed independently by a scheduler” Wikipedia
  46. Threading • AsyncTask • Loader • Threads… Know your war!

  47. 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
  48. Threading - AsyncTaskLoader Use it to fetch data Same features

    as AsyncTask… …plus the Activity life-cycle independency and data caching
  49. Threading - AsyncTaskLoader

  50. Threading - Services • Not threads! • Executed in UI

    Thread by default • Never start a long running operation! • Lifecycle
  51. Threading - IntentService • Particular implementation of a Service •

    Execute sequential operations in background • Don’t need to handle the lifecycle
  52. Networking “process of communication between different terminal nodes to exchange

    data” Me
  53. Networking - best practices Latency Gauging

  54. Networking - best practices Batch connections • Wait, collect and

    send • (Analytics)
  55. Networking - best practices Pre-fetch • Download as much as

    possible • (specially if idle connection)
  56. Networking - best practices Queuing connections

  57. Networking - best practices Caching responses

  58. Networking - best practices Last modified

  59. Networking - best practices Exponential back-off

  60. I want your feedback! http://bit.ly/droidconIT17

  61. #DroidconIt