Figure 11 tracks another double free, this time caused by the demoDoubleFree:
action method from Listing Three. The last two spikes on the Track pane for the Allocations module mark the point before the app crashes with an EXC_BAD_ACCESS
. Selecting these two spikes reveals the object types created before the crash. The type you want to examine is Malloc-16
.
Figure 11.
A drill-down on Malloc-16
lists the pointers one of which is 0x4b52410
, the pointer to the tFoo
data structure. A drill-down on 0x4b52410
displays its allocation history. It reveals tFoo
getting a reference count of 1 when first allocated in demoDoubleFree:
. But doubleFree:
disposes of tFoo
with a call to free()
, decreasing the count to 0. So when demoDoubleFree:
tries to dispose of tFoo
with free()
as well, it will raise the aforementioned exception. Correcting this problem requires removing one of the calls to free()
.
Figure 12 shows a memory hog in progress.
Figure 12.
The routine is the demoMemoryHog:
action method (Listing Ten) creating an array of NSNumber
objects. The Track pane showed a large swatch of allocations. Selecting the swatch displays the CFNumber
objects, all 10,240 instances, taking up a total of 160 KB of memory. The graph on the Allocations (Net/Overall) column for CFNumber
was a solid red.
Listing Ten
- (IBAction) demoMemoryHog:(id)aSrc { NSNumber *tNum; NSInteger tIdx, tMax, tInt; // set the maximum array size tMax = 1024 * 10; for (tIdx = 0; tIdx < tMax; tIdx++) { // create a test entry tInt = rand(); tNum = [NSNumber numberWithInt:tInt]; // update the test array [objcArray addObject:tNum]; } }
A drill-down lists the addresses of each object (not shown); another drill-down reveals the allocation history for one CFNumber
object (0x4d0f6f0
). This memory hog is not serious, because the CFNumber
objects are all marked for autorelease. But unless you try to restrict array growth, you will end up with a low-memory condition.
Figure 13 is another memory hog in progress. The routine is the demoMemoryHog:
method from Listing Eight. The memory hog appears as a red spike on the Track pane next to the Leaks module. Selecting the spike reveals the 1055 instances of Malloc-16
type still resident in memory. Again, the graph on the Allocations (Net/Overall) column was a solid red for that type.
Figure 13.
A drill-down on Malloc-16
lists the pointer addresses of the instances and the routine that made them. And a drill-down on a pointer (shown here as 0x4b2d9a0
) provides details for one instance. Memory consumption is small, a mere 16.48 KB. But if the linked-list grew unimpeded, it could lead to a low-memory condition.
Conclusion
In this article, I showed four common memory problems that can affect an iOS app, how to identify some these problems with Xcode's static analysis feature, and how to prepare an Instruments session and attach it to an iOS app process, as well as how to gather process data and examine that data for memory problems. Ensuring your code is frugal with memory usage always pays off when it comes to developing apps for iOS.
References
Memory Usage Performance Guidelines [PDF]. Mac Developer Library.
Instruments User Guide. [PDF] Mac Developer Library.
Related Reading
Handling Errors in iOS and OS X with Cocoa
Understanding Core Data on iOS
Automatic Reference Counting on iOS
José Cruz is a freelance engineering writer based in British Columbia. He frequently contributes articles to MacTech, REALStudio Developer, and Dr. Dobb's.