Instruments|Instruments 之 Memory

在 iOS 开发中,内存泄漏的检测基本是 APP 功能开发完成之后的必做项目。内存泄漏的检测手段很多,这里就讲讲如何通过 Instruments 的内存工具来定位内存问题。
案例
内存检测以 raywenderlich 的 Catstagram APP 作为分析案例。该 APP包含 2 个页面,第一个页面是一个按钮,第二个页面是一个包含图片的列表,APP 截图如下。

Instruments|Instruments 之 Memory
文章图片
第一个页面
Instruments|Instruments 之 Memory
文章图片
第二个页面 启动 APP,然后点击红色按钮进入列表页面,然后退出列表页面。这么做的原因的是什么呢?因为要检测的是列表页面的内存泄漏问题。所以进入列表页面之后再退出列表页面回到第一个页面,若是列表页面存在内存问题,那么必然是有些对象无法被释放或者该列表页面无法被销毁,这个时候用工具就可以查出对应的内存问题。
Debug Menory Graph
先使用 Xcode(我的 Xcode 版本是 8.3.2) 的 Debug Menory Graph 功能对 APP 的内存泄漏情况做个整体浏览。
点击下图箭头所示的按钮,显示 Debug Menory Graph 界面。

Instruments|Instruments 之 Memory
文章图片
Debug Menory Graph Instruments|Instruments 之 Memory
文章图片
内存泄漏 从上图可以看到,LocationModel,PhotoFeedModel,PhotoModel,UserModel 类的对象在图中都有一个 ! 号,这意味着这些对象都是存在内存问题的,点击对应的 PhotoFeedModel 类的对象,右边显示的是该类的对象的引用情况,可以看到 PhotoFeedModel 有循环引用问题。既然已经知道了哪些对象有内存问题,但是这些对象的使用代码到处都是,要定位问题,光只有这些信息还远远不够,最好是能够定位到某个方法或者某行代码,但这个不是 Debug Menory Graph 的功能。
使用 Leaks 和 Allocations
启动 Instruments,选择 Leaks 模板,Instruments 知道要分析内存问题,所以同时启动了 Allocations 模板。

Instruments|Instruments 之 Memory
文章图片
选择 Leaks 模板 Instruments|Instruments 之 Memory
文章图片
分析内存问题 启动 Instruments 开始录制 APP 运行情况,重复进入退出列表,出现了内存泄漏了情况。打叉表示有内存泄漏。

Instruments|Instruments 之 Memory
文章图片
出现内存泄漏 咋一看信息量有点大,那么我们先过滤一下信息,按箭头所示,选择 Call Tree 视图,然后过滤掉系统库,展开方法调用信息。通过内存泄漏的时间段查看这段时间的方法调用,然后对这些方法的代码进行排查,查找代码问题。
Instruments|Instruments 之 Memory
文章图片
过滤信息 Instruments|Instruments 之 Memory
文章图片
展开调用信息 经过方法排查,定位到了 CatPhotoTableViewCell 的 reverseGeocode 方法,由于方法 reverseGeocode 没有对 self 和 photoModel 变量做 weak 弱引用声明。

func reverseGeocode(locationForPhoto photoModel: PhotoModel) {photoModel.location?.reverseGeocodedLocation(completion: { (locationModel) in self.photoLocationLabel.attributedText = photoModel.locationAttributedString(withFontSize: 14.0) self.photoLocationLabel.attributedText = photoModel.locationAttributedString(withFontSize: 14.0) self.photoLocationLabel.sizeToFit()DispatchQueue.main.async { self.updateConstraints() self.setNeedsLayout() } }) }

修改后如下
func reverseGeocode(locationForPhoto photoModel: PhotoModel) {photoModel.location?.reverseGeocodedLocation(completion: { [weak self, weak photoModel] (locationModel) in self?.photoLocationLabel.attributedText = photoModel?.locationAttributedString(withFontSize: 14.0) self?.photoLocationLabel.attributedText = photoModel?.locationAttributedString(withFontSize: 14.0) self?.photoLocationLabel.sizeToFit()DispatchQueue.main.async { self?.updateConstraints() self?.setNeedsLayout() } }) }

修改之后对修改进行验证,结果说明我们的修改是正确的。
Instruments|Instruments 之 Memory
文章图片
验证修改 总结
【Instruments|Instruments 之 Memory】内存泄漏的解决方法一般步骤
1、使用 Debug Menory Graph 发现问题
2、使用 Instruments 的 Leaks 和 Allocations 定位问题
3、解决问题
4、使用 Debug Menory Graph 验证修改效果
5、若是验证不通过,重复 1 - 4 步骤。若是验证通过则结束。
参考
本文是 raywenderlich 的课程笔记,内容参考 Practical Instruments 课程
1、 demo https://files.betamax.raywenderlich.com/attachments/videos/787/a455ee9d-a5c2-4e1c-9a48-10bf86cbc2b9.zip
2、 https://videos.raywenderlich.com/courses/74-practical-instruments/lessons/5

    推荐阅读