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
Yuta Hirakawa
August 27, 2024
Programming
1.2k
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
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
2
1.5k
【やさしく解説 設計編 #1】「ドメイン駆動」と「実装駆動」ってなに? 〜設計の考え方を、たとえ話で学ぼう〜
panda728
PRO
1
120
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
230
act1-costs.pdf
sumedhbala
0
230
Prismを使った型安全な暗号化_関数型まつり2026
_fhhmm
0
140
Performance Engineering for Everyone
elenatanasoiu
0
270
【SRE NEXT 2026 Lunch Session】一人目専任SREの立ち上げを加速する ― AIと進めたオンボーディングで2分を0.04秒にした話
pkshadeck
PRO
0
2.7k
SREの積み重ねがAI駆動開発のガードレールになった ― 7つの実践/SRE Guardrails The 7
tomoyakitaura
8
4.3k
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
150
symfony/aiとlaravel/boost
77web
0
130
琵琶湖の水は止められてもNet--HTTPのリトライは止められない / You might be able to stop the water flow of Lake Biwa but you can't stop Net::HTTP retries
luccafort
PRO
0
390
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
210
Featured
See All Featured
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
490
What's in a price? How to price your products and services
michaelherold
247
13k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
180
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
470
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
450
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
370
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.9k
How STYLIGHT went responsive
nonsquared
100
6.2k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2k
The Curious Case for Waylosing
cassininazir
1
430
Designing for humans not robots
tammielis
254
26k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
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