问题描述
有两个对象 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 将被释放.一旦它的其他所有者(我猜肯定有人)释放它,A 就会被释放.
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.
这篇关于保留周期:为什么这是一件坏事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!