Leaks工具的作用
Leaks instrument 工具检查进程堆泄露的内存。你可以使用该 instrument 工具 配合 Allocations instrument 工具来获取内存地址的历史记录。该 instrument 工具 要求你加载一个进程以便它可以从进程启动时收集数据。
如果发生了内存泄露,它可以从泄露详细信息中查看泄露的具体对象以及方法调用栈。它可以为我们提供以下信息:
内存泄露的数量(The number of leaks)
每个泄露内存块的大小(The size of each leak)
泄露内存块的地址(Address of the leaked block)
泄露对象的类型(Type of the leadked object)
Leaks工具的使用
- 启动xcode,选择Instruments工具



添加Leaks工具时,会默认将Allocations(分配内存)工具也添加进来。
2.使用Leaks工具
首先,在下图区域选择需要测试的手机及手机上的app

其次,点击左侧第一个按钮,开始追踪数据
数据会实时显示在下图区域

如果想要暂停,点击左侧第二个按钮
如何利用Leaks工具检测解决内存泄露
数据跑完了之后,我们就需要对数据进行分析,看是否有内存泄露的情况
如下图所示,Leaks Checks区域有红色小方块,则表示此处有内存泄露

点击红色小方块后,Details区域会出现详细的信息
这时,点击Details>Leaks,选择Call Tree:

并勾选下图区域:

这时,可以看到左侧Details区域出现了关于Leaks的详细信息,包括:内存泄露对象,泄露个数,占比,以及出现问题的代码位置:

选择一条记录,双击,即可定位到有内存泄露的代码位置

这样就可以针对有问题的代码进行修复啦。
注意:
如Details中定位到的不是具体的代码位置,而是地址符号,这时需要
在Build Settings -> Build Options 将Debug Information Format 的Debug值由DWAFR改成DWAFR with dSYM。

网友评论