Xcode Instruments中的“分配/泄漏"工具显示的信息似乎不正确 [英] Information displayed by Allocations/leaks tools in Xcode Instruments appears incorrect

查看:81
本文介绍了Xcode Instruments中的“分配/泄漏"工具显示的信息似乎不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何使用Instruments查找内存问题并进行修复.我正在使用《大书呆子牧场指南》第20章中的项目,为此目的,该项目会造成人为的内存泄漏.

I am trying to understand how to find memory issues using Instruments and fix them. I'm using the project in Chapter 20 of Big Nerd Ranch Guide that creates an artificial memory leak for this purpose.

概述项目,(完整的源代码位于此github位置 https://github.com/smartiothome/BMI )

Overview the project, (complete source code is at this github location https://github.com/smartiothome/BMI)

创建3类人员,雇员(从人员继承)和资产. 3个类的定义在github项目中.但是最重​​要的一点是

Creates 3 classes Person, Employee (that inherits from person) and Assets. Definitions of the 3 classes are in github project. But most important points are

  • Employee类具有一个实例变量,该实例变量指向已分配给员工和
  • 的Assets对象.
  • 资产类具有一个实例变量,该实例变量指向资产对象已分配到的雇员
  • 当从Assets对象到其Employee对象的指针未标记为弱时,此循环关系导致内存泄漏
  • Employee class has an instance variable that points to the Assets object(s) that have been assigned to the employee and
  • Assets class has an instance variable that points back to the Employee to which the Asset object has been assigned
  • This circular relation leads to memory leak when the Pointer from Assets object to its Employee object is not labelled weak

main.m中的代码摘要:基本上,它将创建一个包含10个元素的NSMutableARrays,称为 employees ,其中包含10个Employee对象,然后创建10个Asset对象并将其随机分配给10个Employee对象(因此,一些Employee对象指向0个资产对象,一些指向1个,另一些指向2个资产对象)

Summary of the code in main.m: Basically, it creates an NSMutableARrays with 10 elements, called employees, that has 10 Employee objects It then creates 10 Asset objects and assigns them at random to the 10 Employee objects (Hence, some of the Employee objects point to 0 Asset objects, some to 1 and some to 2 asset objects)

接着,将employees数组设置为nill.已分配资产对象(和所有资产对象)的雇员对象不会释放,因为由于资产对象之间的强"链接返回到雇员"对象,因此引用计数为1. (注意:由于dealloc方法已被NSLog覆盖,因此您会在控制台输出中清楚地看到所有这些内容.)当Asset类中指向保存它的Employee对象的实例变量设置为弱时,所有Employee和Asset当employees数组设置为nill时,将释放对象.

Following this, the employees array is set to nill. The Employee objects which have been assigned an Asset object (and all of the Asset objects) are not released because the reference count is 1 due to the "strong" link between the Asset objects back to the "Employee" object. (Note: You will clearly see all this in the console output because the dealloc method has been overwritten with a NSLog.) When the instance variable in Asset class pointing to the Employee object that holds it is set to weak, all the Employee and Asset objects are released when employees array is set to nill.

这些是我要运行Instruments的步骤.

These are the steps I'm following to run Instruments.

  1. 我使用分配"和泄漏"工具启动工具,然后按记录"按钮.
  2. 然后我运行程序.
  3. 该程序在结束时有30秒钟的睡眠,因此它不会立即结束.

在谈论我需要帮助的实际问题之前,一个常识性问题Q:当程序最终结束时,我是否可以假定操作系统将释放泄漏的内存"中的Employee和Asset对象?

One general knowledge Q before talking about the actual issue I need help with: When the program finally ends, can I assume the Employee and Asset objects in the "leaked memory" will be released by the OS?

现在由于实际问题,我需要帮助

Now to the actual issue I need help with

  1. 分配下的"工具,详细视图

  1. Under Allocations tool, detailed view

  • 在类别"列=员工"的行中,我看到#Persistent列= 8,而#Persistent字节= 384

  • In the row where Category column = Employee, I see #Persistent column = 8 and under #Persistent bytes = 384

  • 显示的值8与分配的Employee对象的总数(10)或泄漏的Employee对象的数量(7)不匹配(因为3个Emplyee对象没有链接到它们的资产,因此它们可以正确释放)

在类别"列=资产的行中,我看到#Persistent列= 12,在#Persistent字节= 384下.再次,显示的值12与分配和泄漏的资产对象总数不匹配(在这种情况下都是10)

In the row where Category column = Asset, I see #Persistent column = 12 and under #Persistent bytes = 384. Once more, the value 12 shown does not match the total # of Asset objects allocated and leaked (both 10 in this case)

在泄漏"工具的详细视图下,我看到泄漏的对象列为6个资产对象,4个雇员对象,4个Malloc对象和4个NSMUtableArray对象.我以为这是说泄漏的对象是10个Asset对象和7个Employee对象,而没有别的.

Under Leaks tool detailed view, I see Leaked objects listed as 6 Asset objects, 4 Employee objects, 4 Malloc objects and 4 NSMUtableArray objects. I was expecting this to say the leaked objects are 10 Asset objects and 7 Employee objects and nothing else.

因此,我或者误解了Instruments所显示的数据,或者我使用了错误的工具(注意:第一次运行Instruments记录按钮时,代码略有不同,这会引起任何问题).任何指针将不胜感激.

Hence, I'm either misinterpreting the data Instruments is showing or I am using the tool wrong (note: the first time I ran Instruments record button, the code was slightly different, does this cause any issues). Any pointers will be greatly appreciated.

推荐答案

为回答您的一般问题,操作系统将在您退出程序时释放泄漏的内存.

To answer your general question, the OS will release the leaked memory when you quit the program.

您不知道有关Instruments报告的详细信息.以泄漏仪器的数据为例.您担心当您预期有10个泄漏的Asset对象时,Instruments会报告6个泄漏的Asset对象. Instruments报告的泄漏资产对象的数量并不像泄漏资产对象的事实那么重要.

You're getting bogged down with the details on what Instruments is reporting. Take the data from your Leaks instrument as an example. You're worried that Instruments is reporting 6 leaked Asset objects when you're expecting 10 leaked Asset objects. The number of leaked Asset objects that Instruments is reporting isn't as important as the fact that you're leaking Asset objects.

当Instruments告诉您内存泄漏时,您想查找发生内存泄漏的位置,以便您修复泄漏.查找内存泄漏位置的最佳方法是切换到调用树视图.使用跳转栏从详细信息视图切换到调用树视图.切换到调用树视图时,将启用跟踪文档窗口右侧的调用树复选框.选中反转调用树和隐藏系统库"复选框将使在调用树视图中查找代码以及查找应用程序在何处分配泄漏的内存变得更加容易.找到您的应用程序在哪里分配泄漏的内存,将使您更容易找到内存泄漏的来源.

When Instruments tells you that you have memory leaks, you want to find where the memory leak is occurring so you can fix the leak. The best way to find where you're leaking memory is to switch to the call tree view. Use the jump bar to switch from the detail view to the call tree view. When you switch to the call tree view, the Call Tree checkboxes on the right side of the trace document window will be enabled. Selecting the Invert Call Tree and Hide System Libraries checkboxes will make it easier to find your code in the call tree view and find where your app is allocating the leaked memory. Finding where your app is allocating the leaked memory will make it easier for you to find the source of the memory leak.

这篇关于Xcode Instruments中的“分配/泄漏"工具显示的信息似乎不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆