Swift Academy · 课时

Allocations 与 Leaks Instruments

跟踪堆分配,查找内存泄漏和僵尸对象。

第 2 / 4 课12 个步骤

Allocations 与 Leaks Instruments 是 CoddyKit 上的免费 Swift Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Swift Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Swift Academy 课程共包含 4 节课。

Allocations 工具

Allocations 工具会跟踪每次堆分配和释放,显示内存随时间的增长情况。

// Instruments → Allocations template
// Shows live bytes, allocation count, and transient objects

堆增长分析

操作您的应用并观察“实时字节数”图表。如果内存持续增长且没有趋于稳定,则表示存在内存泄漏。

// 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 released

Leaks 工具

Leaks 工具会定期扫描堆,并识别已分配但无法访问的对象(循环引用)。

// 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

读取泄漏图

泄漏对象图会显示哪些对象以循环方式相互保留,从而定位循环引用的根对象。

// 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 工具

VM Tracker 会显示虚拟内存区域和脏页。较高的脏内存压力会导致 iOS 终止您的应用。

// Instruments → "VM Tracker" in allocation instrument
// Look for: large __DATA dirty pages, high CG raster memory

僵尸对象

在 Instruments 中启用僵尸对象,以捕获过度释放的对象——这些对象会变成“僵尸”,在被访问时记录日志。

// Instruments → Edit → Record Settings → Enable Zombie Objects
// Accessing a zombie prints: "*** -[MyObject method] sent to deallocated instance"

堆快照

使用“堆快照”比较两个堆状态,以找出在两次快照之间持续存在的对象,这对于检测视图控制器泄漏非常有用。

// 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 工具中“实时字节数”图表持续增长表示什么?

课程回顾

使用 Allocations 跟踪堆增长和代际快照。使用 Leaks 检测循环引用并查看对象图。使用僵尸对象查找过度释放错误。使用 [weak self] 修复闭包。使用 MetricKit 跟踪生产环境中真实用户的内存情况。

免费开始

用 AI 导师学习 Swift — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
122
课程
409

常见问题解答

「Allocations 与 Leaks Instruments」课时是免费的吗?

是的 — 「Allocations 与 Leaks Instruments」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Swift Academy 课程的其余内容,请升级到 CoddyKit PRO。 Swift Academy 课程共包含 4 节课。

「Allocations 与 Leaks Instruments」这节课中我会学到什么?

跟踪堆分配,查找内存泄漏和僵尸对象。 你通过在浏览器中直接运行的动手代码来练习 Swift Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Swift Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Swift Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「Allocations 与 Leaks Instruments」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Swift Academy 课中编写并运行代码吗?

能。每节 Swift Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Instruments 基础:时间分析器
  2. Allocations 与 Leaks Instruments
  3. 主线程检查器与卡顿
  4. 优化热点路径:COW、内联与特化
← 返回 Swift Academy