博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Find Memory Leaks
阅读量:5746 次
发布时间:2019-06-18

本文共 8189 字,大约阅读时间需要 27 分钟。

查找内存泄漏Find Memory Leaks

 

泄漏分析模板使用分配和泄漏工具来测量应用程序中的一般内存使用情况,并检查已分配给不再引用和可访问的对象的泄漏内存。The 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.

NOTE

如果工具可以访问关于应用程序源代码的信息,则将泄漏报告为类名。否则,泄露的报道作为一个内存地址,如malloc的大小。确保工具访问你的代码的信息,开始分析从Xcode(见文件从Xcode)或配置一个符号映射文件(见地图数据源代码)。If 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 (see ) or configure a symbol mappings file (see ).

寻找内存泄漏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. 单击“记录”按钮在工具栏(或按command-r)开始记录。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.

  8. 单击“时间线”窗格中的“泄漏”工具,在详细窗格中显示与泄漏相关的信息。Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.

  9. 从详细窗格的导航栏中的“详细类型”列表中选择呼叫树。Choose 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.

  10. 记者command-2给检查员的窗格显示设置区。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.

    最新的方法调用首先显示。它也有助于缩小应用程序所生成的方法调用列表。方法调用的应用程序做都是黑色的,之前有一个用户代码图标。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.

  13. 记者command-3显示选定的方法调用在检查员的扩展的详细地区堆栈跟踪。Press Command-3 to display a stack trace for the selected method call in the extended detail area of the inspector.

  14. 双击堆栈跟踪中的方法调用,以在工具中显示其代码。Double-click the method call in the stack trace to display its code in Instruments.

  15. 点击Xcode按钮在细节窗格顶部在Xcode打开代码审查和编辑。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 leaked object using a backtrace
  1. 单击“时间线”窗格中的“泄漏”工具,在详细窗格中显示与泄漏相关的信息。Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.

  2. 从“详细信息窗格”的导航栏中的“详细类型”列表中选择“泄漏”。Choose Leaks from the detail type list in the navigation bar of the detail pane. 

    显示回溯名单泄漏的对象。A list of leaked objects by backtrace is displayed.

    通过回溯查看聚集所有的块分配点的泄漏的泄漏,因为源代码中的一个错误会导致多个运行时泄漏的代码是重复执行。The 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.

  3. 选择要调查的对象。Select an object you want to investigate.

  4. 点击箭头旁边的对象的内存地址显示在详细信息窗格中的对象的历史记忆,以及相应的引用计数和方法调用。Click the focus arrow (image: ../Art/icon_detail_pane_gotoarrow_2x.png) 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.

  5. 记者command-3显示在检查员的扩展的详细地区选定对象的堆栈跟踪。Press Command-3 to display a stack trace for the selected object in the extended detail area of the inspector.

  6. 单击折叠按钮在扩展的详细地区隐藏在堆栈跟踪的系统调用。这样可以更容易地找到应用程序的方法。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.

  7. 双击堆栈跟踪中的一个方法,以在工具中显示其代码。Double-click a method in the stack trace to display its code in Instruments.

  8. 点击Xcode按钮在细节窗格顶部在Xcode打开代码审查和编辑。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看到代码是创造泄漏后,泄漏的原因仍不清楚。漏洞工具允许您通过在详细窗格中使用循环根选项来查看创建漏洞的循环。After 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.

使用循环和根来调查泄漏的对象To investigate a leaked object using cycles and roots
  1. 单击“时间线”窗格中的“泄漏”工具,在详细窗格中显示与泄漏相关的信息。Click the Leaks instrument in the timeline pane to display leak-related information in the detail pane.

  2. 从详细窗格的导航栏中的“详细类型”列表中选择“循环和根”列表。Choose Cycles & Roots from the detail type list in the navigation bar of the detail pane.

    通过循环显示泄漏对象列表。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. 记者command-3显示在检查员的扩展的详细地区选定对象的堆栈跟踪。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. 点击Xcode按钮在细节窗格顶部在Xcode打开代码审查和编辑。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.

TIP

对于使用自动引用计数(ARC)的应用程序,泄漏对象的对象图视图有助于保留循环的诊断。通常,这些问题的解决方法是用弱关键字在对象图上标记一个up引用。For 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.

使用调用树来调查漏洞To investigate a leak using the 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. 记者command-2显示督察窗格显示设置区。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 (image: ../Art/inline_usercode_icon_2x.png).

  5. 选择要调查的方法调用。Select a method call you want to investigate.

  6. 记者command-3显示选定的方法调用在检查员的扩展的详细地区堆栈跟踪。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. 点击Xcode按钮在细节窗格顶部在Xcode打开代码审查和编辑。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.

虽然工具可以帮助您检测内存泄漏,但您仍然需要仔细查看相关的内存历史和代码,以便识别和解决问题。以下场景是常见的泄漏原因:Although Instruments can help you detect memory leaks, 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 leaks:

  • 当对象不再引用时,保留在没有相应释放调用的对象上被调用。Retain has been called on an object without a corresponding release call when the object is no longer referenced.

  • 一个对象被分配和初始化API,不使对象自动释放。An object has been allocated and initialized with APIs that don’t cause the object to autorelease.

  • 如果泄漏并不是一个对象,你可以调用一个API,假设创建内存块malloc的所有权,你错过了一个相应的调用free()。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().

转载于:https://www.cnblogs.com/zyingn/p/Find_Memory_Leaks.html

你可能感兴趣的文章
像黑客一样使用Linux命令行(转载)
查看>>
让进程在后台可靠运行的几种方法
查看>>
Linux学习之:yum程序包管理命令
查看>>
罗森伯格2015年度HDCS中国分销商大会圆满举办
查看>>
正则表达式口诀
查看>>
VIM设置
查看>>
【帧中继】帧中继基础配置
查看>>
苹果手机未来可感知用户需求:感应系统专利曝光
查看>>
Highcharts中Legend动态显示点值
查看>>
Centos7安装haproxy
查看>>
股票K线基本知识
查看>>
结合bgp路由反射器和internet访问的mpls *** 实验
查看>>
MongoDB笔记五——插入操作
查看>>
我的友情链接
查看>>
利用CSS切割图片技术来动态显示图片
查看>>
保留道路
查看>>
bash脚本示例1
查看>>
项目修炼之路(5)高并发下优化Redis缓存效率
查看>>
大型网站架构之分布式消息队列
查看>>
企业应用系统驱动企业业务变革
查看>>