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

実践 9 つのメモリリークどう見つける?/ How to detect 9 types of ...

Kuniwak
October 28, 2022

実践 9 つのメモリリークどう見つける?/ How to detect 9 types of memory leaks?

Kuniwak

October 28, 2022
Tweet

More Decks by Kuniwak

Other Decks in Programming

Transcript

  1. &YFSDJTF public class Exercise1_1 { var value: Any? private let

    weight = Weight1() } public static func exercise1_1() -> Exercise1_1 { let x = Exercise1_1() x.value = x return x } ॥؀ࢀরΛͭ͘Δ άϥϑͷมԽΛΘ͔Γ΍͘͢ 
 ͢ΔͨΊͷେ͖ͳΦϒδΣΫτ 25
  2. &YFSDJTF class Weight1 { private var data: [UInt64] = Array(

    
 repeating: 0, count: 1024 * 1024 ) } ͓Αͦ.J#ͷ༰ྔΛ֬อ 26
  3. &YFSDJTF public class Exercise1_2 { var value: Any? private let

    weight = Weight2() } public static func exercise1_2() -> Exercise1_2 { let x = Exercise1_2() x.value = x return x } ॥؀ࢀরΛͭ͘Δ άϥϑͷมԽΛΘ͔Γ΍͘͢ 
 ͢ΔͨΊͷେ͖ͳΦϒδΣΫτ 36
  4. &YFSDJTF @implementation Weight2 - (id)init { if (self = [super

    init]) { size_t size8MiB = 8*1024*1024; void* mem = malloc(size8MiB); if (mem == NULL) { return self; } // NOTE: Avoid memory overcommit. memset(mem, size8MiB, 1); _mem = mem; } return self; } NBMMPDͰಉ͡༰ྔͷϝϞϦΛ 
 ֬อ͍ͯ͠Δ 37
  5. &YFSDJTF public class Exercise1_3 { var value: Any? private let

    weight = Weight3() } public static func exercise1_3() -> Exercise1_3 { let x = Exercise1_3() x.value = x return x } ॥؀ࢀরΛͭ͘Δ άϥϑͷมԽΛΘ͔Γ΍͘͢ 
 ͢ΔͨΊͷେ͖ͳΦϒδΣΫτ 47
  6. &YFSDJTF class Weight3 { private let data = Data(count: 8

    * 1024 * 1024) } 48 %BUBͰಉ͡༰ྔͷϝϞϦΛ֬อ
  7. &YFSDJTF public static func exercise2() -> Exercise2 { let x

    = Exercise2() var values = [Any]() values.append(Weight2() as Any) for _ in 0..<100 { values.append(Exercise2()) } let y = Indirect() values.append(y) y.value = ["y": x] for _ in 0..<100 { values.append(Exercise2()) } x.value = values return x άϥϑͷมԽΛΘ͔Γ΍͘͢ 
 ͢ΔͨΊͷେ͖ͳΦϒδΣΫτ ॥؀ࢀরΛ௥Ճ ແؔ܎ͳࢀরΛͨ͘͞Μ௥Ճ ແؔ܎ͳࢀরΛͨ͘͞Μ௥Ճ 52
  8. &YFSDJTF public static func exercise3() -> Exercise3 { let x

    = Exercise3() let y = Indirect() x.value = y var prev = y for _ in 0..<100 { let z = Indirect() prev.value = z prev = z } prev.value = x ௕େͳ॥؀ࢀরύεΛઃఆ 54
  9. &YFSDJTF·ͱΊ %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷࣗಈ ݕ஌ 9$5"TTFSU/P-FBL 🤔

    ❌ ✅ ❌ ✅ ௕େͳ॥؀ࢀর͸ݕ஌Ͱ͖ͣ ௕େͳ॥؀ࢀর͸ݕ஌Ͱ͖ͣ 63
  10. &YFSDJTF public class Exercise4 { var value: Any? private let

    weight = Weight1() init() { let x = Indirect() x.value = { _ = self } self.value = x } } Ϋϩʔδϟ͕ࣗ਎ΛڧࢀরͰ 
 Ωϟϓνϟͯ͠॥؀ࢀরͤ͞Δ 64
  11. &YFSDJTF public static func exercise5() -> Exercise5_1 { let x

    = Exercise5_1() let y = Exercise5_2.init(x) x.value = y return x } 0CKFDUJWF$ͱ4XJGUͷؒͰ 
 ॥؀ࢀরͤ͞Δ 66
  12. &YFSDJTF public class Exercise6: EventHubObserver { private let weight =

    Weight2() init() { EventHub.shared.add(observer: self) } deinit { EventHub.shared.removeObserver(observer: self) } } EFJOJUͰߪಡղআ͢Δ͜ͱΛ 
 ҙਤͨ͠ίʔυ͕ͩɺ 
 ͦ΋ͦ΋ߪಡղআ͞Εͳ͍ͱ 
 ࢀর͕࢒͍ͬͯͯEFJOJU͕ 
 ૸Βͳ͍ͷͰϝϞϦϦʔΫ͢Δ 68
  13. &YFSDJTF·ͱΊ %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷࣗಈ ݕ஌ 9$5"TTFSU/P-FBL 🤔

    ❌ ✅ ❌ ✅ ॥؀ࢀরͰ͸ͳ͍ͷͰݕ஌Ͱ͖ͣ ॥؀ࢀরͰ͸ͳ͍ͷͰݕ஌Ͱ͖ͣ 69
  14. &YFSDJTF public class Exercise7 { var value: Any? private let

    weight = Weight2() public class Indirect: CustomReflectable { var value: Any? public var customMirror: Mirror { return Mirror(self, children: []) } } .JSSPS SF fl FDUJOH Ͱ 
 Կ΋TUPSFEQSPQ͕ 
 ͳ͍ͱࡨ֮ͤ͞Δ 70
  15. &YFSDJTF·ͱΊ %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷࣗಈ ݕ஌ 9$5"TTFSU/P-FBL 🤔

    ✅ ✅ ✅ ❌ .JSSPSʹґଘ͍ͯ͠ΔͷͰ$VTUPN3F fl FDUBCMF͕ 
 ࣮૷͞Ε͍ͯΔΦϒδΣΫτͰ͸ෆਖ਼֬ͳ݁ՌʹͳΔ 71
  16. &YFSDJTF·ͱΊ &YFSDJTF %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷ 
 ࣗಈݕ஌

    9$5"TTFSU/P-FBL  ✅ ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ❌ 72
  17. &YFSDJTF·ͱΊ &YFSDJTF %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷ 
 ࣗಈݕ஌

    9$5"TTFSU/P-FBL  ✅ ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ❌ NBMMPDͰ֬อ͞ΕͨϝϞϦ΍%BUBͳͲͷϝϞϦ 
 ࢖༻ྔ͕ݟ͑ͳ͍ɻϝϞϦϦʔΫͯ͠ͳ͍Α͏ʹ 
 ݟ͑ͯ΋ϝϞϦϦʔΫ͍ͯ͠Δ͜ͱ͕͋Γ͏Δ 73
  18. &YFSDJTF·ͱΊ &YFSDJTF %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷ 
 ࣗಈݕ஌

    9$5"TTFSU/P-FBL  ✅ ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ❌ ॥؀ࢀরύε͕௕͍৔߹΍ɺߪಡͷ 
 ղআ๨Ε͸ݕ஌Ͱ͖ͳ͍ɻࣗಈݕ஌Ͱ 
 ϝϞϦϦʔΫ͕͋ΔͱݴΘΕͨ৔߹͸ 
 ৴༻ͯ͠ྑ͍͕ɺٯ͸ա৴ͯ͠͸͍͚ͳ͍ 74
  19. &YFSDJTF·ͱΊ &YFSDJTF %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷ 
 ࣗಈݕ஌

    9$5"TTFSU/P-FBL  ✅ ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ❌ ҆ఆͯ͠ϝϞϦͷ࢖༻ྔͷ૿ݮΛ 
 ֬ೝͰ͖͍ͯΔɻϝϞϦϦʔΫͷ 
 ༗ແΛௐ΂͍ͨͳΒ·ͣ͜ΕΛݟΔ΂͖ 75
  20. &YFSDJTF·ͱΊ &YFSDJTF %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷ 
 ࣗಈݕ஌

    9$5"TTFSU/P-FBL  ✅ ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ❌ ॥؀ࢀরύε͕௕͍৔߹΍ɺߪಡͷ 
 ղআ๨Ε͸ݕ஌Ͱ͖ͳ͍ɻࣗಈݕ஌Ͱ 
 ϝϞϦϦʔΫ͕͋Δͱࢦఠ͞Εͨ৔߹͸ 
 ৴༻ͯ͠Α͍͕ɺͳ͍ͱࢦఠ͞Εͨ৔߹ ͸ա৴ͯ͠͸͍͚ͳ͍ 76
  21. &YFSDJTF·ͱΊ &YFSDJTF %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷ 
 ࣗಈݕ஌

    9$5"TTFSU/P-FBL  ✅ ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ❌ ·Εʹ$VTUPN3F fl FDUBCMFΛ࣮૷͍ͯ͠Δ 
 ΦϒδΣΫτ͕͋ΔͷͰ஫ҙ͕ඞཁɻ ϝϞϦϦʔΫ͕͋Δͱࢦఠ͞Εͨ৔߹͸ 
 ৴༻Ͱ͖Δ͕ɺͳ͍ͱࢦఠ͞Εͨ৔߹͸ 
 ա৴ͯ͠͸͍͚ͳ͍ 77