xcode 内存泄漏检测工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/144261/
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
Memory leak detection tools
提问by SytS
Does Apple's Xcode development environment provide any tools for memory leak detection?
Apple 的 Xcode 开发环境是否提供任何用于内存泄漏检测的工具?
I am especially interested in tools that apply to the iPhone SDK. Currently my favourite platform for hobby programming projects
我对适用于 iPhone SDK 的工具特别感兴趣。目前我最喜欢的业余编程项目平台
Documentations/tutorials for said tools would be very helpful.
所述工具的文档/教程将非常有帮助。
回答by rustyshelf
There is one specifically called Leaks
and like a previous poster said, the easiest way to run it is straight from Xcode:
有一个专门调用的Leaks
,就像之前的海报所说,运行它的最简单方法是直接从 Xcode 中运行:
run -> Start with Performance Tool -> Leaks
运行 -> 从性能工具开始 -> 泄漏
It seems very good at detecting memory leaks, and was easy for a Non-C Head like me to figure out.
它似乎非常擅长检测内存泄漏,对于像我这样的非 C 负责人来说很容易弄清楚。
回答by titaniumdecoy
Select Profile
from the Product
menu in Xcode 6 to launch Apple's Instruments tool. (The application is located inside the Xcode application's package contents: /Applications/Xcode.app/Contents/Applications/
)
Profile
从Product
Xcode 6的菜单中选择以启动 Apple 的 Instruments 工具。(应用程序位于Xcode应用程序的软件包内容内:/Applications/Xcode.app/Contents/Applications/
)
A commercial alternative is OmniObjectMeter.(Discontinued by The Omni Group)
商业替代品是OmniObjectMeter。(由 Omni Group 停产)
回答by lajos
The Clang Static Analyser
is great for finding bugs in C, C++ and Objective-C code:
该Clang Static Analyser
是伟大的发现在C,C ++和Objective-C代码中的错误:
回答by catlan
You can run the tools within Xcode over menu -> run -> start with performance tool -> ...
您可以在 Xcode 中运行这些工具 menu -> run -> start with performance tool -> ...
回答by sKhan
Here is the link for using instrument from xcode to detect memory leak/performance of you ios/mac application Steps to run instrument from Xcode
这是使用 xcode 中的仪器检测 ios/mac 应用程序的内存泄漏/性能的链接从 Xcode 运行仪器的步骤
回答by jww
Does Apple's Xcode development environment provide any tools for memory leak detection?
I am especially interested in tools that apply to the iPhone SDK.
Apple 的 Xcode 开发环境是否提供任何用于内存泄漏检测的工具?
我对适用于 iPhone SDK 的工具特别感兴趣。
Yes. Apple calls them "Instruments" (there's more than just memory tools).
是的。Apple 称它们为“工具”(不仅仅是记忆工具)。
See Apple's Introduction to Instruments User Guide
. In particular, see Locating Memory Issues in Your App
. It provides examples of how to use the memory-oriented trace templates.
请参阅 Apple 的Introduction to Instruments User Guide
. 特别是,请参阅Locating Memory Issues in Your App
。它提供了如何使用面向内存的跟踪模板的示例。
回答by David Seek
Step 1. Pick the Allocations instrument
步骤 1. 选择分配工具
- Choose the profiling template for Allocations:
- 为分配选择分析模板:
- On the main Instruments interface, click VM Tracker, if present, and press the Delete key since you won't be needing that particular instrument:
- 在 Instruments 主界面上,单击 VM Tracker(如果存在),然后按 Delete 键,因为您不需要该特定仪器:
By clicking the plus button in the top right, you can add more instruments for different kinds of testing, but I won't be covering them in this tutorial.
通过单击右上角的加号按钮,您可以为不同类型的测试添加更多仪器,但我不会在本教程中介绍它们。
Step 2. Set up your Instruments settings
步骤 2. 设置您的 Instruments 设置
Before running any analysis, there are a few things you need to do. First, you need to plug in an iOS device that has your app installed on it. It must be a physical device because the iOS Simulator is still a simulator and may not accurately represent memory use in your app or how an app might perform under memory pressure.
在运行任何分析之前,您需要做一些事情。首先,您需要插入安装了您的应用程序的 iOS 设备。它必须是物理设备,因为 iOS 模拟器仍然是模拟器,可能无法准确表示应用程序中的内存使用情况或应用程序在内存压力下的执行情况。
To pick your target, click My Computernear the top, hover over your device, and then pick your app from the sub-menu:
要选择您的目标,请单击顶部附近的我的电脑,将鼠标悬停在您的设备上,然后从子菜单中选择您的应用程序:
Next, there is a panel where you can alter the settings for the types of allocations you will be viewing. Besides making sure the Created & Persistentbubble is checked, there is not much you need to do beforehand.
接下来,有一个面板,您可以在其中更改将要查看的分配类型的设置。除了确保选中Created & Persistent气泡之外,您不需要事先做太多事情。
Step 3. Press record to run the instrument
步骤 3. 按记录运行仪器
Once you press the Record button in the top left, your app will start up on your device, and Instruments will begin to chart your allocations. All you need to do here is run through your app, focusing on possible problem areas to see if more memory allocates than deallocates. This could mean doing a lot of repetitive tasks, but you'll thank yourself later.
一旦您按下左上角的 Record 按钮,您的应用程序将在您的设备上启动,并且 Instruments 将开始绘制您的分配图表。您在这里需要做的就是运行您的应用程序,关注可能的问题区域,看看分配的内存是否多于释放的内存。这可能意味着做很多重复的任务,但你以后会感谢自己。
You should see something like this:
您应该会看到如下内容:
I recommend running through your app once and getting to a stable point in memory so you have a good baseline that will make any increase noticeable. When you are satisfied you have enough data to test, press the stop button in the top left.
我建议运行一次你的应用程序并在内存中达到一个稳定点,这样你就有了一个很好的基线,这将使任何增加都变得明显。如果您对有足够的数据进行测试感到满意,请按左上角的停止按钮。
Step 4. Analyze
步骤 4. 分析
- The first thing I do is set my inspection range to measure the total persistent bytes at my baseline. That persistent byte number is located right under the allocation summary.
- 我做的第一件事是设置我的检查范围以测量我基线处的总持久字节数。该持久字节数位于分配摘要的正下方。
To actually set the inspection range, use the keyboard shortcut Command <for the left inspection range and Command >for the right inspection range. In our app, we have a baseline of about 20MB.
要实际设置检查范围,请使用键盘快捷键Command <用于左侧检查范围和Command >用于右侧检查范围。在我们的应用程序中,我们有大约 20MB 的基线。
- Then, I move my right inspection range to a point where I had run through the app again and came back to our root. Here, you can see memory is about the same. So, by doing this a few more times and seeing your memory come back to our baseline, you can assume there are no major memory issues.
- 然后,我将正确的检查范围移动到我再次运行该应用程序并返回到我们的根目录的位置。在这里,您可以看到内存大致相同。因此,通过多做几次并看到您的记忆恢复到我们的基线,您可以假设没有重大的记忆问题。
There are different ways to analyze this data that I won't cover here, but be aware that there's a whole drop-down menu of ways to view and analyze your data.
有多种不同的方法可以分析这些数据,我不会在此处介绍,但请注意,有一个完整的下拉菜单可以查看和分析数据。
Step 5. Marking generations
步骤 5. 标记世代
If you prefer not to deal with the inspection ranges as much, there is a feature called Mark Generation. There is a button for it on the right panel of instruments.
如果您不想过多地处理检查范围,则可以使用称为标记生成的功能。在仪器的右侧面板上有一个按钮。
This button will mark points on the timeline of instruments based on where the inspection line is. It does this in order to keep track of all the allocations since the previous mark, or from the beginning if there are no other marks. You can mark generations as you are running the allocations instrument or after you have stopped the run, as in this example:
此按钮将根据检测线的位置在仪器的时间线上标记点。这样做是为了跟踪自上一个标记以来的所有分配,或者如果没有其他标记,则从头开始。您可以在运行分配工具时或在停止运行后标记代,如下例所示:
Step 6. Check out the stack trace
步骤 6. 检查堆栈跟踪
The last thing to cover is looking at the stack trace. For this, you want to set your inspection range to highlight all the allocations, and then look at the statistics view, making sure the Created & Persistentbubble is selected on the right panel. In the statistics view, make sure Persistent Bytes is sorted from highest to lowest. There are a lot of allocations here, and it can be hard to understand what is going on, since a lot of them are system allocations.
最后要介绍的是查看堆栈跟踪。为此,您希望设置检查范围以突出显示所有分配,然后查看统计视图,确保在右侧面板上选择了Created & Persistent气泡。在统计视图中,确保 Persistent Bytes 从高到低排序。这里有很多分配,很难理解发生了什么,因为其中很多是系统分配。
Going deep
深入
- Look at the largest allocations and click on the right-facing arrow. A lot of times there will be allocations inside the ones you clicked on and many of them won't have meaning to you.
- 查看最大的分配,然后单击向右的箭头。很多时候你点击的那些里面会有分配,其中许多对你没有意义。
- As you highlight different allocations after clicking an arrow, continue looking at the extended detail on the right panel. Eventually you will come across some bold text that leads to actual code in your project, telling you what the issue might be.
- 在单击箭头后突出显示不同的分配时,继续查看右侧面板上的扩展详细信息。最终,您会遇到一些粗体文本,这些文本指向您项目中的实际代码,告诉您可能存在的问题。
- If you double-click one of the bold items in the stack trace, it will take you to the actual code (assuming you ran allocations on an app you own).
- 如果您双击堆栈跟踪中的粗体项目之一,它将带您进入实际代码(假设您在自己的应用程序上运行分配)。
There are a lot of useful things about this view, one being the mostly yellow tags on the right showing you just how much memory each method call is taking up. Every app is different so you, the developer, have to decide if the highlighted method is a problem, something you can optimize, or just an unavoidable part of your app.
In my case, that UIColor variable is something that is persistent and used throughout our app and is therefore, acceptable throughout the life of our app.
这个视图有很多有用的东西,其中一个是右侧的大部分黄色标签,显示每个方法调用占用了多少内存。每个应用程序都是不同的,因此您,开发人员,必须决定突出显示的方法是一个问题,您可以优化的内容,还是只是应用程序中不可避免的一部分。
就我而言,该 UIColor 变量是持久的并在我们的应用程序中使用,因此在我们的应用程序的整个生命周期中都是可以接受的。
回答by KevDog
ObjectAlloc and MallocDebug should both be of help to you. If you installed the entire SDK, they will be found in Developer->Applications->Performance Tools.
ObjectAlloc 和 MallocDebug 都应该对您有所帮助。如果您安装了整个 SDK,它们将在 Developer->Applications->Performance Tools 中找到。
Their names give you a pretty good clue as to their functions, OA, tracks the objects create and MA is a general memory leak tool.
它们的名称为您提供了一个很好的线索,了解它们的功能,OA,跟踪创建的对象,而 MA 是一个通用的内存泄漏工具。
I haven't tried them with iPhone development yet, but I have to believe that they would work there as well.
我还没有在 iPhone 开发中尝试过它们,但我不得不相信它们也能在那里工作。
Assuming you have registered for ADC iPhone developer site, here the link to follow:Instruments User Guide
假设您已经注册了 ADC iPhone 开发者网站,这里的链接如下:Instruments User Guide
回答by schwa
When using rustyshelf's solution
make sure you test on the iPhone and not on the simulator. Memory usage is dramatically different.
使用时请rustyshelf's solution
确保您在 iPhone 上而不是在模拟器上进行测试。内存使用情况截然不同。
回答by Benoit Caccinolo
Made a sum up of the main memory leak tools: iphone-essential-performance-tools-list
对主要的内存泄漏工具做了一个总结: iphone-essential-performance-tools-list