Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Introduction to Memory Management in Swift - S...
Search
marcy731
October 26, 2023
0
17
Introduction to Memory Management in Swift - Swiftのメモリ管理を知る -
2023/10/26
STORES モバイルLT会!- 2023年秋の気配編 -
marcy731
October 26, 2023
Tweet
Share
More Decks by marcy731
See All by marcy731
めざせ!WKWebViewマスター! / WKWebView Master
marcy731
4
1.7k
GitHub Copilotのススメ
marcy731
1
870
ステートマシンを活用したWebView-ネイティブ間連携へのアプローチ / An Approach to WebView-Native Communication Using State Machines
marcy731
1
1.2k
WebViewをNativeのように使いたい / Using-WebView-like-Native-App
marcy731
2
410
「アプリをつくる仕組み」の構築 / build-system-for-STORES-Branded-Apps
marcy731
0
660
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
133
9k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Practical Orchestrator
shlominoach
186
10k
Rails Girls Zürich Keynote
gr2m
94
13k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
The Language of Interfaces
destraynor
154
24k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Side Projects
sachag
452
42k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Transcript
Introduction to Memory Management in Swift 2023/10/26 STORES ϞόΠϧLTձʂ- 2023ळͷؾฤ
- @marcy731 - SwiftͷϝϞϦཧΛΔ -
• ਖ਼ɺ࠷ۙͷiOSΞϓϦ։ൃʹ͓͍ͯϝϞϦΛҙࣝ͢Δ͜ͱ΄ͱΜͲͳ͍ • ͦΜͳதɺ࠷ۙTCAΛ৮͍ͬͯΔͳ͔ͰStack Over f lowΛىͯ͜͠Ϋϥο γϡ͢Δࣄྫʹૺ۰ • ͋ΒͨΊͯSwiftͷϝϞϦཧΛ؆୯ʹ෮शͯ͠Έͨ
എܠ
1. ܕͱࢀরܕ 2. ϝϞϦஔʢStack / Heapʣ 3. ARC (Automatic Reference
Counting)ͱࢀরΧϯτ 4. ॥ࢀরͱͦͷରࡦ 5. ڧࢀর / ऑࢀর / ඇॴ༗ࢀর 6. ࠓճૺ۰ͨ͠Stack Over f lowͱͦͷରࡦ ࣍
• ܕ • σʔλ͕ίϐʔ͞ΕΔ • ExɿStruct / Enum / Double(Int)
/ String / Array / Dictionary / Tuple / Set • ϙΠϯτ • Swiftܕத৺ͷݴޠ • ίϨΫγϣϯܕʢArray, Dictionaryʣʮcopy-on-writeʯͷΈͰܕʹͳ͍ͬͯΔ • ࢀরܕ • σʔλͷॴͷࢀর͕ίϐʔ͞ΕΔ • ExɿClass / Function / Clusures ܕͱࢀরܕ
• Memory Layout ϝϞϦஔʢStack / Heapʣ Code / Global Variable
Heap Stack Beginning of Memory End of Memory
• StackྖҬ • ओʹܕͷσʔλҰ࣌తͳσʔλ͕อଘ • ؔͷݺͼग़͠ϩʔΧϧมͷ֨ೲ • ֨ೲ͞ΕͨσʔλɺͦͷείʔϓϥΠϑαΠΫϧ͕ऴྃ͢Δͱࣗಈతʹղ์ • HeapྖҬ
• ࢀরܕͷΠϯελϯε͕อଘ͞ΕΔྖҬ • ϝϞϦͷ֬อղ์Λ໌ࣔతʹߦ͏ඞཁ͕͋Δ • SwiftͰARC (Automatic Reference Counting)Λ༻͍ͯࣗಈతʹཧ ϝϞϦஔʢStack / Heapʣ
• copy-on-assignment • ܕͷσʔλΛೖ͢Δͱ͖ɺσʔλ͕࣮ࡍʹίϐʔ͞ΕΔੑ࣭ • Φʔόʔϔουͳ͘ߴʹΞΫηε • copy-on-write • SwiftͷίϨΫγϣϯͰར༻͞ΕΔɺσʔλ͕มߋ͞ΕΔ·ͰίϐʔΛԆͤ͞ΔΈ
• ޮతͳϝϞϦ༻Λ࣮ݱ • ྫ͑RubyͷArrayࢀরܕ͕ͩɺSwiftͷArraycopy-on-writeͷΈͰܕͱͯ͠; Δ·͏ copy-on-assignment / copy-on-write
• ՝ • ࢀরܕΛ෦ʹ࣋ͭStructɺHeapͷࢀরΛ͍࣋ͬͯΔͨΊɺશʹStack֬อ ͞ΕͨΦϒδΣΫτͱΈͳ͞Εͳ͍ • ͦͷͨΊɺ͜ͷΦϒδΣΫτΛίϐʔ͢ΔͱɺHeap֬อ͕͘ͳΓɺίϐʔʹେ͖ ͳίετ͕͔͔Δ • copy-on-writeΛར༻͢Δͱ
• ΦϒδΣΫτΛผͷΦϒδΣΫτʹೖͯ͠ɺܕʢStructͳͲʣίϐʔ͞Ε ͣɺίϐʔͷ1͕ͭมߋ͞ΕΔ·Ͱࢀর͕࡞͞ΕΔ copy-on-write
• ARC (Automatic Reference Counting) • HeapྖҬͷϝϞϦཧͷΈ • ࢀরܕͷΠϯελϯεͷࢀরΛΧϯτ͢ΔγεςϜ •
͜ͷΧϯτΛʮࢀরΧϯτʯͱ͍͏ • ࢀরΧϯτ • Πϯελϯεͷ৽͍͠ڧ͍ࢀর͕࡞ΒΕΔɹ+1 • ͦͷࢀর͕ͳ͘ͳΔɹ-1 • ࢀরΧϯτ͕0ʹͳͬͨͱ͖ɺͦͷΠϯελϯεϝϞϦ͔Βղ์ • ARCΛ༻͢Δ͜ͱͰखಈͷϝϞϦղ์ͷඞཁ͕ͳ͘ͳΔ ARCͱࢀরΧϯτ
• ॥ࢀর • 2ͭҎ্ͷΦϒδΣΫτ͕ޓ͍ʹڧࢀর͠߹͍ͬͯΔঢ়ଶ • ΦϒδΣΫτ͕ೋͱ༻͞Εͳ͍ʹ͔͔ΘΒͣɺΦϒδΣΫτʹׂΓ ͯΒΕͨϝϞϦ͕ղ์͞Εͳ͍ • ͦͷ݁Ռͱͯ͠ϝϞϦϦʔΫΛҾ͖ى͜͢ঢ়ଶ •
ରࡦ • ऑࢀরඇॴ༗ࢀরΛ༻ ॥ࢀরͱͦͷରࡦ
• ڧࢀর(Strong) • ΦϒδΣΫτͷ௨ৗͷࢀর • ࢀরΧϯτ +1 • ऑࢀর(weak) •
ΦϒδΣΫτͷࢀরΛ࣋ͭͷͷɺࢀরΧϯτΛ૿͞ͳ͍ࢀর • ΦϒδΣΫτ͕ղ์͞ΕΔͱࣗಈతʹnilʹͳΔ • ඇॴ༗ࢀর(unowed) • ऑࢀরͱࣅ͍ͯ·͕͢ɺnilʹͳΔ͜ͱΛڐ༰͠ͳ͍ • ࢀর͍ͯ͠ΔΦϒδΣΫτ͕ղ์͞ΕΔલʹඇॴ༗ࢀরࣗମ͕ղ์͞ΕΔ͜ͱΛอূ͢Δඞཁ͕͋Δ ڧࢀর / ऑࢀর / ඇॴ༗ࢀর
• ݪҼ • TCAReduxΞʔΩςΫνϟ • Ruducer / State StructͰఆٛ͞ΕΔ •
StateࢠStateΛෳ࣋ͯΔωετߏ • Structͷωετ͞ΕͨFieldͷංେԽϥϯλΠϜ࣌ʹStackΛѹഭɺ StackOver f lowʹؕͬͨ • iPhone ͷStackϝϞϦ1MB΄Ͳͱখ͍͞ʢSimulator8MBʣ • ରࡦ • TCAStackʹ͋ΔσʔλΛHeapʹಀͨ͢ΊʹCoWΛར༻ͨ͠ PresentationStateͱ͍͏ΈΛಋೖ͍ͯ͠ΔͷͰɺͦΕΛར༻ • ࢠυϝΠϯͷঢ়ଶΛHeapͰཧ͢Δ͜ͱͰɺޮత͔ͭ҆શʹঢ়ଶΛཧ ࠓճૺ۰ͨ͠Stack Over f lowͱͦͷରࡦ
• SwiftૉΒ͍͠ϝϞϦཧγεςϜΛ࣋ͭ • ͦͷͨΊɺීஈ͋·ΓϝϞϦཧΛҙࣝͤͣʹΞϓϦ։ൃ͕Ͱ͖Δ • ҙ • ॥ࢀরʹؾΛ͚ͭΑ͏ • ܕͱࢀরܕͷซ༻࣌ʹؾΛ͚ͭΑ͏
• ͜ͷͱ͖ͷܕStackͰͳ͘Heapʹ֨ೲ͞ΕύϑΥʔϚϯε͕མͪΔ • StructΛωετ͠StateཧΛ͢Δ߹ɺStack Over f lowʹؾΛ͚ͭΑ͏ ·ͱΊ
• https://heart-of-swift.github.io/value-semantics/how-to-use-value- types.html • https://aymanmoo.medium.com/copy-on-assignment-vs-copy-on-write-in- swift-c3016b343d06 • ࢀߟ