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
15
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.5k
GitHub Copilotのススメ
marcy731
1
800
ステートマシンを活用したWebView-ネイティブ間連携へのアプローチ / An Approach to WebView-Native Communication Using State Machines
marcy731
1
1.2k
WebViewをNativeのように使いたい / Using-WebView-like-Native-App
marcy731
2
400
「アプリをつくる仕組み」の構築 / build-system-for-STORES-Branded-Apps
marcy731
0
640
Featured
See All Featured
Designing for humans not robots
tammielis
250
25k
A Philosophy of Restraint
colly
203
16k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Teambox: Starting and Learning
jrom
133
8.8k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Building an army of robots
kneath
302
43k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Bash Introduction
62gerente
608
210k
Agile that works and the tools we love
rasmusluckow
327
21k
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 • ࢀߟ