如何在Swift中识别强参考周期? [英] How can identify strong reference cycles in Swift?

查看:41
本文介绍了如何在Swift中识别强参考周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SWIFT代码中是否存在用于定位强引用循环的工具或方法?

Is there a tool or method to locate strong references cycles in my SWIFT code?

强引用周期是当两个类的实例在没有适当安全性(/无主)的情况下相互引用时,因此阻止了垃圾回收器将它们一次全部丢弃.我创建的变量停止引用那些对象.

A strong reference cycle is when two instances of classes reference each other without the proper safeties (weak/unowned) hence preventing the garbage collector from disposing of them once all the variables I created stopped referencing those objects.

推荐答案

在Swift中查找强参考循环的方法与在Objective-C中相同.

The method for finding strong reference cycles is the same in Swift as it is in Objective-C.

您将通过Xcode运行该应用,充分锻炼该应用以显示周期,然后点击调试内存图"按钮().然后,您可以在左侧面板中选择一个未释放的对象,它会向您显示内存图,通常可以明显地增强参考周期:

You'd run the app from Xcode, exercise the app sufficiently to manifest the cycle, and then tap on the "debug memory graph" button (). You can then select an unreleased object in the panel on the left and it will show you the memory graph, often which can clearly strong reference cycles:

有时候,存储周期并不那么明显,但是您至少可以看到哪个对象对所讨论的对象保持着强烈的参照力.如有必要,您可以向后追溯并确定哪些内容对此有很强的借鉴意义,依此类推.

Sometimes the memory cycles are not as obvious as that, but you can at least see what object is keeping a strong reference to the object in question. If necessary, you can then track backwards and identify what's keeping a strong reference to that, and so on.

有时候,仅知道哪种对象可以保留强引用是不够的,您真的想知道在代码中的哪个位置建立了强引用.如 https://stackoverflow.com/a/30993476/1271826 所示,"malloc堆栈"选项可以是用于在建立此强引用时标识调用堆栈是什么(通常让您标识在其中建立这些强引用的精确代码行).有关更多信息,请参阅WWDC 2016视频使用Xcode进行视觉调试.

Sometimes knowing what sort of object is keeping the strong reference is insufficient, and you really want to know where in your code that strong reference was established. The "malloc stack" option, as shown in https://stackoverflow.com/a/30993476/1271826, can be used to identify what the call stack was when this strong reference was established (often letting you identify the precise line of code where these strong references were established). For more information, see WWDC 2016 video Visual Debugging with Xcode.

您还可以使用仪器来识别泄漏的物体.只需使用分配"工具通过仪器运行应用程序,反复(而不是一次或两次)将应用程序恢复到某种稳定状态,如果内存继续增加,则可能会有很强的参考周期.您可以使用分配"工具来确定哪些对象没有被释放,使用记录引用计数"功能来准确地标识这些强引用的建立位置,等等.

You can also use Instruments to identify leaked object. Just run the app through Instruments with the Allocations tool, repeatedly (not just once or twice) returning the app back to some steady state condition and if memory continues to go up, then you likely have a strong reference cycle. You can use the Allocations tool to identify what sort of objects are not being released, use "record reference count" feature to identify precisely where these strong references were established, etc.

请参阅WWDC 2013视频解决内存问题和WWDC 2012视频 iOS应用性能:内存,以介绍如何识别和解决内存问题.此处提出的基本技术今天仍然适用(尽管乐器的用户界面"工具已作了一些更改……如果要介绍略有更改的用户界面,请参阅WWDC 2014视频

See WWDC 2013 video Fixing Memory Issues and WWDC 2012 video iOS App Performance: Memory for introductions to identifying and resolving memory issues. The basic techniques proposed there are still applicable today (though the UI of Instruments tools has changed a bit ... if you want introduction to the slightly changed UI, see WWDC 2014 video Improving Your App with Instruments).

顺便说一句,垃圾回收"是指非常不同的内存系统,在这里不适用.

As an aside, "garbage collection" refers to a very different memory system and isn't applicable here.

这篇关于如何在Swift中识别强参考周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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