February 16th, 2016
I couldn’t cover everything about Instruments in my Getting Started with Instruments article. Having gotten the basics out of the way, I can now write shorter, more focused articles on Instruments. This is the first of those articles, showing how to measure heap memory growth using the Allocations instrument.
Measuring heap memory growth with Instruments entails the following high-level steps:
Translating these steps to Instruments is mostly straightforward. Click the Record button to start recording. Click the Pause button to pause and resume recording. The only step that could be tough is figuring out how to take a snapshot. To take a snapshot, click the Mark Generation button, which is in the Display Settings section.
When you click the Mark Generation button, the detail view switches to the Generations section. The Generations section has a listing for each snapshot you’ve taken. When you take a snapshot, Instruments gives it a generic name like Generation A. You can give a snapshot a more meaningful name by selecting it and pressing the Return key.
The detail view shows the following information for each snapshot:
The Growth column is the most important column. It tells you the amount of heap growth.
If you move the mouse cursor over the name of a snapshot, a small button with an arrow facing right appears. Clicking that button shows the call tree of memory allocations that occurred during the snapshot period. Use the call tree view to find the areas of your code that allocated the memory.
Next to each snapshot is a disclosure triangle. Clicking the disclosure triangle shows the memory categories in that snapshot. Instruments shows the number of memory allocations and amount of allocated memory for each category.
Most memory categories are the names of Apple’s classes and data structures. The prefixes
UI indicate Apple data structures. Virtual memory allocations have the category
Each memory category has a disclosure triangle next to it. Clicking the disclosure triangle shows each memory allocation in that category. You can see the size of each memory allocation. If you open the extended detail view, you can see the call stack for each memory allocation.
Moving the mouse cursor over a memory category makes the small right arrow button appear. Clicking that button shows the following information for each memory allocation in that category:
Moving the mouse cursor over a memory address makes the small right arrow button appear. Clicking that button shows the reference count history for that memory address.
What I talked about in the previous two sections doesn’t just apply to the Generations section. If you click in the jump bar and choose Statistics, you can also use the techniques I described in the previous two sections. Click the small right arrow button next to a memory category to get information on every memory allocation in the given category. Click the small right arrow button next to a memory address to examine its reference count history.