instrument 文档研究

第一章:

文档地址:

https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/index.html#//apple_ref/doc/uid/TP40004652-CH3-SW1

一些备忘录:

 (1)command + I 是快捷键,添加最近的list   command shift + < ——>edict sheme

 (2)有时候那个工具用不了,又退不出,可以用强制操作,将那个工具退出来

 (3)Trace 追溯 panes and areas窗格和地区

一、概要:

1Even small inefficiencies in apps add up across the system, significantly affecting battery life, performance, responsiveness, and temperature.

即使在app小的效能通过系统加起来,也会自发地影响电池寿命,性能。

2 As an app developer, you have an obligation to make sure your app runs as efficiently as possible.

作为一个开发者,你有责任让你的app运行高效。

3Use Instruments to monitor the impact of your app in order to understand how it utilizes resources and identify times when energy usage is high

使用工具来监管你的app的性能,以确保理解这个工具是怎么利用这个资源的,以及识别能量使用够高的时间。

4 Use best practices to improve efficiency in these areas, and strive for your app to be absolutely idle when not responding to user input

使用最佳的实践在这些区域来改善性能,争取让你的app当没有反应用户输入的时候处于完全闲置的状态


Energy Efficiency and the User Experience

文档地址:

https://developer.apple.com/library/content/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/index.html#//apple_ref/doc/uid/TP40013929


1Great battery life. As energy efficiency goes down, so does battery life. Users want all-day battery life on their portable devices.

长的电池寿命:当能量的效能下降的时候,电池的寿命也下降,用户想要全天的电池寿命在他们便携式的设备上。

2Awesome speed. OS X is designed to provide great performance during complex operations—and to make your app fly.

非常快的速度,OSX在复杂操作的时候被设计用来提供非常高效的性能,让你的app性能飞快。

3Responsiveness. Too many resources being consumed at once can result in a laggy interface that’s slow to respond to user input.

高反应,一次太多的资源被消耗能导致一个延迟的接口,这个接口对用户输入反应很慢。

4Cool, quiet system. As more apps use more resources, the system works harder and faster, and the physical temperature of a device gradually rises. When this occurs, the system takes steps to cool down to a more acceptable level. On devices with fans, those fans may become active and audible to the user.

酷的,安静的系统,当更多的apps使用资源的时候,系统工作越来越难,设备的物理温度逐步上升,当这个发生的时候,系统采取步骤降温至一个可接受的水平。

5On devices with fans, those fans may become active and audible to the user.

在设备上面装风扇,这些风扇可能变得活跃的、能听见声音的给用户。

6Consult this gauge(计量器) regularly throughout your app’s testing cycle to assess your app’s energy impact and identify potential problem areas up-front(预先的). If necessary, dive into Instruments for more in-depth analysis.

在你的app测试循环中经常查询这个计量器以便先前接近你的app的能量影响和身份潜力问题区域,如果有必要,投入工具为了更深入的解析。

7Use the Energy Log profiling template to monitor a variety of factors that affect energy usage on an iOS device, including CPU activity, network activity, screen brightness, and more. Identify the areas where usage is highest, and see if you can reduce impact in those areas. 

使用能量检测工具来检测来检测大量的因素,这些因素影响在iOS设备上的能量使用,包括CPU活动,网络活动,屏幕的亮度,或者其他的。指出使用高的区域,看是否你能降低在这些区域的影响。

8For example, you might find opportunities to defer discretionary or network tasks until more energy efficient times, such as when the device is plugged in or on Wi-Fi.

例如,你也许可以找到机会来延迟任意的或者网络任务,以阻止更多高效能量的次数,例如当设备链接wifi的时候

9The Energy Usage Log instrument indicates a level from 0 to 20, indicating how much energy your app is using at any given time. These numbers are subjective. If your app’s energy usage level is occasionally high, it doesn’t necessarily mean that your app has a problem. 

能量使用log工具显示一个从020的水平,显示你的app在任何时间使用了多少的能量,这些数字是主观的,如果你的app的能量使用水平偶尔变高,这也没意味着你的app就有问题。

10Your app may simply require more energy for some of the tasks it performs. For example, it may use the GPS while performing complex network operations. This is valid energy use. Look for spikes or areas of high energy use that are unexpected or that could be performed at more optimal times.

你的app仅仅在一些特定它完成的的任务需要更多的能量,例如,你也许在完成复杂的网络操作时需要用到GPS,这是有效的能量使用,寻找高能量蛛丝马迹或者地区,这些是不被期盼或者那个能被更多合适的时间完成的。

11Without tethering your device to Instruments (either wirelessly or wired), you can log energy-related data under normal use in order to take realistic measurements.

没有用绳子拴住你的设备给工具,你能在正常的使用下打印能量相关的数据,来获取真实的数据。

12With energy logging enabled, your iOS device records energy-related data unobtrusively while the device is used

当能量logging设置为可能的时候,当在使用的时候,你的iOS设备记录能量相关的数据。

13Because logging is efficient, you can log all day. Logging continues even while the device is in sleep mode. However, if the device’s battery drains completely or the iOS device is powered off, the log data is lost.

因为logging过程是高效的,你能全天loglogging即使在进入睡眠模式后也能继续,如果设备的电池完全用完,或者iOS设备没电了,这些log数据就会消失。

14To see the Developer settings, you must provision the device for development and connect it to your Mac while Xcode or Instruments is running. If your device hasn’t been provisioned yet, see Configuring Your Xcode Project for Distribution and Launching your app on Devices. Even after the device has been provisioned, the Developer setting disappears when the device is rebooted or powered off. To restore the setting, reconnect the device to Xcode or Instruments.

为了看到开发的设置,你必须让你的设备能开发,以及当Xcode或者工具正在运行的时候连接这台设备给你的Mac,如果你的设备还没有被供给,可以看这两个文档,即使你的设备已经被供给,当设备消失或者没电的时候,开发者设置消失,为了重修这个设置,链接你的设备给Xcode或者工具。

15Choose File > Import Logged Data from Device.

选择文件,从设备导入你的数据


Profile Your App’s Memory Usage

1Whenever your app creates objects, memory is allocated for them. Traditionally, it has been the app’s job to track these objects and release them when they are no longer needed so the memory can be reallocated for other objects. 

当你的app创建了对象,内存被分配给他们,习惯上,当他们不在需要的时候,追踪这些对象和释放他们是你的app的工作,腾出内存供其他对象使用。

2Automatic Reference Counting (ARC) is a feature that makes things easier by putting the system in charge of memory management. With ARC enabled, the system handles monitoring object allocations and releasing them when appropriate, leaving very little for your app to do.

ARC是使得内存管理更加容易,当ARC可以的时候,系统在适当的时候处理监管对象的创建和释放,只让非常少的交给你的app去做。

3Regardless of how memory is managed, though, even the best app designs can encounter occasional memory issues that are difficult to isolate.

不管内存是怎么管理的,然而,最佳的app设计能遭遇偶然的内存问题,这些内存问题很难隔离。

4Instruments can help by painting a picture of how your app uses memory, making it easier to identify potential problem areas. It can even auto detect certain types of memory problems and flag them for you for analysis. Use instruments to watch for and track the following

内存管理工具能帮助画一幅图,显示你的app是怎么使用内存的,使得更加容易鉴别问题区域,它能甚至自动鉴别一些内存问题的类型,飘红让你分析,使用工具来看和追踪下面的。

5Overall Memory Use. Monitor at a high level how your app uses memory and compare it to the memory usage of other active processes on the system. Look for areas of large or unexpected memory growth.

监听你的app使用内存出现高水平的地方,并且让其他活跃的程序跟他比较,找到大的或者不期盼的内存增长的区域。

6Leaked Memory. This is memory that was allocated at some point, but was never released and is no longer referenced by your app. Since there are no references to it, there’s now no way to release it and the memory can’t be used again.

内存泄漏,这是那些被分配在一些区域,但是从不被释放,也从不被其他app引用的点,因为没有对他的引用,现在没有办法释放它,这块内存就不能被释放。

 For example, suppose you’ve written an app that creates rectangle objects in a drawing, but never releases the objects when the drawing is closed. In this case, your app would leak more and more memory whenever a drawing containing rectangles is closed. To fix the leak, you need to figure out which object isn’t being released, and then update your app to release it at the appropriate time.

例如,假如你写了一个app,这个app在一个绘制中创建矩形对象,但是当绘制结束的时候,这些对象从不被释放,在这个例子中,只要你的包含矩形的绘制被关闭,你的app将会释放越来越多的内存。为了修复这个泄漏,你需要知道那个对象没有被释放,并且在合适的时候释放它。

7Abandoned Memory. This is memory that your app has allocated for some reason, but it’s not needed and won’t be referenced. 

这是由于某种原因,你的app已经分配的内存,但是它不需要,不将被引用。

For example, suppose your app adds images to a cache after they’ve already been cached—using double the memory for the same images. Or, maybe your app maintains an array of objects in case you need to access them later, but you never actually do.

例如,假定你的app将图片缓存后,你又缓存一遍图片,为相同的图片使用了双倍的内存,或者,也许你的app管理了一个对象数组供以后使用,但是你从不使用它。

 Unlike leaked memory, abandoned memory like this is still referenced somewhere in your app. It just serves no purpose. Since it’s still technically valid, it’s more difficult for Instruments to identify and requires more detective work on your part to find

不像内存泄漏,像上面遗弃的内存遗弃的内存仍被引用在你的app,它没有目的的服务,因为它仍是技术上有效的,但是对工具来讲它是更困难找到和识别,需要一些更侦探性的工作来找到。

8Zombies. This is memory that has been released and is no longer needed, but your code still references it somewhere. For example, suppose your app contains an image cache. Once the cache has been cleared, your app shouldn’t attempt to refer to the images that it previously contained. Calls to these nonexistent images are considered zombies—references to objects that are no longer living. 

僵尸对象,这是一块已经被释放并且不被需要的内存,但是你的码扔在某处引用了它,例如,假定你的app包含一个图片缓存,一旦这个缓存已经被清除,你的app不应该尝试再引用那个数组,这个数组包含之前图片的,调用这些不存在的图片被认为是僵尸对象,引用那些不存在的对象。

9Because memory problems can be difficult to find, it’s helpful to take periodic snapshots of your app at various stages in order to look for unexpected or unrestrained memory growth. You can compare snapshots to determine how objects are being allocated and destroyed, and how memory is used over time.

因为内存问题很难被发现,定期的在各种平台查看你的app能让你找到那些意外的或者大量的内存增长,你能比较快照来决定对象是怎么创建的,怎么毁坏的,并且内存随着时间推移是怎么使用的。

10It’s also important to test your app by continuously performing a set of repeatable user actions that cause your app to enter a new state and return to its original state when done. This type of research is known as generational analysis.

它也是非常重要的来测试你的app,通过持续的完成一系列持续性的用户行动,这引起你的app进入一个新的状态,然后当测试完成后进入原始状态,这类型的测试叫做待分析

11 Each time your app enters its new state, a “generation” of objects is created. In most cases, you should expect these objects to be released when your app returns to its original state

每次你的app进入它的新状态,一代对象被创建,大部分例子里,当你的app返回他的原始状态的时候,你应该期盼这些对象被释放。

12For example, if your app opens a window, then any window objects should be deallocated when the window is closed. If they aren’t, then you may have a memory problem.

例如,如果你开了一个窗,当这个窗被关闭的时候,任何窗对象应该被释放,如果不是,你也许就会有一个内存问题。

13 When performing generational analysis, initial generations may exhibit larger than expected memory use due to caching and other initialization processes. Therefore, repeat user actions multiple times in order to gain a truer sense of your app’s behavior.

当完成代际分析的时候,初始的那一代由于缓存和其他初始程序可能比期盼的内存要大,因此,重复用户使用多次以便获得一个更真实的app行为。

Monitor Memory Usage

1Use the Activity Monitor profiling template to track your app’s real and virtual memory use in comparison to other processes running on the system.

使用activity monitor profiling 模板通过比较其他正在系统运行的程序来追踪你的app真实的和虚拟的内存使用情况。

2The Activity Monitor profiling template includes the Activity Monitor instrument, which is preconfigured to display system load and virtual memory size. You can enable other memory-related statistics for viewing via the inspector pane.

活动检测模板包含活动检测文件,这个文件被预配置来展示系统加载和虚拟的内存尺寸,你能看其他相关的内存统计通过检测通道。

3、余下部分没看懂

Find Memory Leaks


To investigate a leaked object using a backtrace

1The Leaks profiling template uses the Allocations and Leaks instruments to measure general memory usage in your app and check for leaks—memory that has been allocated to objects that are no longer referenced and reachable.

Leaks profileing template 使用Allocationsleaks工具来管理你的app的通用的内存使用,并且检查泄漏,是那些分配给对象不再引用或不可到达的内存。

2If Instruments has access to information about your app’s source code, a leak is reported as a class name. Otherwise, a leak is reported as a memory address, such as Malloc-size. To ensure that Instruments has access to information about your code, initiate profiling from Xcode  or configure a symbol mappings file 

如果工具方便接近你的app的源码信息,泄漏就会报导出来是一个类型,否则,一个泄漏就报导为内存地址,例如Malloc-size,为了确保这个工具能接近你的源码信息,从Xcode初始化一个profiling文件,或者配置一个symbol地图文件 

3Launch Instruments.

Watch the Leaks instrument in the timeline pane for leaks. A leak appears as a red bar.

timeline走廊查看泄漏工具的泄漏,一个泄漏将会出现一个红色的bar

4Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.

timeline pane点击泄漏工具来展示泄漏相关的信息

5Choose 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.

从详情走廊的导航栏选择Call Tree ,一系列与任何泄漏的相关方法被展示。

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

按下command -2 展示检查廊道的设置区域。

7Under 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 (

call tree 展示设置里面,选择这两个库,大部分最近的方法首先展示,它也帮你缩小的方法的范围,被你的app调用的方法是黑色的

8Press Command-3 to display a stack trace for the selected method call in the extended detail area of the inspector.

按下command -3 来展示一个stack trace for这个选择的方法call

9Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.

timeline走廊点击泄漏工具来展示泄漏相关的信息

10A list of leaked objects by backtrace is displayed

一系列泄漏的对象是展示

11The Leaks by Backtrace view aggregates all of the leaked blocks by their allocation point, because a single mistake in source code can result in multiple runtime leaks as the code is executed repeatedly.

一个在源码简单的错误因为源码重复的执行就能导致多个运行时泄漏

12Click the focus arrow (此处省略一个图) next to the object’s memory address to display the memory history of the object in the detail pane, along with corresponding reference counts and method calls.

点击靠近下一个对象内存地址的箭头,来展示在细节走廊的对象的历史,以及对应的引用计数和方法调用

13Press Command-3 to display a stack trace for the selected object in the extended detail area of the inspector.

按压command -3 在检查器延伸的区域来展示选择的对象的一个栈的跟踪。

14Click 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.

在延展的区域点击瓦解button,还隐藏在stack trace里面的系统调用,这使得定位方法更加容易

15Double-click a method in the stack trace to display its code in Instruments.

双击在stack trace一个方法来展示工具里面的源码

16Click the Xcode button () at the top of the detail pane to open the code in Xcode for review and editing.

点击类似xcode logo的图表来打开源码,以供重看以及编辑

17After you open Xcode to see the code that is creating the leak, the cause of the leak may still be unclear. The Leaks instrument allows you to see the cycle that is creating the leak by using the Cycles & Roots option in the detail pane.

在你打开Xcode来看引起泄漏的源码时,泄漏的引起也许扔不清晰,泄漏工具通过使用Cycles or Roots选项来允许你查看制造泄漏的循环。

To investigate a leaked object using cycles and roots

通过循环和跟来研究内存泄漏

19Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane

通过在在时间线廊道泄漏工具来展示泄漏相关在详情走廊的详细信息

20Choose Cycles & Roots from the detail type list in the navigation bar of the detail pane.A list of leaked objects by cycle is displayed.

21If available, view the object graph for the object.

如果可能,看对象的对象图表

22Click the focus arrow () next to the object to display the memory history of the object in the detail pane, along with corresponding reference counts and method calls.

点击靠近object的箭头来展示内存历史,以及反应的引用数量和方法calls

23Press Command-3 to display a stack trace for the selected object in the extended detail area of the inspector.

按压command -3 来展示一个stack trace
24Click 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.

extended detail area 点击button隐藏系统调用,这让你更容易定位app方法。

25For apps that use Automatic Reference Counting (ARC), the object graph view of leaked objects aids in the diagnosis of retain cycles. Frequently, the solution to these problems is marking an up-reference in the object graph with the weak keyword.


对于那些使用了arcapps,泄漏的对象图表帮助引用循环的诊断,频繁地,这些问题的解决方案是通过weak标志一个在图表里面的向上引用
investigate a leak using the call tree

基本与上面的类同

If a call is made to a deallocated object, a flag is inserted in the timeline pane and a Zombie Messaged dialog appears, indicating that a message was sent to a deallocated object at a specific memory address.If you close the Zombie Messaged dialog, you can reopen it by clicking the flag.

如果一个调用是一个重新分配的对象,一朵旗帜被插入timeline走廊,一个僵尸消息的dialog出现了,显示一条消息在一个特定的内存区域被发送给一个重新分配的对象,如果你关闭这个僵尸消息会话,你能通过点击这个旗帜重新打开。

26Although Instruments can help you detect zombies, you still need to look carefully through the related memory history and your code in order to identify and resolve the problem. The following scenarios are common causes of zombies:

  • An object has already been released (or autoreleased), and your app tries to release it again.
  • An object hasn’t been retained when it should have been.
  • Some other call is made to an object after it has been released.
  • 尽管工具能帮助你识别僵尸,你扔需要自己查看相关的内存历史和你的码以至于鉴别和解决问题,下面的章节是僵尸的共同的引起:
  • 1、一个对象已经释放或者自动释放,你的app尝试重新释放
  • 2、一个对象没有被引用当他已经被引用的时候,一些其他的调用被变成另一个对象,在那个对象已经释放后。


  • 可以参考的文章:
  • http://www.10tiao.com/html/330/201709/2653579361/1.html
第二章:

1Instruments doesn’t fix problems with your code. It helps you better understand what your code is doing by capturing detailed statistics as your app runs and presenting them to you for analysis

工具没有修复你的码的问题,它帮助你更好理解你的码在做什么,通过俘获详细的统计数据,重现给你统计。

2、Since every app is different, the actual steps for finding and resolving problems vary. Therefore, you must learn how to interpret(翻译) the information Instruments collects by filtering out data you don’t need, and drilling down(下钻)into data that’s relevant to your app. You must then perform some detective(侦探性的) work to correlate(关联) any data you identify to the code in your app so you can make improvements.

因为你的app是不同的,找和解决问题的真实步骤也不同,因此,你必须学会通过过滤你不需要的数据来解释文档收集的信息,下钻那些与你相关的数据,你必须完成一些侦探性的工作来关联你识别的任何数据给你的码,以便你能做些改进。

3、Many instruments capture periodic(周期的) stack traces(堆栈跟踪) as they monitor your app. When you identify a specific data point you want to investigate further, you can go through the stack trace to identify the corresponding code. If you’re profiling an app you have opened in Xcode, you can often view the source code right in Instruments or jump over to Xcode to examine it further.

许多工具当他们监听你的app的时候将俘获很多周期性的stack traces,当你发现你想要深入调查的指定数据的时候,你能经过这个堆追踪来识别对应的数据,如果你描绘一个你在Xcode打开的app,你常常能在调试工具的右边看到源码,或者跳过Xcode来检查深远的地方。

4、The most prominent(最杰出的) portion of a trace document is the timeline pane, which presents a time-based view of the data gathered by each instrument (see Figure 11-1). 

一个追踪文件最杰出的部分是timeline走廊,这个呈现了一个基于时间的视图。

5、This pane makes it easy to spot(认出) trends and potential problem areas in your app at a glance(乍一看).

这个走廊让你很容易认出趋势和潜在的问题

6、 For example, a spike(长钉) in a memory usage graph indicates a time when your app allocated more memory than usual. 

例如,在内存使用图表的一个钉鞋显示一个时间,你的app分配比以往更多的内存

7、This spike might be normal, or it might indicate that your code created more objects or memory buffers than you anticipated

这个spike也许是正常的,或者它显示你的码创建比你参与的更多的对象或者内存

8、An instrument such as Leaks can also identify where your app is not handling memory properly. If the Leaks timeline track is sparsely populated, you know that your app is behaving properly. If the track is not empty, you might want to examine why.

一个工具例如leaks 能识别你的app在哪儿没有正确地处理APP,如果泄漏timeline 

9、If you’ve captured a lot of data with a trace, you can expand or contract the detail presented in the timeline pane, allowing you to focus in on a specific subset(一个热定的子集) of data (see Figure 11-2).

如果你已经俘获了很多追踪数据,你能扩展或者联系呈现在timeline 廊道的详情数据,允许你集中在指定的subset。

10、看到Navigate the Detail Pane这里

可以参考的文章

https://www.raywenderlich.com/166125/instruments-tutorial-swift-getting-started



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值