保留周期:为什么这样的坏事? [英] Retain Cycles: Why is that such a bad thing?

查看:98
本文介绍了保留周期:为什么这样的坏事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个对象A和B. A创建B并保留它。 B有一个指向A的实例变量,保留它。所以两者保持彼此。有人说,这种强有力的联系不能再破。

There are two Objects A and B. A creates B and retains it. B has an instance variable that points to A, retaining it. So both retain eachother. Some people say, that this strong connection can't be broken ever again.

但是是真的吗?

如果B将释放A,那么A可以轻松释放B,因此B将被释放。

If B would release A, then A could easily release B, and so B would be deallocated. A would be deallocated as soon as it's other owner (I guess there must be someone) releases it.

或者这个问题只适用于A不创建的情况下B,但只是通过保留它在一个实例变量保持一个强的引用?我仍然不明白为什么这个连接不能被再次分解。

Or does this problem only apply in a case where A does not create B, but just holds a strong reference to it through retaining it in an instance variable? I still don't see why that connection could not be broken up again.

推荐答案

循环不坏,经常避免,因为他们可以使它棘手,以确保你没有内存泄漏。泄漏发生,特别是当对象被引用计数时。在使用引用计数的语言或系统中,对象跟踪指向它的引用的数量。每当一个引用被删除,计数就会减少,当计数变为零时,没有引用,所以可以删除该对象。

Cycles aren't bad, but they are often avoided because they can make it tricky to ensure you haven't got memory leaks. Leaks occur especially when objects are 'reference counted'. In a language or system that uses reference counting, an object keeps track of the number of references pointing at it. Every time a reference is deleted, the count goes down, when the count gets to zero, there are no references and so the object can be deleted.

这通常需要小心本身和工作确定没有任何仔细的想法。如果你有一组没有循环的对象,并且你删除了对根对象的引用,那么它将被删除,这意味着它所拥有的对象的引用将被删除,被引用的对象将具有它们的引用计数去零。它们将被删除,级联会导致所有对象被删除。

This usually takes care of itself and works ok without any careful thinking. If you've got a group of objects with no cycles and you drop your reference to the root object, then it will be deleted, this means references it has to objects it owns will be dropped, the objects being referenced will have their reference counts go to zero. They'll be deleted and the cascade will cause all objects to be deleted.

但是...如果你有一个循环,这个级联不工作。你可能有一组对象,你不想要它们,所以你删除对这些对象的唯一引用,但因为有一个循环,对象相互引用。这意味着它们的引用计数永远不会为零,并且不会被删除。这是内存泄漏。

But... if you have a cycle, this cascade doesn't work. You may have a group of objects and you don't want them any more, so you drop the only reference you have to these objects, but because there is a cycle the objects reference each other. This means their reference counts never go to zero, and they don't get deleted. This is a memory leak.

很明显,你可以做一些小心的管理,并在你把你的引用删除一组你不想要的对象之前中断循环。但是,正如我刚才说的,这需要仔细的管理。这很容易出错。这是发生内存泄漏的主要原因之一。

Clearly, you can do some careful management and break the cycles before you drop your reference to a group of objects you don't want any more. But... as I just said, this takes careful management. It's very easy to get wrong. This is one of the main reasons that memory leaks occur.

为了避免泄漏的风险,以及在不再需要一组对象时正确地打破循环的棘手工作,程序员通常尽量避免循环。这对于许多程序员的大项目来说变得更加重要,因为没有人能理解整个系统。如果有循环,程序员将不得不注意和花费很长时间学习彼此的代码以避免循环。

To avoid the risk of leaks and the tricky job of breaking cycles correctly when you no longer need a group of objects, programmers usually try to avoid cycles. This becomes more important on big projects with many programmers where no one person understands the whole system. If there were cycles, the programmers would have to watch out and spend a long time studying each others code to avoid cycles.

一些语言与垃圾收集器(例如C#)删除一组不再需要的对象,即使该组包含循环。

Some languages with garbage collectors (eg C#) can delete a group of objects that are no longer needed even if the group contains cycles.

这篇关于保留周期:为什么这样的坏事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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