Channels ▼


Debugging Memory in iOS Apps

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.


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.


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.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.