Find Memory Leaks

本文介绍如何使用Instruments工具中的泄漏分析模板来检测iOS应用中的内存泄漏。通过启动Instruments,选择泄漏模板并运行应用,可以发现内存泄漏并在Xcode中审查相关代码。文章还提供了常见内存泄漏原因的分析。

查找内存泄漏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 Profile from Xcode) or 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. 单击“记录”按钮在工具栏(或按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

<think>好的,我需要帮助用户确认electron-builder是否安装成功,并解释安装过程中出现的警告信息的含义。根据用户提供的引用内容,我需要结合这些信息来给出准确的回答。 首先,用户提到全局安装electron-builder,所以我要先说明如何验证全局安装是否成功。根据引用[2],安装命令是`npm install -g electron-builder`。验证的方法通常是检查版本号,例如运行`electron-builder --version`,如果有输出版本号,说明安装成功。 接下来是关于警告信息的处理。引用[3]提到electron-builder没有捆绑Python文件,但可能需要一些构建工具。用户安装时可能会遇到与Python或构建工具相关的警告。比如,如果系统没有安装Python或者node-gyp,可能会出现警告。例如,node-gyp需要Python和构建工具,所以如果这些依赖缺失,npm会发出警告。此时,用户可能需要安装windows-build-tools(引用[3]中的可选依赖)或者使用pyenv-win来管理Python环境。 另外,引用[1]指出可以使用cnpm代替npm来安装,可能有助于解决网络问题导致的安装警告。如果用户遇到网络问题,可以尝试切换镜像源或使用cnpm。 需要提醒用户,警告信息不一定意味着安装失败,但可能会影响后续的打包过程。例如,缺少构建工具可能导致无法编译原生模块。因此,建议用户根据警告内容安装相应的依赖,如Python、Visual Studio Build Tools等。 总结步骤:1. 验证安装;2. 分析警告内容;3. 根据警告安装必要依赖;4. 提供额外建议如使用cnpm或检查网络。同时,引用相关的引用内容来支持回答,比如引用[2]和[3]中的安装方法和依赖项说明。</think>要确认electron-builder是否安装成功及处理警告信息,请按以下步骤操作: --- ### 1. **验证全局安装是否成功** **方法**:在终端执行命令: ```bash electron-builder --version ``` - 若输出版本号(例如`22.10.5`),则安装成功[^2] - 若提示`command not found`,需重新安装或检查环境变量 --- ### 2. **常见警告信息及处理方法** #### (1) **Python相关警告** **示例**:`gyp ERR! find Python` 或 `python not found` - **原因**:electron-builder依赖`node-gyp`编译原生模块,而`node-gyp`需要Python环境[^3] - **解决方案**: - **Windows**:安装`windows-build-tools`(包含Python和VS构建工具) ```bash npm install --global windows-build-tools ``` - **Mac/Linux**:通过包管理器安装Python(如`brew install python`) #### (2) **构建工具缺失警告** **示例**:`MSBuild tools not found` - **原因**:缺少C++编译环境(如Visual Studio的构建工具) - **解决方案**: - **Windows**:安装[Visual Studio Build Tools](https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/),勾选“C++桌面开发” - **其他系统**:安装`gcc`/`make`等基础编译工具 #### (3) **网络或权限警告** **示例**:`ETIMEDOUT` 或 `EACCES` - **原因**:网络问题或权限不足 - **解决方案**: - 使用`cnpm`替代npm(引用国内镜像)[^1] ```bash cnpm install -g electron-builder ``` - 以管理员身份运行终端(Linux/macOS加`sudo`) --- ### 3. **补充建议** - 检查`node-gyp`是否安装:`npm list -g node-gyp` - 若需打包含Python依赖的项目,需单独配置环境(如通过`pyenv-win`管理Python版本)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值