发现被遗弃的记忆Find Abandoned Memory
配置分析模板使用分配和VM跟踪工具来测量应用程序中的一般内存和虚拟内存使用量。但是,为了跟踪已分配但不需要的废弃内存,请严格地关注分配工具。该工具测量堆内存使用情况并跟踪分配,包括按类指定特定对象分配。The Allocations profiling template uses the Allocations and VM Tracker instruments to measure general and virtual memory usage in your app. However, to track down abandoned memory that’s been allocated but isn’t needed again, focus strictly on the Allocations instrument. This instrument measures heap memory usage and tracks allocations, including specific object allocations by class.
因为废弃的内存仍然被应用程序所引用,所以仪器不能确定它的重要性。为了找到被抛弃的内存,使用代分析来确保在重复执行同一组操作时内存不会继续增长。例如,结束和开始一个新游戏,打开和关闭一个窗口,创建和删除联系人,或设置和复位的偏好都应该返回你的应用程序在操作前一个稳定的内存状态。多次循环这样的操作不应导致意外或无限制的记忆增长。工具可以帮助您将内存增长的周期与特定的对象分配相关联,这样您就可以释放它们并减少应用程序的内存占用。Because abandoned memory is still referenced by your app, Instruments can’t determine its importance. To find abandoned memory, use generational analysis to ensure that memory does not continue to grow while repeatedly performing the same set of operations. For example, ending and starting a new game, opening and closing a window, creating and deleting a contact, or setting and unsetting a preference are all operations that should conceptually return your app to a previous and stable memory state. Extensive cycling through such operations many times should not result in unexpected or unrestrained memory growth. Instruments helps you correlate periods of memory growth with specific object allocations, so you can release them and reduce your app’s memory footprint.
-
运载工具。Launch Instruments.
-
在出现的分析模板选择对话框中,单击“分配”。In the profiling template selection dialog that appears, click Allocations.
-
从目标设备和流程列表中选择您的设备和应用程序。Choose your device and app from the target device and process lists.
-
单击“选择”创建跟踪文档。Click Choose to create a trace document.
-
单击时间线窗格中的分配工具。Click the Allocations instrument in the timeline pane.
-
记者command-2给检查员的窗格显示设置区。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-r)开始记录。Click the Record button (
) in the toolbar (or press Command-R) to begin recording.
-
在应用程序中执行一系列可重复的动作。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.
-
单击“检查窗格”中的“标记生成”按钮。Click the Mark Generation button in the inspector pane.
在“跟踪”窗格中显示一个标志以标识生成。A flag appears in the track pane to identify the 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.
您还可以通过在跟踪窗格的时间线中拖动检查头到所需的位置并单击标记生成来完成标记后的几代。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.
-
在监视细节窗格时,执行步骤8和9,直到您看到内存是否无限制地增长。Perform steps 8 and 9 several times while monitoring the detail pane until you see whether memory is growing without limit.
IMPORTANT
在最初的几次迭代中,由于缓存,可能会出现额外的分配。因此,为了建立基线,创建一些初始代是很重要的。然后,为真实分析创建额外的一代。During the first few iterations, extra allocations may occur due to caching. Therefore, it is important to create a few initial generations for the purpose of establishing a baseline. Then, create additional generations for true analysis.
-
请单击“停止”按钮在工具栏(或按command-r再次)当你准备停止记录。Click the Stop button (
) in the toolbar (or press Command-R again) when you’re ready to stop recording.
-
在细节窗格中搜索几代,找到一个代表重复内存增长的页面。Scan through the generations in the detail pane and find one that seems representative of repeated memory growth.
生长#持续列告诉你多少额外的内存分配和多少自前代发生。如果你的应用程序在一次操作后恢复到原来的状态,你不应该期望它会一代一代地增长。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.
-
点击右边的三角一代显示已分配自前代的新对象。Click the disclosure triangle (
) of a generation to display new objects that have been allocated since the prior generation.
-
寻找持续存在的对象。如果你找到一个,点击右边的三角来显示它的实例。Look for objects that are persisting. If you identify one, click the disclosure triangle (
) to display its instances.
-
选择对象实例。Select an object instance.
-
记者command-3显示检查员的扩展的详细地区所选实例堆栈跟踪。Press Command-3 to display a stack trace for the selected instance in the extended detail area of the inspector.
这个堆栈跟踪提供了负责分配实例的方法调用的完整列表。This stack trace provides the complete list of method calls responsible allocating the instance.
-
单击折叠按钮在扩展的详细地区隐藏在堆栈跟踪的系统调用。这样可以更容易地找到应用程序的方法。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 (
).
-
双击堆栈跟踪中的一个方法,以在工具中显示其代码。Double-click a method in the stack trace to display its code in Instruments.
-
点击Xcode按钮在细节窗格顶部在Xcode打开代码编辑。Click the Xcode button (
) at the top of the detail pane to open the code in Xcode for editing.
-
确定分配是否有用。如果不是,那就是需要解决的被抛弃的记忆。Determine whether the allocation is useful. If it’s not, it’s abandoned memory that needs to be resolved.