xcode 如何使用 Instruments 分析内存使用情况和性能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6641540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to profile memory usage & performance with Instruments?
提问by ma11hew28
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.
- 僵尸检测对象在哪里被过度释放,非常适合调试
EXEC_BAD_ACCESS
错误。 - 泄漏检测内存泄漏。
- 核心动画 w 颜色混合层来检测帧速率和半透明子视图,非常适合平滑
UITableView
滚动。
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?
我使用了 Allocations 并看到我的 iPhone 应用程序从 1 MB 的总分配内存开始,并在正常使用后增长到 5 MB。iPhone 内存使用量过高是怎么回事?iPad?苹果电脑?
回答by Mark Szymczyk
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.
要回答原因,分析内存使用情况对于 iOS 应用程序尤其重要,因为 iPhone 和 iPad 的 RAM 比 Mac 少得多。iPhone 4 有 512 MB 的 RAM,但早期版本有 256 或 128 MB。考虑到操作系统使用的 RAM 和多任务处理,您的应用程序不会浪费太多 RAM,因此了解您的应用程序使用多少内存很重要。
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.
要回答方法,请使用 Allocations 工具来测量内存使用情况。All Allocations 类别中的 Live Bytes 列会告诉您应用程序当前使用的内存量。Allocations 工具的 heapshot 分析测量应用程序中的内存增长。使用跳转栏左侧的菜单进行 heapshot 分析。
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.
Time Profiler 工具分析您的应用程序的性能。使用 Time Profiler 仪器的困难部分是解释结果。Time Profiler 工具不会告诉您您的应用程序 75% 的时间都花在函数 X 上。您必须挖掘数据以找到代码中的慢点。
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.
关于可接受的内存使用量,这取决于您要支持的设备和应用程序。像 Xcode 这样使用 100 MB RAM 的应用程序是可以的,但是像 TextEdit 这样的应用程序使用 100 MB 的内存来处理一页文档就会有问题。5 MB 对于 iOS 应用程序来说应该不是问题。
回答by Calvin
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.
分配实时字节不包括 OpenGL 纹理内存,这是由 CALayer/UIViews 使用的。这就是与内存监视器不一致的根源。
See the answer to this question here: Understanding the memory consumption on iPhone
在此处查看此问题的答案: 了解 iPhone 上的内存消耗
回答by CarmeloS
The memory really loaded into device's physical memory is the Resident Memory
in VM Tracker Instrument
.
真正加载到设备物理内存的内存是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.
Allocation Instrument
仅标记由malloc/[NSObject alloc]
一些框架缓冲区创建的内存,例如,解压缩图像位图不包含在内,Allocation Instrument
但它始终占用您的大部分内存。
Please Watch WWDC 2012 Session 242 iOS App Performance: Memory to get the information from Apple.
请观看 WWDC 2012 Session 242 iOS App Performance: Memory 从 Apple 获取信息。