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.4k
GitHub Copilotのススメ
marcy731
1
790
ステートマシンを活用したWebView-ネイティブ間連携へのアプローチ / An Approach to WebView-Native Communication Using State Machines
marcy731
1
1.2k
WebViewをNativeのように使いたい / Using-WebView-like-Native-App
marcy731
2
390
「アプリをつくる仕組み」の構築 / build-system-for-STORES-Branded-Apps
marcy731
0
640
Featured
See All Featured
Building Flexible Design Systems
yeseniaperezcruz
327
38k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
Gamification - CAS2011
davidbonilla
80
5k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
For a Future-Friendly Web
brad_frost
175
9.4k
Building Applications with DynamoDB
mza
90
6.1k
Building Adaptive Systems
keathley
38
2.3k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Measuring & Analyzing Core Web Vitals
bluesmoon
2
75
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
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 • ࢀߟ