How to profile memory usage & performance with Instruments?
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开发者_StackOverflow 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.
- Why should I profile memory usage & performance? My app runs fine.
- 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?
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.
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: Understanding the memory consumption on iPhone
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.
精彩评论