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
18
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.8k
GitHub Copilotのススメ
marcy731
1
910
ステートマシンを活用したWebView-ネイティブ間連携へのアプローチ / An Approach to WebView-Native Communication Using State Machines
marcy731
1
1.3k
WebViewをNativeのように使いたい / Using-WebView-like-Native-App
marcy731
2
430
「アプリをつくる仕組み」の構築 / build-system-for-STORES-Branded-Apps
marcy731
0
670
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
366
19k
Designing for Performance
lara
604
68k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
GraphQLとの向き合い方2022年版
quramy
44
13k
Designing for humans not robots
tammielis
250
25k
BBQ
matthewcrist
85
9.4k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.1k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Mobile First: as difficult as doing things right
swwweet
222
9k
The Pragmatic Product Professional
lauravandoore
32
6.4k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.5k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
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 • ࢀߟ