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

メモリリークに関するウワサの今昔

 メモリリークに関するウワサの今昔

動作確認のサンプルコードあります https://github.com/woshidan/MemoryLeakTest

懇親会や下記の記事にていただいたご指摘を元に、
Viewに渡すContextの部分をApplicationContextからActivityContextに訂正させていただきました。
非常に丁寧にまとめられていてとても勉強になりました。

http://ytrino.hatenablog.com/entries/2016/05/26

ありがとうございます。

woshidan

May 25, 2016
Tweet

More Decks by woshidan

Other Decks in Programming

Transcript

  1. GMO Pepabo, Inc.
    Woshidan
    2016/05/25 potatotips #29
    ϝϞϦϦʔΫʹؔ͢
    Δ΢ϫαͷࠓੲ

    View full-size slide

  2. ࣗݾ঺հ
    !XPTIJEBO
    (.0ϖύϘ
    CMPH
    IUUQXPTIJEBOIBUFOBCMPHDPN
    "OESPJE׳Ε͖͔ͯͨ΋

    View full-size slide

  3. ϝϞϦϦʔΫରࡦͰ
    ͙͙ͬͯग़ͯ͘Δ͜ͱ

    View full-size slide

  4. ϝϞϦϦʔΫରࡦͰ͙͙ͬͯग़ͯ͘Δ͜ͱ
    7JFXͷίʔυ্ͰҾ਺ͷ$POUFYU͸Մೳͳ
    ݶΓ"QQMJDBUJPO$POUFYU<>
    ಺෦Ϋϥε͸֎෦ΫϥεͷϓϩύςΟ͕ෆཁ
    ͳΒՄೳͳݶΓTUBUJDʹ͢Δ<>
    3FDZDMFS7JFXͷ"EBQUFSΛ֎͢<>
    #JUNBQͷϝϞϦΛ໌ࣔతʹղ์<>
    ࢖Θͳ͘ͳͬͨΠϕϯτϦεφΛղ์͢Δ<>

    View full-size slide

  5. ͍·΋ຊ౰ʹඞཁ

    View full-size slide

  6. -FBL$BOBSZ
    <><>
    Ͱࢼͯ͠Έͨ

    View full-size slide

  7. ϝϞϦϦʔΫରࡦͰ͙͙ͬͯग़ͯ͘Δ͜ͱ
    7JFXͷίʔυ্ͰҾ਺ͷ$POUFYU͸Մೳͳ
    ݶΓ"QQMJDBUJPO$POUFYU<>
    ಺෦Ϋϥε͸֎෦ΫϥεͷϓϩύςΟ͕ෆཁ
    ͳΒՄೳͳݶΓTUBUJDʹ͢Δ<>
    3FDZDMFS7JFXͷ"EBQUFSΛ֎͢<>
    #JUNBQͷϝϞϦΛ໌ࣔతʹղ์<>
    ࢖Θͳ͘ͳͬͨΠϕϯτϦεφΛղ์͢Δ<>

    View full-size slide

  8. ࣮ݧ3FDZDMFS7JFX
    "DUJWJUZPO$SFBUF
    SFDZDMFS7JFX 3FDZDMFS7JFX
    pOE7JFX#Z*E 3JESFDZDMFS7JFX

    Θ͟ͱ"DUJWJUZ$POUFYUΛ౉͍ͯ͠Δ͕ɺ͘͝كʹ͔͠ϦʔΫ͠ͳ͔ͬͨ
    SFDZDMFS7JFXTFU-BZPVU.BOBHFS OFX-JOFBS-BZPVU.BOBHFS UIJT


    SFDZDMFS7JFXTFU"EBQUFS OFX$VTUPN"EBQUFS UIJT
    OFX"SSBZ-JTU*OUFHFS "SSBZTBT-JTU





    "DUJWJUZPO%FTUSPZ
    !0WFSSJEF
    QVCMJDWPJEPO%FTUSPZ
    \
    Կ΋͠ͳ͍
    TVQFSPO%FTUSPZ

    ^

    View full-size slide

  9. ࣮ݧ3FDZDMFS7JFX
    QSJWBUFDMBTT$VTUPN"EBQUFSFYUFOET
    3FDZDMFS7JFX"EBQUFS$VTUPN"EBQUFS7JFX)PMEFS\
    $POUFYUN$POUFYU
    "SSBZ-JTU*OUFHFSN%BUB
    QVCMJD$VTUPN"EBQUFS $POUFYUDPOUFYU "SSBZ-JTU*OUFHFSEBUB
    \
    N$POUFYUDPOUFYU
    N%BUBEBUB
    ^

    !0WFSSJEF
    QVCMJDWPJEPO%FUBDIFE'SPN3FDZDMFS7JFX 3FDZDMFS7JFX
    SFDZDMFS7JFX
    \
    N$POUFYUOVMM
    TVQFSPO%FUBDIFE'SPN3FDZDMFS7JFX SFDZDMFS7JFX

    ^

    View full-size slide

  10. ݁Ռ3FDZDMFS7JFX
    ΄ͱΜͲϝϞϦϦʔΫͷݕग़͕֬ೝ͞Εͳ
    ͔ͬͨ
    ࣮ݧ༻"DUJWJUZΛԿઍճ΋࡞ͬͯফͯ͠ճసͤͨ͞
    ͱ͜Ζɺճఔ౓ݕग़͞Εͨ΍Γ͗͢Ͱޡݕ

    ࠷ॳ͸͜ͷลΛ͓୊ʹ͠Α͏ͱ͍ͯͨ͠ͷͰࣦ᪨͠
    Α͏͔ͱࢥͬͨ
    جຊతʹ"DUJWJUZ$POUFYUͰΑ͍Ͱ͢
    <><>

    View full-size slide

  11. ϝϞϦϦʔΫରࡦͰ͙͙ͬͯग़ͯ͘Δ͜ͱ
    7JFXͷίʔυ্ͰҾ਺ͷ$POUFYU͸Մೳͳ
    ݶΓ"QQMJDBUJPO$POUFYU<>
    ಺෦Ϋϥε͸֎෦ΫϥεͷϓϩύςΟ͕ෆཁ
    ͳΒՄೳͳݶΓTUBUJDʹ͢Δ<>
    3FDZDMFS7JFXͷ"EBQUFSΛ֎͢<>
    #JUNBQͷϝϞϦΛ໌ࣔతʹղ์<>
    ࢖Θͳ͘ͳͬͨΠϕϯτϦεφΛղ์͢Δ<>

    View full-size slide

  12. ࣮ݧ'"#ͷΫϦοΫϦεφ
    "DUJWJUZPO$SFBUF
    GBCTFU0O$MJDL-JTUFOFS OFX$VTUPN$MJDL-JTUFOFS


    "DUJWJUZPO%FTUSPZͰ͸Կ΋͠ͳ͍
    DMBTT$VTUPN$MJDL-JTUFOFSJNQMFNFOUT7JFX0O$MJDL-JTUFOFS\
    !0WFSSJEF
    QVCMJDWPJEPO$MJDL 7JFXW
    \
    PQFO3FDZDMFS7JFX W

    ^
    ^

    View full-size slide

  13. ϦʔΫ͕֬ೝͰ͖ͳ
    ͔ͬͨ

    View full-size slide

  14. ࣮ݧ ͰϝϞϦϦʔΫ͠ͳ͍ཧ༝
    "OESPJEͷHBSCBHFDPMMFDUJPO͸ϚʔΫΞ
    ϯυεΠʔϓํࣜ<>
    ϧʔτͱͳΔΦϒδΣΫτ͔Β࠷୹ͷด࿏Λ࡞ΔΑ͏ʹ
    ΦϒδΣΫτಉ࢜ͷࢀরπϦʔΛ࡞੒<>
    ϧʔτͱͳΔͷ͸"DUJWJUZ 4FSWJDF ΫϥεΦϒδΣΫτͳͲʜ<>
    ੜଘ͍ͯ͠ΔϧʔτΦϒδΣΫτͱͦͷࢠΛϚʔΫͯ͠
    ͍͘<><>
    ϚʔΫ͞Εͳ͔ͬͨΦϒδΣΫτΛ($Ͱղ์͢Δ<>
    ࢠͷཁૉ͔ΒḷΕΔ͍ͣΕ͔ͷϧʔτͷΦϒδΣΫτ͕ੜ
    ଘ͍ͯͨ͠ΒɺੜଘʹϚʔΫ͞Εͯ($͞Εͳ͍<><>

    View full-size slide

  15. ࣮ݧ ͰϝϞϦϦʔΫ͠ͳ͍ཧ༝
    ٯʹɺϧʔτΦϒδΣΫτʹ౰ͨΔ΋ͷ͕
    ͢΂ͯੜଘ͍ͯ͠ͳ͚Ε͹($͞ΕΔ
    ΠϕϯτϦεφ΍3FDZDMFS7JFX "EBQUFS
    ͷ৔߹ͷϧʔτΦϒδΣΫτ͸ɺ"DUJWJUZ
    ଞʹαʔϏεͳͲ͔Βࢀর͍ͯ͠ͳ͔ͬͨΒ"DUJWJUZ
    ͕ੜଘͱͯ͠ϚʔΫ͞Εͳ͔ͬͨΒ͜ΕΒ΋ϚʔΫ͞
    Εͳ͍
    ($͞Ε͓͔ͯ͘͠ͳ͍ɺϦʔΫ͠ͳͯ͘౰વ

    View full-size slide

  16. ͳͥϝϞϦϦʔΫ͠ͳ͍ͷʹ
    ͜ΕΒͷ͜ͱ͕ϝϞϦϦʔΫͷ࿩Ͱ
    ݕࡧ͢Δͱग़ͯ͘Δͷ͔

    View full-size slide

  17. ੲ͸ϝϞϦϦʔΫ͠ͳͯ͘΋
    ࡉ͔͘ϝϞϦΛ؅ཧ͔ͨͬͨ͠

    View full-size slide

  18. "OESPJEΑΓલ d೥

    ($ʹؔ͢Δ͢΂ͯͷ޻ఔͰΞϓϦͷ͢
    ΂ͯͷεϨου͕ࢭ·ͬͨ<>
    ΦϒδΣΫτͷϚʔΫͱεΠʔϓʹ࣌ؒ
    ͕͔͔Γɺ($ͷఀࢭ࣌ؒ͸ඵΛ௒
    ͑Δ͜ͱ͕͟Βͩͬͨ<>
    ͜·ΊʹࢀরΛ੾͓ͬͯ͘͜ͱͰɺੜଘ͍ͯ͠
    ΔΦϒδΣΫτ͔ΒͷϚʔΫΛૣ͔ͨͬͨ͘͠

    View full-size slide

  19. "OESPJEҎ߱ ೥d

    ($ͷओཁͳ޻ఔ͕ΞϓϦͷओཁͳεϨο
    υͷॲཧͱฒߦͯ͠ߦ͑ΔΑ͏ʹͳͬͨ
    <>
    ΦϒδΣΫτͷϚʔΫͱεΠʔϓʹϚϧ
    νίΞ$16ͷԸܙ͕ड͚ΒΕΔΑ͏ʹ
    ͳΓɺఀࢭ͕࣌ؒ୹͘ͳͬͨ<>
    ࡉ͔͍ϝϞϦ؅ཧΑΓ($Ͱճऩ͞Εͳ͍ϝϞϦ
    ϦʔΫʹؔ৺͕Ҡͬͨ

    View full-size slide

  20. "DUJWJUZ͕ϦʔΫ͠ʹ͘͘ͳͬͯΔ
    XJEHFUΫϥεͳͲͷϝϞϦϦʔΫ͸͠
    ͹͠͹"OESPJE։ൃऀͷ(PPHMFHSPVQ
    ͷ.-ͳͲʹใࠂ͞Ε͍ͯΔ<><>
    %SBXBCMF͕͍࣋ͬͯͨ$BMMCBDLͷΠϯελϯ
    ε͕"OESPJEҎ߱Ͱऑࢀরʹมߋ͞ΕΔ
    ͳͲमਖ਼͞Ε͍ͯΔ΋ͷ΋͋Δ<>
    ੲ͸ϦʔΫ͚ͨ͠Ͳɺ͍·͸ϦʔΫ͠ͳ
    ͍ه๏΋͋Δ<>

    View full-size slide

  21. 6*ͷΠϕϯτϦεφͷղ์
    ʹ͍ͭͯ͸ݱࡏ͸Ҏલ΄Ͳؾ
    ʹ͠ͳͯ͘΋ྑͦ͞͏

    View full-size slide

  22. ଞͷ࿩΋ͦ͜·Ͱؾʹ͠
    ͳ͍͍ͯ͘

    View full-size slide

  23. ϧʔτΦϒδΣΫτ͕
    "DUJWJUZͱҧ͏ͷ͸
    ૬มΘΒͣ஫ҙ͕ඞཁ

    View full-size slide

  24. #JUNBQ
    /BUJWFͷϝϞϦ؅ཧΛ͍ͯ͠Δ෦෼
    ͕͋Γ<><>ɺ*NBHF7JFXͷଐੑͳͲ
    Ͱͳ͘ίʔυͰ؅ཧ͢Δ৔߹͸ެࣜͷ
    υΩϡϝϯτ<>ʹैͬͯ஫ҙ͢Δ
    ࣗ৴͕ͳ͚Ε͹1JDBTTP<> (MJEF<>
    ͳͲͷϥΠϒϥϦΛ௨ͯ͠ѻ͏

    View full-size slide

  25. ଞʹ͸௕࣌ؒͷ)BOEMFS
    <>
    ΍"TZOD5BTL
    <>
    ͳͲ

    View full-size slide

  26. "TZOD5BTL
    ඇTUBUJDͳ಺෦Ϋϥεʹ͢Δͱݺͼग़͠ݩ
    "DUJWJUZ΁ͷࢀরΛ͍࣋ͬͯΔͷͰɺ࣮૷
    ͢Δͱ͖͸TUBUJDʹ͢Δ<>
    ݺͼग़͠ݩ"DUJWJUZʹͭͳ͕Δࢀর͸ऑࢀ
    রͰ<><>
    ݺͼग़͠ݩ"DUJWJUZͷPO%FTUSPZͰ
    "TZOD5BTLDBODFM USVF
    <>
    ͜ͷลɺ3Y+BWB<>ʹஔ׵͢Δͱ؆୯ʹॻ͚Δ

    View full-size slide

  27. ϝϞϦϦʔΫʹؔ͢Δ΢ϫαͷࠓੲ
    "DUJWJUZΛϧʔτΦϒδΣΫτͱ͢Δ6*
    ͷΠϕϯτϦεφͷղ์ʹ͍ͭͯ͸ݱࡏ
    ͸ͦ͜·Ͱਆܦ࣭ʹͳΒͳ͍͍ͯ͘
    XJOEPX5PLFOͷऔಘ΍5IFNFͷҾ͖ܧ͗ͳ
    ͲͷͨΊɺجຊతʹ"DUJWJUZ$POUFYUΛ<>
    #JUNBQͷѻ͍΍"TZOD5BTLͳͲʹͭ
    ͍ͯ͸Ҿ͖ଓ͖஫ҙ͢Δ

    View full-size slide

  28. αϯϓϧίʔυஔ͍ͯ·͢
    IUUQTHJUIVCDPN
    XPTIJEBO.FNPSZ-FBL5FTU

    View full-size slide

  29. ࢀߟ
    <>IUUQBOESPJEEFWFMPQFSTCMPHTQPUKQBWPJEJOHNFNPSZMFBLTIUNM
    <>IUUQRJJUBDPNFUFUFUFUJUFNTBGFEFDF⒎EDCG
    <>IUUQEIBUFOBOFKQIJEFDIFDL
    <>IUUQTUBDLPWFSqPXDPNRVFTUJPOTSFNPWFBOPODMJDLMJTUFOFS
    <>IUUQTHJUIVCDPNTRVBSFMFBLDBOBSZ
    <>IUUQRJJUBDPNSFKBTVQPUBSPJUFNTBDGFBCEBEEE
    <>IUUQXXXTMJEFTIBSFOFUUBSBTMFTLJWBOESPJENFNPSZGVOEBNFOUBMT
    <>IUUQXXXTMJEFTIBSFOFUTIBPCJONFNPSZNBOBHFNFOUGPSBOESPJEBQQT
    <>IUUQXXXTMJEFTIBSFOFU7JLBT#BMJLBJHDJOBOESPJE
    <>IUUQDPEFOPUFOFUBOESPJEIUNM
    <>IUUQTHSPVQTHPPHMFDPNGPSVNNTHBOESPJEEFWFMPQFSTDM4[S4DVE9P
    V-P5HGN&+
    <>IUUQTHSPVQTHPPHMFDPNGPSVNUPQJDHPPHMFBENPCBETTEL
    *ZKRENFVN.EJTDVTTJPO
    <>IUUQTHJUIVCDPNBOESPJEQMBUGPSN@GSBNFXPSLT@CBTFDPNNJU
    GBGCEECECGFBGCD

    View full-size slide

  30. ࢀߟ
    <>IUUQRJJUBDPNBNBZJUFNTEGGDGBB⒎
    <>IUUQHBSFOBHJUIVCJPCMPHBOESPJENFNPSZMFBLT
    <>IUUQTEFWFMPQFSBOESPJEDPNUSBJOJOHEJTQMBZJOHCJUNBQTNBOBHF
    NFNPSZIUNM
    <>IUUQTRVBSFHJUIVCJPQJDBTTP
    <>IUUQTHJUIVCDPNCVNQUFDIHMJEF
    <>IUUQXXXTMJEFTIBSFOFUIJSPTIJLVSPLBXBBTZODUBTL
    <>IUUQZBO[NCMPHTQPUKQBOESPJECJUNBQIUNM
    <>IUUQTHJUIVCDPN3FBDUJWF93Y+BWB
    <>IUUQZVLJCMPHTQPUKQUIJTHFUBQQMJDBUJPODPOUFYUBDUJWJUZBQQMJDI
    <>IUUQZUSJOPIBUFOBCMPHDPNFOUSZ

    View full-size slide

  31. ϦʔΫ͢ΔΦϒδΣΫτͷπϦʔͷྫ
    Θ͟ͱϦʔΫ͢ΔΑ͏ʹTUBUJDม਺Λ
    ΠϯελϯεͷϓϩύςΟʹηοτ͠
    ͨ৔߹ʹ૝ఆ͞ΕΔπϦʔ<>

    View full-size slide

  32. ϦʔΫ͢ΔΦϒδΣΫτͷπϦʔͷྫ

    View full-size slide

  33. ϦʔΫ͢ΔΦϒδΣΫτͷπϦʔͷྫ

    View full-size slide

  34. ϦʔΫ͢ΔΦϒδΣΫτͷπϦʔͷྫ

    View full-size slide

  35. ϦʔΫ͢ΔΦϒδΣΫτͷπϦʔͷྫ
    "DUJWJUZͷΠϯελϯε͕ϦʔΫ͢Δ
    αϯϓϧίʔυ4UBUJD'JFME-FBL"DUJWJUZ

    View full-size slide

  36. )FBQ%VNQ΋
    ͋ΔΑ

    View full-size slide

  37. େࡶ೺ͳݟ͚ͭํ
    "OESPJE4UVEJPͷ"OESPJE
    .POJUPSͷ.FNPSZλϒΛ։͘

    View full-size slide

  38. େࡶ೺ͳݟ͚ͭํ
    "DUJWJUZͷىಈͰϝϞϦ͕૿͑ͨޙɺ
    ͦͷ"DJUJWUZΛऴྃͤͯ͞ɺτϥοΫ
    Ϙλϯ ($
    Λdճԡͯ͠($ͤͯ͞
    ΋άϥϑʹग़͍ͯΔϝϞϦͷมԽΛݟ
    Δ
    )FBQ%VNQΛݟͯҟৗʹͨ͘͞Μ
    Πϯελϯεͷ͋ΔΫϥεΛ୳͢

    View full-size slide

  39. )FBQ%VNQͷྫ
    ո͍͠ίʔυΛ༻ҙ
    3FDZDMFS7JFXͷ"EBQUFSΛ
    ղ์ͯ͠ͳ͍

    View full-size slide

  40. )FBQ%VNQͷྫ
    ո͍͠"DUJWJUZΛԿճ͔ىಈऴྃ͢
    Δͱʜ

    View full-size slide

  41. )FBQ%VNQͷྫ
    ը໘ӈଆͷϝχϡʔ͔Β$BQUVSF
    "OBMZTJTΛ։͘

    View full-size slide

  42. )FBQ%VNQͷྫ
    ʮ%FUFDU-FBLFE"DUJWJUJFTʯͰϝ
    ϞϦϦʔΫͷ͋Δ"DUJWJUZ͕Θ͔Δ

    View full-size slide

  43. )FBQ%VNQ஫ҙ
    "OESPJE4UVEJPͷπʔϧ͸͍·઀ଓ
    தͷ୺຤Λ؂ࢹͯ͠Δ
    πʔϧͰ؂ࢹͯ͠Δ୺຤։ൃ༻୺຤
    ͸͍͍ͩͨΞϓϦ͕Πϯετʔϧ͞Ε
    ͔ͯΒͷܦա͕࣌ؒ୹͍
    ΞϓϦͷΠϯετʔϧ࣌ʹ͸ΞϓϦ͕
    ࢖༻͢ΔϝϞϦྖҬ͕ॳظԽ͞ΕΔ

    View full-size slide