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

How to find problem causes and performance improvement

How to find problem causes and performance improvement

Presentation to explain how to find problem causes and performance improvement for Android developers.

224e8a32760b146c3184f7451149efca?s=128

Fukui Atsuko

March 09, 2017
Tweet

Transcript

  1. How to find problem causes and improve performance DroidKaigi 2017.3.10

    Room1 11:50- Fukui Atsuko
  2. About me • ෱Ҫರࢠ / Fukui Atsuko • @muumuumuumuu •

    Retty, Inc. • ͦͷલ͸૊ΈࠐΈܥAndroidͷ͓࢓ࣄ
  3. Agenda • ໨ඪɺલఏ஌ࣝͷ֬ೝ • Case 1: Systrace • Case 2:

    Allocation Tracker • ͓·͚1: Memory Leak • ͓·͚2: ֶशࢿྉ
  4. ຊ೔ͷ໨ඪ

  5. ΋ͬ͞ΓΞϓϦΛͲ͏ʹ͔͢Δͱ͖ʹ ḷΓ͕ͪͳ੒௕Ϩϕϧ • ͦ΋ͦ΋Ͳ͏͍͍͔ͯ͠Θ͔Βͳ͍ (´ʀωʀʆ) • ίʔυ͔ΒΞλϦΛ͚ͭͯ੩తղੳ ( ◠‿◠ )

    • πʔϧΛ׆༻ͯ͠ޮ཰Α͘վળ ٩( 'ω' )و
  6. ΋ͬ͞ΓΞϓϦΛͲ͏ʹ͔͢Δͱ͖ ʹॳ৺ऀͷḷΓ͕ͪͳ੒௕Ϩϕϧ • ͦ΋ͦ΋Ͳ͏͍͍͔ͯ͠Θ͔Βͳ͍ (´ʀωʀʆ) • ίʔυ͔ΒΞλϦΛ͚ͭͯ੩తղੳ ( ◠‿◠ )

    • πʔϧΛ׆༻ͯ͠ޮ཰Α͘վળ ٩( 'ω' )و ͜ͷ͋ͨΓͷਓΛର৅ʹ͓࿩͠͠·͢
  7. ΋ͬ͞ΓΞϓϦΛͲ͏ʹ͔͢Δͱ͖ ʹॳ৺ऀͷḷΓ͕ͪͳ੒௕Ϩϕϧ • ͦ΋ͦ΋Ͳ͏͍͍͔ͯ͠Θ͔Βͳ͍ (´ʀωʀʆ) • ίʔυ͔ΒΞλϦΛ͚ͭͯ੩తղੳ ( ◠‿◠ )

    • πʔϧΛ׆༻ͯ͠ޮ཰Α͘վળ ٩( 'ω' )و ͜ͷϨϕϧ·Ͱ͍͘ͷ͕໨ඪ
  8. Android Performance લఏ஌ࣝ

  9. Androidʹ͓͚ΔPerformance • ҰͭͷApplicationʹMain Thread͸Ұͭ • ฒྻॲཧΛ͔ͨͬͨ͠ΒผThreadʹॲཧΛ෼ ͚Δඞཁ͕͋Δ

  10. Androidʹ͓͚ΔPerformance • Application͝ͱʹAndroidγεςϜͷϦιʔε ʢCPU, ϝϞϦʣͷୣ͍߹͍ • γεςϜ͕֤ApplicationʹϝϞϦΛׂΓ౰ͯΔ • ϝϞϦ্ݶʹୡ͢ΔͱGC͕૸Δ •

    CPU͕࢖ΘΕͯPerformance͕௿Լ
  11. ANR • Application Not Responding (ΞϓϦέʔ γϣϯ͕Ԡ౴͍ͯ͠·ͤΜ) • Input Event͕5ඵҎ্Ԡ౴ͳ͠

    • Broadcast Receiverͷॲཧʹ10ඵҎ্͔͔ Δ
  12. ۩ମతʹ

  13. ͝঺հ͢Δπʔϧ • ͳΔ΂͘ීஈ։ൃऀ͕਌͠ΜͰ͍ΔπʔϧΛ ࢖͏ • Android Studio (v2.2.3)

  14. ίʔυΛ࢖ͬͯઆ໌͠·͢ • Android NͷEaster Egg • ίʔυ͕ެ։͞Ε͍ͯ·͢ • https://android.googlesource.com/ platform/frameworks/base/+/

    android-7.0.0_r1/packages/EasterEgg • ʮpackages/eastereggʯͰάάͬͯ΋ग़·͢
  15. Neko • ͻͨ͢ΒͶ͜ΛूΊΔΞϓϦ • Ͷ͋ͭ͜ΊͷΑ͏ʹڈͬͨΓ͠ͳ͍ • Quick Settingsʹ㕒Λஔ͍͓ͯ͘ͱͶ͕͜૿ ͍͑ͯ͘

  16. ͜Μͳײ͡

  17. ɹ ͕100ඖۙ͘ू·Δͱ… • Ұཡը໘Λ։͖ͳ͕Β㕒Λஔ͘ͱANR • Ұཡը໘ΛεΫϩʔϧ͢Δͱ΋ͨͭ͘

  18. Case 1:
 Ұཡը໘Λ։͖ͳ͕Β 㕒Λஔ͘ͱANR

  19. ANRྫ

  20. ύϑΥʔϚϯεͰࠔͬͨ࣌͸ • Ͳͷॲཧʹ͕͔͔͍࣌ؒͬͯΔ͔ௐ΂Δ • ͕͔͔͍࣌ؒͬͯΔॲཧΛվળ͢Δ

  21. ύϑΥʔϚϯεͰࠔͬͨ࣌͸ • Ͳͷॲཧʹ͕͔͔͍࣌ؒͬͯΔ͔ௐ΂Δ • ͕͔͔͍࣌ؒͬͯΔॲཧΛվળ͢Δ

  22. Systrace • ApplicationͱγεςϜશମͷTrace LogΛऔ ಘͰ͖Δ • ࣌ؒ࣠ͰThread͝ͱʹԿ͕ى͍ͬͯ͜Δ͔Θ ͔Δ • ANRͷݪҼͱͳΔMain

    ThreadΛϒϩοΫ ͍ͯ͠ΔݪҼ͕ݟ͑Δ
  23. Systrace • Device MonitorΛ্ཱͪ͛Δ • Tools > Android > Android

    Device Monitor
  24. Systrace (ଓ͖) • DeviceλϒͰtraceର৅ͷΞϓϦΛબ୒ • ɹɹΛΫϦοΫ • ͍ΖΜͳઃఆΛ֬ೝͯ͠OKΛΫϦοΫ

  25. ݁ՌΛݟͯΈΔ • html͕ੜ੒͞ΕΔ

  26. ͳΜ͔ಡΈࠐ·ΕͯΔ…

  27. Ͳͷลͷίʔυ͕ಈ͍͍ͯΔ͔ʁ • “R.drawable.body” ͰίʔυΛݕࡧ • Ͳ͏΍Β ɹ ɹΛඳը͢Δॲཧ͕ಈ͍͍ͯΔ

  28. ύϑΥʔϚϯεͰࠔͬͨ࣌͸ • Ͳͷॲཧʹ͕͔͔͍࣌ؒͬͯΔ͔ௐ΂Δ • ͕͔͔͍࣌ؒͬͯΔॲཧΛվળ͢Δ

  29. ॲཧΛվળ͠Α͏ • վળҎલʹͦ΋ͦ΋ඞཁͳॲཧͳͷ͔ߟ͑Δ • 㕒Λ͓͘λΠϛϯάͰ ɹ ͷඳըॲཧ͸ ඞཁͳ͍͸ͣ • ෆඞཁͳॲཧ͕ಈ͍ͯίετ্͕͕͍ͬͯ

    Δ
  30. ίʔυͷ௥͍ํ • ɹɹ Λඳը͢ΔτϦΨʔΛϦετΞοϓ • Ұཡը໘ͷonCreate() • Shared Preference ͷมߋ௨஌

  31. ίʔυͷ௥͍ํ • Shared Preferenceͷมߋ௨஌ͷτϦΨʔΛ ߟ͑ͯΈΔ • ɹ ɹͷσʔλͱ㕒ͷ৘ใΛҰͭͷShared Preferenceʹ·ͱΊͯ͠·ͬͨ •

    㕒ͷঢ়ଶ͕มΘͬͨͨΊɹɹɹΛ࠶ඳը
  32. मਖ਼ํ๏ • ΋ͱ΋ͱҰͭͷSharedPreference Τα Ұཡը໘࠶ඳըॲཧ 4IBSFE1SFGFSFODF$IBOHF-JTUFOFS

  33. • σʔλΛ෼཭͠ɺద੾ͳൣғͰlistenerΛઃ ఆ Τα मਖ਼ํ๏ Ұཡը໘࠶ඳըॲཧ 4IBSFE1SFGFSFODF$IBOHF-JTUFOFS

  34. None
  35. Case 2: Ұཡը໘ΛεΫϩʔϧ͢Δͱ ΋ͨͭ͘

  36. ΋͖ͨͭྫ

  37. ύϑΥʔϚϯεͰࠔͬͨ࣌͸ • Ͳͷॲཧʹ͕͔͔͍࣌ؒͬͯΔ͔ௐ΂Δ • ͕͔͔͍࣌ؒͬͯΔॲཧΛվળ͢Δ

  38. Allocation Tracker (DDMS) • Applicationͷmemory allocationΛه࿥ • ه࿥ର৅ • Call

    Stack • ϝϞϦαΠζ
  39. Allocation Tracker (DDMS) • Android Studioىಈ • Android Monitor >

    Monitors >
  40. Allocation Tracker (ଓ͖) • Tracking͕ελʔτ͢ΔͷͰɺը໘Λૢ࡞͠ ͯ΋͔ͨͭͤΔ • Tracking͍ͨ͠ಈ࡞͕ऴྃͨ͠Β΋͏Ұ౓ɹ Λԡͯ͠Trackingऴྃ

  41. ݁ՌΛݟͯΈΔ • .allocϑΝΠϧ͕ੜ੒͞ΕΔ

  42. ݁ՌΛݟͯΈΔ • Stack Trace ΛͨͲΕɺͲ͕͜ॏ͍͔Θ͔Δ • ӈΫϦοΫͰιʔείʔυʹඈ΂·͢ʂ

  43. ΋͖ͨͭͷݪҼ • εΫϩʔϧ͢Δͱ͖ʹɹɹ ͷΞΠίϯΛඳ ը͢Δॲཧ͕ॏ͗͢Δ • ॲཧͷৄࡉΛಡΜͰΈΔ • Bitmapͷੜ੒ɺVector Drawableͷੜ੒Λ

    શͯϝΠϯεϨουͰ࣮ߦ
  44. ύϑΥʔϚϯεͰࠔͬͨ࣌͸ • Ͳͷॲཧʹ͕͔͔͍࣌ؒͬͯΔ͔ௐ΂Δ • ͕͔͔͍࣌ؒͬͯΔॲཧΛվળ͢Δ

  45. ॲཧͷվળ • ॏ͍ͨॲཧΛผεϨουʹҠͯ͠ɺը໘ͷߋ ৽ॲཧͷΈϝΠϯεϨουͰߦ͏Α͏ʹมߋ ͢Δ • ը໘ͷߋ৽ॲཧ͸UIεϨουͰߦ͏ඞཁ͕ ͋ΔͷͰ஫ҙ

  46. None
  47. ͓·͚ Memory Leakʹ͍ͭͯ

  48. Memory Leak • GC (Garbage Collection)ର৅ʹͳΒͳ͍͚ ΕͲɺ࢖ΘΕ͍ͯͳ͍ΦϒδΣΫτ͕ϝϞϦ ώʔϓʹډ࠲Γଓ͚Δ͜ͱ • ղ์͞Εͳ͍ͱ࢖༻ՄೳͳϝϞϦ͕ݮ͍ͬͯ

    ͘ͷͰɺGC͕සൟʹى͜ΓɺύϑΥʔϚϯ ε͕௿Լ͢Δ
  49. Heap Viewer (DDMS) • ಛఆ࣌఺ͷϝϞϦͷঢ়گΛݟΔ͜ͱ͕Ͱ͖Δ

  50. Heap Viewer (DDMS) • Device MonitorΛ্ཱͪ͛Δ • Tools > Android

    > Android Device Monitor
  51. Heap Viewer (DDMS) • Device Tab > ର৅ͷϓϩηεΛબ୒ • ɹΛԡͯ͠

    Cause GCϘλϯԡԼ
  52. Heap Viewer (DDMS) • ೚ҙͷλΠϛϯάͰGCΛىͯ͜͠Heap Size ΍γεςϜʹׂΓ౰ͯΒΕͨϝϞϦΛ֬ೝͰ ͖Δ

  53. Leak Canary • Square͕ࣾΦʔϓϯιʔεͰެ։͍ͯ͠Δ library • https://github.com/square/leakcanary • LeakΛݕ஌ͨ͠ΒϦΞϧλΠϜͰ௨஌ •

    ͲͷΠϯελϯε͕Leak͔ͨ͠Θ͔Δ
  54. ͓·͚ ֶशࢿྉ

  55. Android Developers • ͝ଘ஌ެࣜReference • ͜ͷ͋ͨΓ͕ΦεεϝͰ͢ • Develop > Training

    > Best Practices for Performance • Develop > Android Studio >User Guide > Profile Your App
  56. UDACITY • ΦϯϥΠϯֶशϓϥοτϑΥʔϜ • https://www.udacity.com/ • Google͕ύʔτφʔγοϓΛక݁ • Android Performanceίʔε

    • ಈըͱίʔυͱΫΠζͷ૊Έ߹Θͤ
  57. ॻ੶ • ϝϞϦϦʔΫͷ࿩͕΅Μ΍Γͱ͔͠Θ͔Βͳ ͔ͬͨਓ޲͚ • JavaͰͳͥͭ͘Δͷ͔ ஌͓͖͍ͬͯͨJava ϓϩάϥϛϯάͷجૅ஌ࣝ • ถ࣋

    ޾ण, ೔ܦBPࣾ, 309p
  58. ॻ੶ • AndroidͷThread΍ProcessΛ΋ͬͱ஌Γͨ ͍ਓ޲͚ • Androidͷͳ͔Έ InsideAndroid • Tae Yeon

    Kimଞ, ύʔιφϧϝσΟΞ, 506p
  59. Thank you!