Skip to main content

Instruments: Finding Where You Allocate Memory

·2 mins

I have seen several questions recently on Stack Overflow from people using the Allocations instrument in Instruments. When looking through the information the Allocations instrument provides, they have difficulty finding the areas of their code responsible for the memory allocations. Answering questions on this topic several times on Stack Overflow tells me it’s time to make a post about finding where your application allocates memory.

There are three views in Instruments that can help you find the parts of your code that are allocating large amounts of memory: the extended detail view, the call tree view, and the source view.

Extended Detail View #

When you select a memory allocation from the detail view, the extended detail view shows the call stack for that memory allocation. Examining the call stack can help you find the function in your code that makes the memory allocation. To open the extended detail view, choose View > Extended Detail or click the right button in the View group in the toolbar.

Call Tree View #

The Allocations instrument initially shows the table view, which tells you memory allocation statistics. Switching to the call tree view can help you find where your application allocates memory. Use the jump bar to switch to the call tree view.

To the left of the call tree view is the Call Tree collection of checkboxes. Selecting the Invert Call Tree and Hide System Libraries checkboxes can help you quickly find your code in the call tree view.

Source View #

Double-clicking one of your functions from the extended detail view’s call stack or the call tree view opens the source view. The source view highlights the lines of code in the function that are allocating memory and tells you the amount of memory allocated. More information on the source view is available in the Instruments Source View post, but keep in mind that some parts of the post do not apply in Xcode 4.