Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android Performance Tips

Tips to improve Android performance: serializable, view holder pattern, asyntask, threadpool, ...

  • Be the first to comment

Android Performance Tips

  1. 1. Android performance Huỳnh Quang Thảo Silicon Straits Saigon Google Developer Expert on Android
  2. 2. 10 tips to become
  3. 3. 10 tips to become Small
  4. 4. 10 tips to become Small Easy to apply
  5. 5. 10 tips to become Small Easy to apply become …
  6. 6. Tip 1: Avoid Serialization • Pass data between androic components • Serialize data before persisting to disk/network …
  7. 7. Tip 1: implement parcelable Serialization is slow because using java reKlection Parcelable is faster with little effort
  8. 8. Tip 1: implement parcelable Serialization is slow because using java reKlection Parcelable is faster with little effort Parcelable is extremely faster with some effort
  9. 9. Tip 1: Parcelable generator generate again when changing Kields
  10. 10. Tip 1: Other libraries Google Protobuf Google Flatbuffer
  11. 11. Tip 2: Avoid internal getter/setter •Without JIT, direct Kield access is 3x faster than getter/setter •With JIT, direct Kield access is 7x faster than getter/setter
  12. 12. Tip 2: Avoid internal getter/setter •Without JIT, direct Kield access is 3x faster than getter/setter •With JIT, direct Kield access is 7x faster than getter/setter
  13. 13. Tip 3: Avoid using enum - Code size overhead: One enum requires 13x memory more than int version. - Runtime overhead: - Cost 12-16 bytes for one enum. - Cost about 20+ bytes for each object in one enum. Reference: https://www.youtube.com/watch?v=Hzs6OBcvNQE
  14. 14. Tip 3: Using @IntDef
  15. 15. Tip 4: View Holder Pattern
  16. 16. Tip 4: Using RecycleView
  17. 17. Tip 4: Using RecycleView
  18. 18. Tip 4: Using RecycleView
  19. 19. Tip 5: Carefully Dependency Injection •Guice/ Roboguice: run-time dependency injection framework. •Dagger: Compile-time dependency injection framework.
  20. 20. Tip 5: Carefully Dependency Injection •Guice/ Roboguice: run-time dependency injection framework.
  21. 21. Tip 6: Prefer for-index for index
  22. 22. Tip 6: Prefer for-index for index for iterator
  23. 23. Tip 6: Prefer for-index for index for iterator for simple
  24. 24. Tip 6: Prefer for-index for index ArrayList 2603 for index Vector 4664 for Simple ArrayList 5133 iterator ArrayList 5142 Iterator vector 11778 for simple vector 11783 •Davik 4.4.4 •list contains 400,000 random integers •list is walked through 10 times •Min and max time are thrown out. get average the rest
  25. 25. Tip 7: Aware Autoboxing boolean 8 bits int 32 bits Kloat 32 bits long 64 bits boolean java.lang.Boolean int java.lang.Integer Kloat java.lang.Float long java.lang.Long
  26. 26. Tip 7: Aware Autoboxing
  27. 27. Tip 7: Aware Autoboxing
  28. 28. Tip 7: Aware Autoboxing
  29. 29. Tip 7: Aware Autoboxing
  30. 30. Tip 7: Aware Autoboxing
  31. 31. Tip 7: Aware Autoboxing 84 new objects (16x84)
  32. 32. HashMap <obj, obj> SpareBoolMap <bool, obj> SpareIntMap <int, obj> SpareLongMap <long, obj>
  33. 33. Tip 8: Using Strict Mode
  34. 34. Tip 9: Threading model
  35. 35. Tip 9: HandlerThread
  36. 36. Tip 9: Asynctask
  37. 37. Tip 9: Asynctask •get off UI-work
  38. 38. Tip 9: Asynctask •get off UI-work •execute serial
  39. 39. Tip 9: Threadpool
  40. 40. Tip 9: Threadpool
  41. 41. Tip 9: IntentService • is service
  42. 42. Tip 9: IntentService • is service • all tasks will put into queue
  43. 43. Tip 9: IntentService • is service • all tasks will put into queue • don’t need to implement looper, handler …
  44. 44. Tip 9: IntentService • is service • all tasks will put into queue • don’t need to implement looper, handler … • thread-safe
  45. 45. Tip 10: Control memory heap size
  46. 46. Tip 10: Control memory heap size Android VM limits memory allocation for each application
  47. 47. Tip 10: Control memory heap size Android VM limits memory allocation for each application Android VM limits memory allocation for each process
  48. 48. Java Heap • The Java Heap is managed by Garbage Collector
  49. 49. Java Heap • Increase memory allocation by creating different process
  50. 50. Java Heap • Increase memory allocation by allowing large heap allocation
  51. 51. Native Heap • Native heap is managed by programmer. • Native heap is allocated and deallocated manually
  52. 52. Ashmem • Ashmem: Android shared memory • Allow processes share memory and can be reclaimed automatically in future
  53. 53. Does it matter • Performance dept.
  54. 54. Does it matter • Performance dept. • keep code clean is better
  55. 55. Does it matter • Performance dept. • keep code clean is better • beware micro optimization. must proKile code
  56. 56. ProKile code • Allways proKile code for checking performance
  57. 57. GPU Renderer
  58. 58. Systrace
  59. 59. Memory viewer
  60. 60. Thanks for listening

    Be the first to comment

  • sdhar27

    Jun. 25, 2020

Tips to improve Android performance: serializable, view holder pattern, asyntask, threadpool, ...

Views

Total views

70

On Slideshare

0

From embeds

0

Number of embeds

0

Actions

Downloads

2

Shares

0

Comments

0

Likes

1

×