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
Code Reviewing Like a Champion
maltzj
520
39k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Code Review Best Practice
trishagee
65
17k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.9k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
4 Signs Your Business is Dying
shpigford
181
21k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
A better future with KSS
kneath
238
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 • ࢀߟ