I am trying to understand how to find memory issues using Instruments and fix them. I’m using the BMI project in the Big Nerd Ranch Guide Objective-C that creates an artificial memory leak for this purpose.
These are the steps I’m following to run Instruments.
- I start Instruments with “Allocations” and “Leaks” tools and press the record button.
- Then I run the program.
- 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
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 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 in spite of the “strong” pointer from Assets class to the Employee class)
In the row where Category column = Asset, I see #Persistent column = 12 and under #Persistent bytes = 384. Once more, the value 10 does not match the total # of Asset objects allocated and leaked (both 10 in this case).
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 be 10 Asset objects and 7 Employee objects and nothing else?
Hence, I’m either using Instruments tool wrong (note: the first time I ran Instruments record button, the code was slightly different, I don’t know if this causes any issues) or I’m misinterpreting the data it is showing. Either way, any pointers will greatly help.
Complete source code is at this github location github.com/smartiothome/BMI