AllocationsとLeaks Instruments
ヒープ割り当てを追跡し、メモリリークやゾンビオブジェクトを見つけます。
「AllocationsとLeaks Instruments」はCoddyKit上の無料Swift Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwift Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Swift Academyコースには全4レッスンが含まれています。
Allocations Instrument
Allocations instrumentは、すべてのヒープ割り当てと解放を追跡し、時間の経過に伴うメモリの増加を表示します。
// Instruments → Allocations template
// Shows live bytes, allocation count, and transient objectsヒープ増加の分析
アプリを操作し、Live Bytesグラフを確認します。増加が止まらず安定しない場合は、メモリリークが発生している可能性があります。
// Normal: allocations rise and fall as views appear/disappear
// Leak: allocations only grow, never released世代の分析
ユーザーフローの前後で世代スナップショットを記録すると、予期せず残存したオブジェクトを確認できます。
// Instruments toolbar → "Mark Generation"
// Perform action (e.g., open and close a screen)
// "Mark Generation" again
// Inspect objects in the new generation that weren't releasedLeaks Instrument
Leaks instrumentはヒープを定期的にスキャンし、割り当てられているものの到達不能なオブジェクト(循環参照)を特定します。
// Run Leaks alongside Allocations in the "Leaks" template
// Red bars in the timeline = leaked objects detected
// Click a bar to see the leaked object graphリークグラフの読み方
Leakオブジェクトグラフには、どのオブジェクトが循環して互いを保持しているかが表示され、循環参照の起点を特定できます。
// Example cycle:
// ViewController → ViewModel → Closure → ViewController
// Instruments draws the reference edges visuallyクロージャにおける循環参照の検出
最も一般的なリークは、クロージャがselfを強参照し、その一方でselfがクロージャを保持することです。
class MyVC: UIViewController {
var onComplete: (() -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
// LEAK: closure captures self; self holds onComplete
onComplete = { self.dismiss(animated: true) }
// FIX:
onComplete = { [weak self] in self?.dismiss(animated: true) }
}
}VM Tracker Instrument
VM Trackerには、仮想メモリ領域とダーティページが表示されます。ダーティメモリの負荷が高いと、iOSによってアプリが終了されます。
// Instruments → "VM Tracker" in allocation instrument
// Look for: large __DATA dirty pages, high CG raster memoryゾンビオブジェクト
InstrumentsでZombie Objectsを有効にすると、解放済みオブジェクトへのアクセスを検出できます。解放済みオブジェクトは「ゾンビ」に変換され、アクセスされたときにログを記録します。
// Instruments → Edit → Record Settings → Enable Zombie Objects
// Accessing a zombie prints: "*** -[MyObject method] sent to deallocated instance"ヒープショット
「Heap Shot」で2つのヒープ状態を比較すると、ショット間で残存しているオブジェクトを見つけられます。ビューコントローラーのリーク検出に特に役立ちます。
// After navigating to and from a screen twice:
// Heap should return to baseline
// Objects remaining in the diff = leaked view controllers or models本番環境のメモリ計測におけるMetricKit
MXMetricPayloadを使用して、本番環境の実ユーザーからメモリとパフォーマンスのデータを収集します。
import MetricKit
class AppDelegate: UIApplicationDelegate, MXMetricManagerSubscriber {
func didReceive(_ payloads: [MXMetricPayload]) {
payloads.forEach { print($0.memoryMetrics as Any) }
}
}理解度チェック
Allocations instrumentの「Live Bytes」グラフが着実に増え続けている場合、何を示していますか。
レッスンのまとめ
Allocationsを使ってヒープの増加と世代スナップショットを追跡します。Leaksを使って循環参照を検出し、オブジェクトグラフを確認します。解放済みオブジェクトへのアクセスによるバグにはZombie Objectsを使用します。[weak self]でクロージャを修正します。MetricKitを使って、本番環境の実ユーザーのメモリ使用状況を追跡します。
よくある質問
「AllocationsとLeaks Instruments」レッスンは無料ですか?
はい。「AllocationsとLeaks Instruments」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Swift Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Swift Academyコースには全4レッスンが含まれています。
「AllocationsとLeaks Instruments」で何を学びますか?
ヒープ割り当てを追跡し、メモリリークやゾンビオブジェクトを見つけます。 ブラウザで直接実行するハンズオンコードでSwift Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Swift Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwift Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「AllocationsとLeaks Instruments」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwift Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwift Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Instrumentsの基礎:Time Profiler
- AllocationsとLeaks Instruments
- Main Thread Checkerとハング
- ホットパスの最適化:COW、インライン化、特殊化