xcode 你如何检测 iPhone 上的内存泄漏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/494327/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:58:06  来源:igfitidea点击:

How do you detect memory leaks on iPhone?

iphonexcodememory-leaks

提问by 4thSpace

I'm using the Leaks Instruments feature through Xcode to (try and) find memory leaks. I still haven't figured out how to use this program. I click Leaks in the program and see memory increasing as I do various things in the simulator. I have Extended Detail pane displayed. The only thing in Extended Detail pane that references my app is main. As in the main method produced by Xcode. Everything else is UIKit, Foundations, and other SDK classes I didn't write. What am I doing wrong that nothing is showing up from my app?

我正在通过 Xcode 使用 Leaks Instruments 功能来(尝试和)查找内存泄漏。我还没有弄清楚如何使用这个程序。我在程序中单击 Leaks 并看到内存在我在模拟器中执行各种操作时增加。我显示了扩展详细信息窗格。Extended Detail 窗格中唯一引用我的应用程序的是 main。就像在 Xcode 生成的 main 方法中一样。其他一切都是 UIKit、Foundations 和其他我没有编写的 SDK 类。我的应用程序没有显示任何内容,我做错了什么?

Before I hit 3 minutes, there are over 100 leaks totaling 2.5k. Is this common?

在我打 3 分钟之前,有超过 100 次泄漏,总计 2.5k。这是常见的吗?

回答by OwenGoss

I've written up a Tutorial on using Instruments to track iPhone memory leaks. I'm not sure if it will help you with what you're dealing with or not...couldn't hurt, though. :-)

我写了一篇关于使用 Instruments 跟踪 iPhone 内存泄漏的教程。我不确定它是否会帮助你解决你正在处理的问题......不过不会伤害。:-)

http://www.streamingcolour.com/blog/tutorials/tracking-iphone-memory-leaks/

http://www.streamingcolour.com/blog/tutorials/tracking-iphone-memory-leaks/

回答by lajos

Change the view to "Extended Detail" on the instruments panel. This will show you the stack trace of each leaked object after you stop recording and select the leaked object.

在仪器面板上将视图更改为“扩展细节”。这将在您停止录制并选择泄漏对象后显示每个泄漏对象的堆栈跟踪。

You do see calls into the API, but what you are interested in is finding the last method of your application before the API calls, that is where the leak is.

您确实看到了对 API 的调用,但您感兴趣的是在 API 调用之前找到应用程序的最后一个方法,这就是泄漏所在。

A tip: turn on "gather memory contents" in the leaks view. Seeing the object values should also help finding where the problem is.

提示:在泄漏视图中打开“收集内存内容”。查看对象值也应该有助于找到问题所在。

You don't want any leaks. 100 leaks is not typical (at least in my apps ;) Typical should be 0.

你不想要任何泄漏。100 次泄漏并不典型(至少在我的应用程序中;)典型应为 0。

回答by Sophie Alpert

I'm not familiar with how to use Leaks, but you can always try running the Clang analyzer on your code to see if that'll turn anything up: http://clang.llvm.org/StaticAnalysis.html. It can often find many bugs that might lead to memory leaks.

我不熟悉如何使用 Leaks,但您可以随时尝试在您的代码上运行 Clang 分析器,看看是否会出现任何问题:http: //clang.llvm.org/StaticAnalysis.html。它通常可以找到许多可能导致内存泄漏的错误。

回答by Xorsat

Xcode: run -> Start with Performance Tool -> Leaks

Xcode:运行 -> 从性能工具开始 -> 泄漏

回答by Genericrich

Keep in mind that the Simulator may leak when the device will not. Ran into that once already with UITableViewController class.

请记住,当设备不会泄漏时,模拟器可能会泄漏。使用 UITableViewController 类已经遇到过一次。

回答by Genericrich

Use LLVM/Clang Static Analyzer.

使用 LLVM/Clang 静态分析器。

回答by Kendall Helmstetter Gelner

Note also that the leak tool is not going to show you instances where objects are over-retained and still held on to. Leaks are cases where objects that should have been let go are just hanging around with no-one to clean them up. Over retained objects are validly held onto even though you'd think they should be gone - thus the leak tool cannot point them out, since they are still referred to and there's no way to tell them apart from objects that should still be retained.

另请注意,泄漏工具不会向您显示对象被过度保留并仍然保留的实例。泄漏是指本应被释放的物体只是闲逛而无人清理它们的情况。即使您认为它们应该消失,过度保留的对象也会被有效保留 - 因此泄漏工具无法指出它们,因为它们仍然被引用并且没有办法将它们与应该仍然保留的对象区分开来。

To find those, use the memory reporting tool and make sure that memory use goes down fully after you free an object. If you notice something isn't freeing memory, you can start by putting breakpoints in dealloc to see if what you expect to see released is actually getting released.

要找到这些,请使用内存报告工具并确保在释放对象后内存使用完全减少。如果您发现某些内容没有释放内存,您可以首先在 dealloc 中放置断点,以查看您期望看到的释放内容是否真的被释放了。

You need to look for both cases to keep a clean memory footprint.

您需要查找这两种情况以保持干净的内存占用。

回答by Shawn

Run -> Start with Performance Tool -> Leaks

回答by Shawn

Leaks application that can be found in Xcode: run -> Start with Performance Tool -> Leaks.

可以在 Xcode 中找到的 Leaks 应用程序:run -> Start with Performance Tool -> Leaks.

Apple's Instruments utility that can be found in /Developer/Applications/Performance Tools.

可以在/Developer/Applications/Performance Tools.

回答by user2998756

One of the best way to find the memory leaks is Select Product-> Analyze. In the left Xcode shows in which file you have memory leaks. What are the variable causing memory leaks. This is one of the best way to find memory leaks.

查找内存泄漏的最佳方法之一是选择“产品”->“分析”。在左侧的 Xcode 中显示了您在哪个文件中存在内存泄漏。导致内存泄漏的变量是什么。这是查找内存泄漏的最佳方法之一。