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
メモリ最適化を究める!iOSアプリ開発における5つの重要なポイント
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
210
進化を続けるGo toolsの現在地 / The Current State of Ever-Evolving Go Tools
hond0413
0
230
ここ半年くらいでAIに作らせたR用ツール
eitsupi
0
380
Cloudflare is Agents
chimame
0
170
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
700
バグを直したら useEffect が消えた
colorful12
2
130
PostgreSQL 18で考えるUUID主キー
kazuhiro1982
0
470
torikago - Ruby::Boxで照らすモジュラモノリスの実行境界
se4weed
1
380
PHP に部分適用が来るぞ!……ところで何それ?おいしいの? #phpcon / phpcon-2026
shogogg
0
660
複数の Claude Code が"放置"されてしまう問題をCLI ダッシュボードを自作して解決した話
sumihiro3
1
690
仕様駆動開発の消費期限
watany
20
8.4k
JAWS-UG横浜 #102 AWSサ終供養LT会 成仏できない AWS サービスたち 〜本日、三体供養します〜
maroon1st
0
360
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Accessibility Awareness
sabderemane
1
180
Build your cross-platform service in a week with App Engine
jlugia
234
19k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
470
ラッコキーワード サービス紹介資料
rakko
1
4.4M
How to build a perfect <img>
jonoalderson
1
5.9k
Paper Plane (Part 1)
katiecoart
PRO
1
10k
Optimizing for Happiness
mojombo
378
71k
Product Roadmaps are Hard
iamctodd
55
12k
How STYLIGHT went responsive
nonsquared
100
6.2k
Scaling GitHub
holman
464
140k
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