Kuniwak
October 28, 2022
5k

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

October 28, 2022

## Transcript

4. ### ϝϞϦϦʔΫ͕͋Δͱɺ࢖͑ΔϝϞϦ͕   ঃʑʹݮ͍ͬͯ͘ ͢ΔͱϝϞϦϓϨογϟʔ͕֤ΞϓϦʹ   ͔͚ΒΕɺͦΕͰ΋ϝϞϦ͕଍Βͳ͍ͱ   ΞϓϦ͕KFUTBNʢւʹϙΠʣ͞ΕΔ ৄ͘͠͸ʮ*EFOUJGZJOHIJHINFNPSZVTFXJUIKFUTBNFWFOUSFQPSUTʯΛࢀরɻ

IUUQTEFWFMPQFSBQQMFDPNEPDVNFOUBUJPOYDPEFJEFOUJGZJOHIJHINFNPSZVTFXJUIKFUTBNFWFOUSFQPSUT 4

25. ### &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
26. ### &YFSDJTF class Weight1 { private var data: [UInt64] = Array(

repeating: 0, count: 1024 * 1024 ) } ͓Αͦ.J#ͷ༰ྔΛ֬อ 26

✅ ✅ ✅ ✅ 35
36. ### &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
37. ### &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

46. ### &YFSDJTF·ͱΊ %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷࣗಈ ݕ஌ 9\$5"TTFSU/P-FBL 🤔

✅ ✅ ✅ ✅ 46 NBMMPDͰ֬อ͞ΕͨϝϞϦ͕දࣔ͞Εͳ͍
47. ### &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
48. ### &YFSDJTF class Weight3 { private let data = Data(count: 8

* 1024 * 1024) } 48 %BUBͰಉ͡༰ྔͷϝϞϦΛ֬อ

51. ### &YFSDJTF·ͱΊ %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷࣗಈ ݕ஌ 9\$5"TTFSU/P-FBL 🤔

✅ ✅ ✅ ✅ 51 %BUBͰ֬อ͞ΕͨϝϞϦ͕දࣔ͞Εͳ͍
52. ### &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

✅ ✅ ✅ ✅ 53
54. ### &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

63. ### &YFSDJTF·ͱΊ %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷࣗಈ ݕ஌ 9\$5"TTFSU/P-FBL 🤔

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

weight = Weight1() init() { let x = Indirect() x.value = { _ = self } self.value = x } } Ϋϩʔδϟ͕ࣗ਎ΛڧࢀরͰ   Ωϟϓνϟͯ͠॥؀ࢀরͤ͞Δ 64

✅ ✅ ✅ ✅ 65
66. ### &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

✅ ✅ ✅ ✅ 67
68. ### &YFSDJTF public class Exercise6: EventHubObserver { private let weight =

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

❌ ✅ ❌ ✅ ॥؀ࢀরͰ͸ͳ͍ͷͰݕ஌Ͱ͖ͣ ॥؀ࢀরͰ͸ͳ͍ͷͰݕ஌Ͱ͖ͣ 69
70. ### &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 Ͱ   Կ΋TUPSFEQSPQ͕   ͳ͍ͱࡨ֮ͤ͞Δ 70
71. ### &YFSDJTF·ͱΊ %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷࣗಈ ݕ஌ 9\$5"TTFSU/P-FBL 🤔

✅ ✅ ✅ ❌ .JSSPSʹґଘ͍ͯ͠ΔͷͰ\$VTUPN3F fl FDUBCMF͕   ࣮૷͞Ε͍ͯΔΦϒδΣΫτͰ͸ෆਖ਼֬ͳ݁ՌʹͳΔ 71
72. ### &YFSDJTF·ͱΊ &YFSDJTF %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷ   ࣗಈݕ஌

9\$5"TTFSU/P-FBL  ✅ ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ❌ 72
73. ### &YFSDJTF·ͱΊ &YFSDJTF %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷ   ࣗಈݕ஌

9\$5"TTFSU/P-FBL  ✅ ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ✅ ✅ ✅ ✅  🤔 ❌ ✅ ❌ ✅  🤔 ✅ ✅ ✅ ❌ NBMMPDͰ֬อ͞ΕͨϝϞϦ΍%BUBͳͲͷϝϞϦ   ࢖༻ྔ͕ݟ͑ͳ͍ɻϝϞϦϦʔΫͯ͠ͳ͍Α͏ʹ   ݟ͑ͯ΋ϝϞϦϦʔΫ͍ͯ͠Δ͜ͱ͕͋Γ͏Δ 73
74. ### &YFSDJTF·ͱΊ &YFSDJTF %FCVH/BWJHBUPS %FCVH.FNPSZ (SBQI *OTUSVNFOUT ͷϝϞϦάϥϑ *OTUSVNFOUTͷ   ࣗಈݕ஌

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

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

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

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