Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
メモリ最適化を究める!iOSアプリ開発における5つの重要なポイント
Search
Yuta Hirakawa
August 27, 2024
Programming
1.3k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
メモリ最適化を究める!iOSアプリ開発における5つの重要なポイント
iOS DC 2024で発表した資料になります。
Yuta Hirakawa
August 27, 2024
Other Decks in Programming
See All in Programming
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
190
アクセシビリティから考える情報設計
high_g_engineer
0
420
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
270
一人だけ、Kiroが静止する日
hideg
0
140
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
340
JPUG勉強会 OSSデータベースの内部構造を理解しよう(第2回)
oga5
0
280
Go × SIMDで高速化するベクトル検索 ~ルーフラインモデルでSIMDが効く境界を探れ! ~
po3rin
1
4.8k
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
180
ゲームコントローラやキーボードのファームウェアをSwiftで書く
kishikawakatsumi
1
270
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
490
[2026-09-26]空論ジェネリックプロセス~テスト資産とAIで紡ぐ、再現可能なパフォーマンスチューニングの話~
tosite
0
230
C#の現在地 進化の歴史と、AI時代の.NET Everywhere
neuecc
5
4.4k
Featured
See All Featured
Statistics for Hackers
jakevdp
799
230k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
1.3k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
550
Practical Orchestrator
shlominoach
192
12k
Embracing the Ebb and Flow
colly
88
5.2k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.6k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
530
The SEO Collaboration Effect
kristinabergwall1
1
580
Transcript
© RAKUS Co., Ltd. 1 メモリ最適化を究める! iOSアプリ開発における5つの重要なポイント Yuta.Hirakawa
2 ⾃⼰紹介 名前: Yuta Hirakawa 所属: 株式会社ラクス 担当: iOSアプリ開発 趣味: 料理、読書 @hirasan333
3 メモリ最適化を究める! iOSアプリ開発における5つの重要なポイント
4 循環参照によるメモリリーク キャッシュ
5 循環参照によるメモリリークについて iOSは利⽤されなくなったオブジェクトを メモリから開放してくれる仕組みがある。 ただし実装⽅法によっては「循環参照」が発⽣し、 メモリに残り続ける事象のことをメモリリークと⾔う。
6 循環参照によるメモリリークについて Object A (参照カウント1) Object B (参照カウント1) 互いに強参照
7 selfが強参照になりメモリリークが発⽣する。 AVCaptureDevice.requestAccess(for: .video) { granted in if granted {
self.isCameraAuthorized = true } } 1.クロージャ内self参照による循環参照
8 解決⽅法: [weak self]を追加し弱参照にする。 AVCaptureDevice.requestAccess(for: .video) { [weak self] granted
in if granted { self?.isCameraAuthorized = true } } 1.クロージャ内self参照による循環参照
class Child { var parent: Parent init(parent: Parent) { self.parent
= parent } } 9 ⼦クラスに親クラスを持たせる必要がある場合、循環参照が発⽣する。 2.親⼦クラスの循環参照
10 解決⽅法: ⼦クラスが持っている親クラスにweakを付けて弱参照にする。 class Child { weak var parent: Parent?
init(parent: Parent) { self.parent = parent } } 2.親⼦クラスの循環参照
11 3.URLSessionConfiguration = .defaultの指定 プロポーザルに⼊れておりましたが、 後々調べた所問題無い判明しました。
12 3.URLSessionConfiguration = .defaultの指定 プロポーザルに⼊れておりましたが、 後々調べた所問題無い判明しました。 実は5つのポイントではなく4つのポイントでした。
class URLSessionClient { init(...) { super.init() self.session = URLSession(configuration: config,
delegate: self, delegateQueue: queue) } } 13 URLSessionのdelegateがselfの状態にする。 4.URLSessionDelegateのextension実装
14 URLSessionDelegateをextensionで実装し、URLSessionClientの関数を 呼び出すと強参照になる。 extension URLSessionClient: URLSessionDelegate { func urlSession(_ session:
URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { myMethod(didReceive: challenge, completion: completionHandler) } } 4.URLSessionDelegateのextension実装
15 解決⽅法: SessionDelegate クラスを作成してデリゲート処理を分離し、 sessionClientプロパティをweakにする。 class SessionDelegate: NSObject, URLSessionDelegate {
weak var sessionClient: URLSessionClient? func urlSession( _ session: URLSession, didReceive challenge: URLAuthenticationChallenge, ... ) { sessionClient?.myMethod(didReceive: challenge, completionHandler: completionHandler) } 4.URLSessionDelegateのextension実装
16 解決⽅法: URLSessionClientではSessionDelegateに⾃⾝を設定する。 class URLSessionClient { private let delegate =
SessionDelegate() init(...) { super.init() self.delegate.sessionClient = self self.session = URLSession(configuration: config, delegate: self.delegate, delegateQueue: queue) } } 4.URLSessionDelegateのextension実装
17 1度のみの利⽤であってもこのAPI利⽤して画像を取得すると メモリ上にキャッシュされる。 UIImage(named: "imageName") Image("imageName") 5.UIImage(named:)による画像呼び出し
18 解決⽅法: 再利⽤しない画像はcontentsOfで読み込む。 if let path = Bundle.main.path(forResource: "imageName", ofType:
"png"), let data = try? Data(contentsOf: URL(fileURLWithPath: path)), let image = UIImage(data: data) { Image(uiImage: image) } 5.UIImage(named:)による画像呼び出し
19 • 循環参照(強参照)に気をつける。 ◦ クロージャ ◦ 親⼦関係にあるクラス ◦ extension •
UIImageやImageで画像を取得する場合はキャッシュされることを 意識する。 メモリ最適化を究める! iOSアプリ開発における5つの重要なポイント Yuta Hirakawa @hirasan333