Information displayed by Allocations/leaks tools in Xcode Instruments appears incorrect

我的未来我决定 提交于 2019-12-11 10:22:23

问题


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.

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

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

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)

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.

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

  1. I start Instruments with "Allocations" and "Leaks" tools and press the record button.
  2. Then I run the program.
  3. The program has a 30s sleep at the end so that it does not end immediately.

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. Under Allocations tool, detailed view

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

      • The value 8 shown does not match either the total # of Employee objects allocated (10) or the number of Employee objects leaked (7) (since 3 Emplyee objects had no assets linked to them they get released correctly)
    • 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)

  2. 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.

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.


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/27682161/information-displayed-by-allocations-leaks-tools-in-xcode-instruments-appears-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!