How to profile memory usage & performance with Instruments?

XcodePerformanceProfilingInstrumentsAllocation

Xcode Problem Overview


Of all the Instruments Trace Templates, I love using:

  • Zombies to detect where an object is getting over-released, great for debugging EXEC_BAD_ACCESS errors.
  • Leaks to detect memory leaks.
  • Core Animation w Color Blended Layers to detect frame rate & translucent subviews, great for smoothing up UITableView scrolling.

I always hear people saying to profile my app's memory usage & performance.

  1. Why should I profile memory usage & performance? My app runs fine.
  2. How do I do it?

I've used Allocations and see that my iPhone app starts at 1 MB total allocated memory and grows to 5 MB after normal usage. What is too high amount of memory usage on the iPhone? iPad? Mac?

Xcode Solutions


Solution 1 - Xcode

To answer the whys, profiling memory usage is especially important for iOS apps because iPhones and iPads have much less RAM than Macs. The iPhone 4 has 512 MB of RAM, but earlier versions had 256 or 128 MB. Factor in the RAM the OS uses and multitasking, and your app doesn't have much RAM to waste so it's important to be aware of how much memory your app uses.

Profiling performance is something you usually do when your app is running slowly. Profile it to find the slow spots in your code so you can make the code run faster. If your app runs fine, you don't have much need to profile for performance.

To answer the hows, use the Allocations instrument to measure memory usage. The Live Bytes column in the All Allocations category tells you the amount of memory your app is currently using. The Allocations instrument's heapshot analysis measures memory growth in your app. Use the menu on the left side of the jump bar to do heapshot analysis.

The Time Profiler instrument profiles your app for performance. The difficult part of using the Time Profiler instrument is interpreting the results. The Time Profiler instrument isn't going to tell you your app spends 75% of its time in Function X. You have to dig through the data to find the slow spots in your code.

Regarding acceptable memory usage, it depends on the devices you want to support and the app. An app like Xcode using 100 MB of RAM would be OK, but an app like TextEdit using 100 MB for a one page document would be a problem. 5 MB shouldn't be a problem for an iOS app.

Solution 2 - Xcode

To address some of the comments in Mark's answer:

Allocations live bytes doesn't include OpenGL texture memory, which is used by CALayer/UIViews. This is the source of the disagreement with the Memory Monitor.

See the answer to this question here: https://stackoverflow.com/questions/363493/understanding-the-memory-consumption-on-iphone

Solution 3 - Xcode

The memory really loaded into device's physical memory is the Resident Memory in VM Tracker Instrument.

Allocation Instrument only marks the memory created by malloc/[NSObject alloc] and some framework buffer, for example, decompressed image bitmap is not included in Allocation Instrument but it always takes most of your memory.

Please Watch WWDC 2012 Session 242 iOS App Performance: Memory to get the information from Apple.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionma11hew28View Question on Stackoverflow
Solution 1 - XcodeSwift Dev JournalView Answer on Stackoverflow
Solution 2 - XcodeCalvinView Answer on Stackoverflow
Solution 3 - XcodeCarmeloSView Answer on Stackoverflow