0
Get off my thread!
Adventures in perceived performance
Ori Peleg
Software Engineer
DroidCon TLV, June, 2014
1 Our app is stuck
2 Background service
3 Jank hunters never sleep
4 Nonblocking frenzy
5 Conclusion
Agenda
Our app is stuck
Slow activity
▪  App was stuck
▪  ~5 seconds on older devices
Slow activity
▪  App was stuck
▪  ~5 seconds on older devices
▪  ANR :-(
▪  Computed items to display
Make it faster?
▪  The app was still stuck!
▪  Turns out we were Main Thread
bandits
▪  Any work done on the main
thread
▪...
Make it seem faster!
▪  The app was still stuck!
▪  Turns out we were Main Thread
bandits
▪  Any work done on the main
thr...
Background service still slow
Complex logic in the background
▪  “Make it a service”
▪  The app is super-slow
▪  “Separate process”
▪  The app isn’t stu...
Back to a single process
▪  App takes 10 seconds to load
▪  ANR :-(
▪  Can the service affect the UI’s
performance?
▪  (ye...
Threads
▪  Many things can happen in
parallel
▪  Main thread stays free
▪  But we create a lot of threads
Thread pools (Executors)
▪  Reuse threads
▪  Grows as needed
Jank hunters never sleep
Jank hunters
▪  We must fight to keep the
main thread free!
▪  Every Activity Service callback
is called in the main thread...
SharedPreferences
▪  In-memory cache
▪  Fast enough for the main
thread
▪  But what about editing?
▪  .commit() flushes to ...
Common patterns
▪  Loading images
▪  Picasso, by Square
▪  UI isn’t ready
▪  Progress indicator
▪  Some of the UI isn’t re...
Common errors
▪  Activity/Fragment have died
while processing
▪  Cancel work in onStop/
onDestroy
▪  Check isDestroyed/isA...
Nonblocking frenzy manageable?
High-Velocity News – the FAST news app
▪  Fetch headlines in topic
▪  Fetch stories before you tap
▪  Simplify Concurrency...
Futures (Guava’s extensions)
Get news feed (blocking)
Get news feed (Future)
Get news stories (parsed)
Get top news story
Display the news story
Alternatives to the callback chain
▪  Futures.transform
▪  RxJava
Conclusion get off my thread!
Simple rules to follow
▪  Be aware what’s running on which thread
▪  Remember what’s slow
▪  Offload asynchronicity to libr...
Ori Peleg
orip@fb.com
Upcoming SlideShare
Loading in...5
×

Get Off My Thread! - keep your UI super-responsive

954

Published on

The importance of keeping the main thread free and ways to do it. We'll cover classic main thread busy pitfalls and classic workaround (IntentService, AsyncTask). We'll also cover useful techniques for getting back to the main thread and Simple synchronization strategies.

As presented in DroidCon Tel Aviv 2014 by:
Ori Peleg, Facebook
http://il.droidcon.com

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
954
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
25
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Get Off My Thread! - keep your UI super-responsive"

  1. 1. Get off my thread! Adventures in perceived performance Ori Peleg Software Engineer DroidCon TLV, June, 2014
  2. 2. 1 Our app is stuck 2 Background service 3 Jank hunters never sleep 4 Nonblocking frenzy 5 Conclusion Agenda
  3. 3. Our app is stuck
  4. 4. Slow activity ▪  App was stuck ▪  ~5 seconds on older devices
  5. 5. Slow activity ▪  App was stuck ▪  ~5 seconds on older devices ▪  ANR :-( ▪  Computed items to display
  6. 6. Make it faster? ▪  The app was still stuck! ▪  Turns out we were Main Thread bandits ▪  Any work done on the main thread ▪  Prevents UI updates ▪  Prevents interaction (touch, back button)
  7. 7. Make it seem faster! ▪  The app was still stuck! ▪  Turns out we were Main Thread bandits ▪  Any work done on the main thread ▪  Prevents UI updates ▪  Prevents interaction (touch, back button) ▪  AsyncTask to the rescue!
  8. 8. Background service still slow
  9. 9. Complex logic in the background ▪  “Make it a service” ▪  The app is super-slow ▪  “Separate process” ▪  The app isn’t stuck, but… ▪  Multi-process adds a lot of complexity
  10. 10. Back to a single process ▪  App takes 10 seconds to load ▪  ANR :-( ▪  Can the service affect the UI’s performance? ▪  (yes) ▪  Many dependent initialization steps ▪  AsyncTask?
  11. 11. Threads ▪  Many things can happen in parallel ▪  Main thread stays free ▪  But we create a lot of threads
  12. 12. Thread pools (Executors) ▪  Reuse threads ▪  Grows as needed
  13. 13. Jank hunters never sleep
  14. 14. Jank hunters ▪  We must fight to keep the main thread free! ▪  Every Activity Service callback is called in the main thread! ▪  Computations – too slow ▪  Network access – too slow ▪  Disk I/O? ▪  Even that is too slow ▪  StrictMode FTW
  15. 15. SharedPreferences ▪  In-memory cache ▪  Fast enough for the main thread ▪  But what about editing? ▪  .commit() flushes to disk (can block) ▪  No guarantees (no fsync) ▪  .apply() is good
  16. 16. Common patterns ▪  Loading images ▪  Picasso, by Square ▪  UI isn’t ready ▪  Progress indicator ▪  Some of the UI isn’t ready ▪  Show later (progress / fade in) ▪  Background processing ▪  IntentService (instead of Service)
  17. 17. Common errors ▪  Activity/Fragment have died while processing ▪  Cancel work in onStop/ onDestroy ▪  Check isDestroyed/isAdded ▪  Thread-sensitive code ▪  ThreadLocal
  18. 18. Nonblocking frenzy manageable?
  19. 19. High-Velocity News – the FAST news app ▪  Fetch headlines in topic ▪  Fetch stories before you tap ▪  Simplify Concurrency with Futures™
  20. 20. Futures (Guava’s extensions)
  21. 21. Get news feed (blocking)
  22. 22. Get news feed (Future)
  23. 23. Get news stories (parsed)
  24. 24. Get top news story
  25. 25. Display the news story
  26. 26. Alternatives to the callback chain ▪  Futures.transform ▪  RxJava
  27. 27. Conclusion get off my thread!
  28. 28. Simple rules to follow ▪  Be aware what’s running on which thread ▪  Remember what’s slow ▪  Offload asynchronicity to libraries ▪  Test on typical (vs high-end) devices ▪  Threads/Executors/Futures FTW
  29. 29. Ori Peleg orip@fb.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×