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
エンジニアにデザインハーネスを 〜デザインプロセスを規定するためのハーネス〜 / Design harness from an engineer's perspective
rkaga
2
1.7k
php-fpmのプロセスが枯渇した日-調査・対処・そして本当にやるべきだったこと-
shibuchaaaan
0
140
Apache Hive: Toward a Cloud Native Lakehouse
okumin
0
160
継続モナドとリアクティブプログラミング
yukikurage
3
640
FDEが実現するAI駆動経営の現在地
gonta
2
220
AIエージェントで 変わるAndroid開発環境
takahirom
2
720
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
720
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
16
3.6k
ソフトウェア設計に溶けるインフラ ― AWS CDK のインフラ認識論
konokenj
2
640
分散システム、なんですぐ死んでしまうん?耐障害性を高めたいあなたのためのレジリエンスパターン入門
mshibuya
7
6.9k
人間の目はかわらない、だからJPEGは30年もつ
yuzneri
8
15k
はてなアカウント基盤 State of the Union
cockscomb
1
1.3k
Featured
See All Featured
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.3k
Making the Leap to Tech Lead
cromwellryan
135
10k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.3k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1k
Fireside Chat
paigeccino
42
4k
Design in an AI World
tapps
1
270
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.4k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
390
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Tell your own story through comics
letsgokoyo
1
1k
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