Instruments User Guide编程指南5-Profile Your App's Memory Usage

About Memory Analysis

无论什么时候APP创建对象,将会为对象分配内存。惯例上,APP将跟踪这些对象,并且在不需要的时候释放它们以至于能够为其它对象重新分配内存。Automatic Reference Counting (ARC) 是一个非常有用的特性,将使得内存事情变得非常简单,将由系统负责内存管理。当ARC被允许,系统将监控对象在合适的时候分配和释放,APP将几乎不需要做什么工作。不管内存如何管理,但是,即使是最好的APP在某些情况下也可能遭遇内存问题,所以非常难被孤立。

Instruments能够帮助绘制APP是怎样使用内存的图形,这使得更容易识别潜在的问题区域。它甚至能够自动检测内存问题的类型并且进行标记,方便我们的分析。使用Instruments来监测和跟踪如下情况:

Overall Memory Use:监控在高级别上是APP如何使用内存并且与系统中其他活跃过程使用内存进行比较。寻找大区域或者意想不到的内存增长。 Monitor Memory Usage .

Leaked Memory内存已经被分配,但是从来没有被释放掉并且不再被APP引用。正是因为并没有对象引用该区域内存,而且没有方式释放内存,所以该内存不能够被再一次使用。例如:假定我们编写一个APP,在绘制的时候创建了一个矩形对象,但是当绘制结束却从未释放该对象。在这种情况下:我们的APP可能泄露越来越多的内存。为了解决泄露,我们需要知道哪一个对象并没有被释放掉,这时更新APP并且在适合的适合释放该对象。 Find Memory Leaks

Abandoned Memory:由于某些原因内存被分配了,但是它并不需要和被引用。例如:假设我们的APP中在图片被缓存之后又添加了图片(images)到缓存,使用了两倍的内存存储相同的images。或者,APP中拥有一个包含对象的数组,为了能够在需要的时候访问,但是事实上你从未使用。不像内存泄露,被遗弃的内存(abandoned memory)好像这里仍然在APP中的某个地方引用。它仅仅是没有目的性,因为在技术上是合理的,所以使用Instruments是非常困难识别和进行更多的检测工作。Find Abandoned Memory.

Zombies:这是内存已经被释放并且不再需要,但是在代码中仍然有某个地方引用了它。例如:假设我们APP中包含了图片缓存,一旦缓存被清理,在APP中不能够尝试着引用不存在的对象。对于这些不存在的对象,我们称之为僵尸对象(zombies) ,即对象不再被引用。Find Zombies.

由于内存问题能够变得非常困难被发现,为了寻找意想不到或者无限制的内存增长,可以对APP的不同阶段进行周期性快拍(snapshots )。我们能够对比快拍来确定对象是怎样被分配和释放,和内存的超时使用。

通过持续执行一系列重复的用户行为,导致APP进入新的状态和回到最初状态,对于测试APP非常重要。这种类型的搜索称为generational analysis,每次APP进入新的状态,“generation”对象将被创建。在大多数情况下,当APP回到最初状态的时候,你应该期待这些对象被释放掉。例如:如果APP打开了Window,当Window被关闭的时候,任意的Window对象都应该被释放掉。如果这些对象没有被释放掉,这是可能会发生内存问题。当执行generational analysis,初始化generationa可能呈现比我们预期的情况产生更大的内存,由于缓存和初始化过程。因此,需要多次重复用户动作,为了获得真实的APP体验感觉。对于更深层次的内存问题,可以看Advanced Memory Management Programming Guide.

Monitor Memory Usage

使用Activity Monitor性能模板通过对比运行在系统上的其它运行过程来跟踪APP真实和虚拟内存。

To create a trace document to monitor overall memory usage 创建跟踪文件监控整体内存使用情况

1:Launch Instruments. 启动Instruments
2In the profiling template selection dialog that appears, click Activity Monitor.  选择Activity Monitor
3:Choose your device and app from the target device and process lists. 选择设备和APP
4:Click Choose to create a trace document. 创建跟踪文件

Activity Monitor 性能分析模板包括Activity Monitor 工具,并且预先被配置显示系统加载和虚拟内存的大小。我们能够使用其他内存相关的统计来并使用监控面板来查看。

To enable display of Activity Monitor memory statistics 显示监控的内存统计

1:Press Command-1 to display the record settings area of the inspector pane (if it’s not already opened). 如果录制设置部分没有打开,可以使用 Command-1 进行打开
2:In the statistics to list area of the inspector, select the memory-related statistics you want to see.The following memory-related statistics are available for display:在监控面板区域的statistics to list 可以选择与内存相关的统计选项,下面是可以获取的显示:

Physical Memory Wired The amount of memory being used by the operating system  操作系统所使用的内存情况
Physical Memory Active  —The amount of memory being used by processes other than the operating system.   对比操作系统其它进程使用的内存数量
Physical Memory Inactive—The amount of memory that was recently released 最近释放的内存数量
Physical Memory Used—The total amount of memory that was used while performing the sample 当执行采样时,全部使用的内存数量
Physical Memory Free—The amount of available memory.  可获得的内存数量
Total VM Size—The total size of virtual memory.  虚拟内存的全部数量
VM Page In Bytes—The amount of memory calls sent to RAM. 内存调用发送到RAM的数量
VM Page Out Bytes—The amount of memory calls sent to disk.  内存调用发送到磁盘的数量
VM Swap Used—The amount of memory called from disk. 磁盘内存被调用的梳理

3:In the system statistics area, select the memory-related statistics you want to see. If desired, click the shape or color controls to change how the statistics are represented in the timeline.  在系统统计区域,选择我们想统计的与内存相关的选项,点击shape或Color可以改变在timeline的统计显示

To gather overall memory usage detail  为了获取整体内存的使用详情

1:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar or press Command-R to begin recording. 点击录制按钮开始记录
2:Use your app normally. 正常使用APP
      As the Activity Monitor instrument profiles your app, it performs samples at periodic intervals and displays the results in the timeline and detail panes.
3:Click the Stop button (image: ../Art/inline_stop_button_2x.png) in the toolbar or press Command-R again when you’re ready to stop recording. 停止记录

建议:当APP被profile了,选择在timeline中插入旗帜(flag)来指示这里有执行具体的任务。例如:在创建新的document之前添加flag,当我们探索分析获取的数据,可以直接到flag去进行分析。

时间面板(timeline pane)提供了图示显示收集的数据。滚动timeline能够理解APP和其他运行系统上进程是如何使用内存的。对于跟多详情,可以进一步理解详情面板。

To view summarized memory usage by sample

1:Click the Activity Monitor track in the timeline.  在timeline点击Activity Monitor进行跟踪
2:Choose Samples from the detail type list in the navigation bar of the detail pane. 选择采样

To view detailed memory usage by a process at a specific point during profiling

1:Click the Activity Monitor track in the timeline  在timeline点击Activity Monitor进行跟踪
2:Press Command-2 to display the display settings area of the inspector pane (if it’s not already opened). 打开详情面板
3:In the process detail tracking area of the inspector, click “Track inspection head” to configure the detail pane to display results based on the currently selected area in the timeline, rather than the latest sample performed. 在监控面板中的过程详情跟踪,点击“Track inspection head”来配置详情面板,基于当前选中的timeline,而不是最新执行的采样
4:Select a point in the timeline to view the statistics that were captured at that time. 选中timeline中的点查看数据
5:View memory usage statistics in the detail pane.  在详情面板看内存使用统计

Use the detail type list to toggle between a summary view of the collected data by process or a parent child view.

Find Abandoned Memory 

Allocations 性能模板使用Allocation和VM Tracker工具来测量APP整体和虚拟内存的使用。但是,为了查找未使用的内存(abandoned memory)即内存已经被分配但是并没有使用,应该集中于Allocations工具,该工具测量堆内存的使用并且追踪分配,包括由类分配的具体对象。

由于未使用的内存是仍然在APP的某个地方被引用,所以Instrument不能够确定它的重要性。为了找到该部分未使用的内存,使用generational analysis确保在重复执行相同的操作时,内存不会持续增长。例如:结束和开始一个新的游戏,打开和关闭Window,创建和删除联系,所有的操作都未设置优先权,得到稳定的内存状态。大量的循环操作不应该导致未知的内存增加。Instrument能够帮助我们找到相关内存增长对应的具体对象,以至于我们能够释放这些对象减低APP的内存足迹。

To look for abandoned memory in your app

1:Launch Instruments.           启动Launch Instruments.
2:In the profiling template selection dialog that appears, click Allocations.     在出现的性能分析框中选中 Allocations
3:Choose your device and app from the target device and process lists.        选中设备和APP
4:Click Choose to create a trace document.                 点击Choose创建trace document
5:Click the Allocations instrument in the timeline pane.                在时间面板中点击Allocations工具
6:Press Command-2 to show the display settings area of the inspector pane.This gives you quick access to the Mark Generation button, which you use to flag different generations of actions as you profile your app.             点击 Command-2 显示设置区域,或者直接在最右侧面板中点击中间齿轮⚙按钮,这里我们可以访问Mark Generation按钮,可以用来标记不同的 generations
7:Click the Record button () in the toolbar (or press Command-R) to begin recording.               点击录制按钮开始记录
8:Perform a short sequence of repeatable actions in your app.In order to accurately generate trends, this should be a set of actions that starts and finishes with the app in the same state.             在APP中执行一系列重复的行为,来获取准确的数据
9:Click the Mark Generation button in the inspector pane.             点击右侧监督面板中,设置区域下的Mark Generation按钮,实现快照。快照成功显示在详情面板
     A flag appears in the track pane to identify the generation.            一个红旗标志将显示在轨道上标识generation
     A list of generations you’ve marked is shown in the detail pane. Each generation includes a list of allocations that has occurred since the previous generation. 
    详情面板会显示一系列的generation,每一个generation都包括从之前generation开始的一系列分配。
      You can also mark generations after you’re done recording by dragging the inspection head in the track pane’s timeline to the desired location and clicking Mark Generation.        我们也可以在录制结束之后 mark generations,通过拖动检测头到时间轴面板中需要的位置并进行Mark Generation。
10:Perform steps 8 and 9 several times while monitoring the detail pane until you see whether memory is growing without limit.         当监控详情面板执行步骤8和9几次一直到你看到内存是否无限制地增长。
11:Click the Stop button () in the toolbar (or press Command-R again) when you’re ready to stop recording.         点击停止按钮停止记录
12:Scan through the generations in the detail pane and find one that seems representative of repeated memory growth.          扫描详情面板的generations 并找到一个反复内存增长的
      The Growth and # Persistent columns tell you how much additional memory and how many allocations have occurred since the previous generation. If your app returns to its original state after an operation, you shouldn’t expect there to be growth from generation to generation.          Growth 和 # Persistent 专栏将告诉我们对比之前的快照又增加了多少内存,又有多少被分配。如果在执行一次操作之后返回了最初的状态,那么这里将不会有内存的增长。
13:Click the disclosure triangle () of a generation to display new objects that have been allocated since the prior generation.          点击三角符号来显示已经被分配的新对象
14:Look for objects that are persisting. If you identify one, click the disclosure triangle () to display its instances.       寻找持久化的对象,如果确认了,点击三角显示对应的实例
15:Select an object instance.      选择对象实例
16:Press Command-3 to display a stack trace for the selected instance in the extended detail area of the inspector.     按压Command-3在监督的扩展面板区域中显示选中实例的轨道踪迹
This stack trace provides the complete list of method calls responsible allocating the instance.      该轨道跟踪提供了完整的创建实例的方法列表
17:Click the Collapse button () in the extended detail area to hide system calls in the stack trace. This makes it easier to locate your app’s methods.
Calls made by your app are colored black and preceded by a user code icon ().       点击()按钮隐藏系统在轨道踪迹中的的调用,这样更加容易定位APP的方法
18:Double-click a method in the stack trace to display its code in Instruments.        双击在堆栈轨道中现实的方法
19:Click the Xcode button () at the top of the detail pane to open the code in Xcode for editing.         点击顶部的Xcode按钮打开工程进行编辑
20:Determine whether the allocation is useful. If it’s not, it’s abandoned memory that needs to be resolved.        确定分配是否有用,如果没有用,那么背抛弃的内存需要被处理


Find Memory Leaks

Leaks分析模板使用了Allocations和Leaks工具来测量APP整体的内存使用并确定泄露-内存已分配给对象但是对象不在被引用和获取。

注意:如果Instruments有权利获取APP中的源码信息,那么内存泄露会通过类名报道。否则,内存泄露将使用内存地址显示对应的部分,比如:Malloc-size.为了确保Instruments有权利获取APP代码信息,在Xcode中初始化profiling(seeProfile from Xcode) 或者 配置符号映射文件(configure a symbol mappings file see Map Data to Source Code).

To look for memory leaks

1:Launch Instruments.

2:In the profiling template selection dialog that appears, click Leaks.

3:Choose your device and app from the target device and process lists.

4:Click Choose to create a trace document.

5:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.

6:Use your app normally.

7:Watch the Leaks instrument in the timeline pane for leaks. A leak appears as a red bar.   在时间轴面板观察Leaks工具,如果有内存泄露将会显示红色小状态图形

8:Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.  点击 Leaks instrument,在详情面板查看相关信息

9:Choose Call Tree from the detail type list in the navigation bar of the detail pane.  在详情面板的高航条中选择 Call Tree

     A list of method calls related to any detected leaks is displayed.   容易和内存泄露被检测到都会显示在详情面板中

10:Press Command-2 to show the display settings area of the inspector pane.

11:Under the Call Tree display settings, select Invert Call Tree and Hide System Libraries.    在Call Tree显示设置区域,选中 Invert Call Tree 和 Hide System Libraries

The most recent method calls are shown first. It also helps narrow down the list of method calls to ones made by your app. Method calls made by your app are colored black and preceded by a user code icon (image: ../Art/inline_usercode_icon_2x.png).   最近的方法调用将首先显示。它还有助于缩小的列表方法调用的应用程序

12:In the call tree, select a method call you want to investigate.   在 call tree中,选择我们想调查的方法

13:Press Command-3 to display a stack trace for the selected method call in the extended detail area of the inspector.  Command-3在监督区域的扩展面板中查看选中方法的轨道踪迹

14:Double-click the method call in the stack trace to display its code in Instruments. 双击方法定位代码

15:Click the Xcode button (image: ../Art/inline_xcode_2x.png) at the top of the detail pane to open the code in Xcode for review and editing.  点击顶部的Xcode按钮调到对应代码部分进行编辑和修改。


在打开Xcode查看代码所创建的内存泄露,内存泄露可能仍然不清楚,即不是那么明显,Leaks instrument允许我们使用详情面板中的 Cycles & Roots查看循环泄露


To investigate a leaked object using cycles and roots 使用cycles and roots 查找泄露对象
1:Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.    点击Leaks instrument 显示相关的内存泄露信息
2:Choose Cycles & Roots from the detail type list in the navigation bar of the detail pane. 选择详情面板导航条上的Cycles & Roots
A list of leaked objects by cycle is displayed.  可以看到循环引用的对象,下图是两个对象发生循环引用之后显示的图表:



3:Select an object you want to investigate.  选择想要调查的对象
4:If available, view the object graph for the object. 如果可以,查看对象的图表
5:Click the focus arrow (image: ../Art/icon_detail_pane_gotoarrow_2x.png) next to the object to display the memory history of the object in the detail pane, along with corresponding reference counts and method calls.   
6:Press Command-3 to display a stack trace for the selected object in the extended detail area of the inspector.
7:Click the Collapse button (image: ../Art/inline_hidesystemcalls_button_2x.png) in the extended detail area to hide system calls in the stack trace. This makes it easier to locate your app’s methods.
8:Double-click a method in the stack trace to display its code in Instruments.
9:Click the Xcode button (image: ../Art/inline_xcode_2x.png) at the top of the detail pane to open the code in Xcode for review and editing.

To investigate a leak using the call tree 使用call tree查找泄露
1:Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.
2:Select Call Tree from the detail type list in the navigation bar of the detail pane.A list of method calls related to any detected leaks is displayed.
3:Press Command-2 to show the display settings area in the inspector pane.
4:Under the Call Tree display settings, select Invert Call Tree and Hide System Libraries.
The most recent method calls are shown first. It also helps narrow down the list of method calls to ones made by your app. Method calls made by your app are colored black and preceded by a user code icon ().
5:Select a method call you want to investigate.
6:Press Command-3 to display a stack trace for the selected method call in the extended detail area of the inspector.
7:Double-click the method call in the stack trace to display its code in Instruments.
8:Click the Xcode button () at the top of the detail pane to open the code in Xcode for review and editing.

虽然Instruments能够帮助我们检测内存泄露,但是我们仍然需要仔细查看相关的内存历史和代码来确定并解决问题,下面是一些常常导致泄露的情形:
1:Retain has been called on an object without a corresponding release call when the object is no longer referenced. 循环是当一个对象不再被引用的时候,被没有进行对应的释放
2:An object has been allocated and initialized with APIs that don’t cause the object to autorelease.  对象有被创建和初始化但是并没有被释放
3:If a leak isn’t an object, you may be calling an API that assumes ownership of a malloc-created memory block, and you are missing a corresponding call to free()


Find Zombies

Zombies性能分析模板使用Allocations工具测量APP的内存使用,集中监测“zombie”对象,该对象是已经被释放之后并不存在。在这个模板中,Allocations工具已经配置了能够检测 NSZombie检测。这是调试模型(debug mode),需要设置环境变量 NSZombieEnabled 为true,指引编译器使用任意对象替代NSZombie类型的对象,该对象已经被释放并且引用计数为0(即对象不存在)。如果向这些已经被释放的对象发送信息,这些对象将被标记,当APP崩溃,记录停止,Zombie信息对话显示。我们能够检测Zombie对象的历史retain和release,确定问题发生的准确位置。

注意:Zombies模板由于改变了环境,以至于被释放的对象从未被释放所以导致持久化内存增长。这是预期的行为,这也意味除了标志zombie和内存地址,其它由Allocation工具提供的数据应该被忽略,我们不应该添加Leaks工具到Zombies模板,这将导致不准确的结果,对于iOS APP,使用Zombies模板需要使用iOS模拟器而不是真机。

To look for zombies in your app

1:Launch Instruments.
2:In the profiling template selection dialog that appears, click Zombies.
3:Choose your device and app from the target device and process lists.
4:Click Choose to create a trace document.
5:Click the Record button (image: ../Art/inline_record_button_2x.png) in the toolbar (or press Command-R) to begin recording.
6:Use your app normally. 
     如果调用了已经被释放的对象,那么将会在时间轴面板中插入标记并且 Zombie 信息对话框将显示,指示信息被发送给了再具体内存地址已经释放的对象。如果关闭Zombie 信息对话框,点击标识能够重新打开。
7:Click the focus arrow (image: ../Art/icon_detail_pane_gotoarrow_2x.png) next to the zombie’s memory address to display the memory history of the zombie object in the detail pane, along with corresponding reference counts and method calls. 
8:Select the Zombie event (or any other event you want to investigate) in the detail pane.
9:Press Command-3 to display a stack trace for the selected event in the extended detail area of the inspector.
10:Click the Collapse button (image: ../Art/inline_hidesystemcalls_button_2x.png) in the extended detail area to hide system calls in the stack trace. This makes it easier to locate your app’s methods.Calls made by your app are colored black and preceded by a user code icon (image: ../Art/inline_usercode_icon_2x.png).
11:Double-click a method in the stack trace to display its code in Instruments.
12:Click the Xcode button (image: ../Art/inline_xcode_2x.png) at the top of the detail pane to open the code in Xcode for editing.

虽然Instruments能够帮助我们检测zombies,,但是我们仍然需要仔细查看相关的内存历史和代码来确定并解决问题,下面是一些常常导致泄露的情形:
1:An object has already been released (or autoreleased), and your app tries to release it again. 对象已经被释放掉,APP尝试再release
2:An object hasn’t been retained when it should have been. 当对象需要被retained的时候并没有被retained
3:Some other call is made to an object after it has been released. 在对象释放之后,被再次使用


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值