当 Leaks 工具没有显示内存泄漏时,如何调试它们? [英] How to debug memory leaks when Leaks instrument does not show them?

查看:40
本文介绍了当 Leaks 工具没有显示内存泄漏时,如何调试它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 Swift 编写的 iOS 应用程序正在泄漏内存 - 在某些情况下,一些对象应该被释放,但它们没有.我通过简单地添加这样的 deinit 调试消息来了解这个问题:

I have an iOS app written in Swift that is leaking memory - in certain situation some objects should be released but they are not. I have learnt about the issue by simply adding deinit debug messages like this:

deinit {
    println("DEINIT: KeysProvider released")
}

因此,在应该导致对象释放的此类事件之后,控制台中应该出现 deinit 消息.但是,对于一些应该释放的对象,消息丢失了.尽管如此,Leaks Developer Tool 并未显示任何泄漏.我该如何解决这种情况?

So, the deinit message should be present in console after such events that should cause the object to release. However, for some of the objects that should be released, the message is missing. Still, Leaks Developer Tool does not show any leaks. How do I solve such situation?

推荐答案

在 Xcode 8 中,您可以点击Debug Memory Graph";按钮, 在调试工具栏中(显示在屏幕底部):

In Xcode 8, you can click on the "Debug Memory Graph" button, in the debug toolbar (shown at the bottom of the screen):

只需在左侧面板中确定您认为应该释放的对象,它就会向您显示对象图(显示在上面的主画布中).这对于快速识别在相关对象上建立强引用的位置非常有用.从这里,你可以开始你的研究,诊断为什么那些强引用没有被解析(例如,如果有问题的对象有来自其他应该被释放的东西的强引用,也看看那个对象的图表,你可能会发现问题(例如强引用周期、重复计时器等).

Just identify the object in the left panel that you think should have been deallocated, and it will show you the object graph (shown in the main canvas, above). This is very useful in quickly identifying where the strong references were established on the object in question. From here, you can start your research, diagnosing why those strong references were not resolved (e.g. if the object in question has a strong reference from something else that should have been deallocated, look at that object's graph, too, and you may find the issue (e.g. strong reference cycles, repeating timers, etc.).

请注意,在右侧面板中,我看到了调用树.我是通过打开malloc 堆栈"得到的.方案设置中的日志记录选项:

Notice, that in the right panel, I'm seeing the call tree. I got that by turning on the "malloc stack" logging option in the scheme settings:

无论如何,完成后,您可以单击上面第一个屏幕快照右侧面板中堆栈跟踪中显示的相关方法调用旁边的箭头,您可以看到最初建立强引用的位置:

Anyway, having done that, one can then click on the arrow next to the relevant method call shown in the stack trace in the right panel of the first screen snapshot above, and you can see where that strong reference was originally established:

传统的 Instruments 技术(如果使用旧版本的 Xcode 尤其有用)在我的原始答案中描述如下.

The traditional Instruments technique (especially useful if using older versions of Xcode) is described below, in my original answer.

我建议使用 Instruments 的分配";带有记录引用计数"的工具;特点:

I would suggest using Instruments' "Allocations" tool with the "Record Reference Counts" feature:

然后,您可以在 Instruments 中运行该应用程序,然后通过单击箭头搜索您知道正在泄漏的类并深入了解:

You can then run the app in Instruments and then search for your class that you know is leaking and drill in by clicking on the arrow:

然后,您可以使用扩展详细信息"深入了解详细信息并查看堆栈跟踪.右侧面板:

You can then drill into the details and look at the stack trace using the "Extended Details" panel on the right:

在那个扩展细节"中面板,将重点放在黑色的代码上,而不是灰色的系统调用上.无论如何,从扩展细节"开始面板,然后您可以直接在 Instruments::

In that "Extended Details" panel, focus on your code in black rather than the system calls in gray. Anyway, from the "Extended Details" panel, you can then drill into your source code, right in Instruments::

有关使用 Instruments 跟踪内存问题的更多信息和演示,请参阅:

For more information and demonstrations in using Instruments to track down memory problems, please refer to:

这篇关于当 Leaks 工具没有显示内存泄漏时,如何调试它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆