iphone - Retain Cycles: Why is that such a bad thing? -


Two objects are A and B, B creates and retains it. B is an example variable that points to A, to maintain it. Then both of them will retain each other. Some people say that this strong relationship can never be broken again.

But is this really the case?

If BA will release, then A can easily release B, and therefore B will be deallocated. A. The other owner (I think should be with someone) will be canceled in case of release.

Does this problem apply only in cases where ABC does not make, but maintains a strong context through maintaining it in an example variable? I still do not know why that relation could not be broken again.

The cycles are not bad, but often they are saved because they can make it difficult to ensure You do not have a memory leak that leaks specially when objects are counted as 'reference' uses a reference calculation in a language or system, an object keeps track of the number of references indicated on it. Each time the reference is removed, the count goes down, when the count becomes zero, there is no reference and hence the object can be removed.

It usually takes care of yourself and acts without carefulness. If you do not have a circle and you leave the reference to your object, then it will be removed, this means that the object that it is owned will be removed, the reference number will be zero when the object is referenced. They will be removed and all objects will be removed due to cascade.

But ... if you have a cycle, then this waterfall is not working. You can have a group of objects and you do not want them more, so you only reference these objects, but because one circle object references each other. This means that the number of their references never goes to zero, and they are not removed. This is a memory leak.

Obviously, you can manage some careful and leave your reference in the group of objects you do not want before breaking the cycle. But ... as I said now, it takes careful management, it is very easy to do wrong. It is one of the main reasons, which is leaking memory.

To avoid the risk of leak and to avoid the difficult task of breaking the cycle, when you do not need a group of objects, programmers generally try to avoid the cycles, with many programmers. It becomes more important on big projects where no person understands the entire system. If there are cycles, then the programmer will have to look and spend a long time to study each other code to avoid the cycles.

With garbage collectors (like C #), a few languages ​​can be removed from a group of objects which are no longer necessary even in the circle of the group.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -