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
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
0
220
Jetpack Compose メカニズム
skydoves
0
470
App Intentsのビルドプロセスを支える技術
kntkymt
0
420
Workers Cache を知る
syumai
0
200
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
1
11k
AGENTS.md Is Not Enough:Build Skills, Don't Download Them
lx_t
0
130
AI活用は、個人から組織へ|マルチプレイヤーエージェントハーネス「QM」の社内活用事例 / AI use is moving from individuals to orgs
rkaga
1
160
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
190
20260914 AIエージェント時代のPlatform Engineering LLM基盤とプロダクトの責務境界線
kanfab1
7
2.2k
wkhtmltopdfの次どうするか問題2026
willnet
2
1.6k
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
120
動作中のプログラムの中身をリアルタイムに覗く / Realtime Debugger for CSharp with Roslyn
prota
1
1k
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
Scaling GitHub
holman
464
140k
The untapped power of vector embeddings
frankvandijk
2
1.9k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.9k
GitHub's CSS Performance
jonrohan
1033
470k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
67
58k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Google's AI Overviews - The New Search
badams
0
1.6k
Ethics towards AI in product and experience design
skipperchong
2
380
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
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