简体   繁体   中英

Understanding retain cycle in depth

Lets say we have three objects: a grandparent, parent and child. The grandparent retains the parent, the parent retains the child and the child retains the parent. The grandparent releases the parent.

What will happen in this case ?

Unless there is some other reference to the parent or child, they both become orphaned. But the retain cycle between the parent and child prevent either from being released and they become wasted memory.

A child should never retain a parent. If anything, use a weak reference in the child to maintain a reference to the parent.

Retain Cycle is the condition When 2 objects keep a reference to each other and are retained, it creates a retain cycle since both objects try to retain each other, making it impossible to release.

Here The "Grandparent" retains the "parent" and "parent" retains the "child" where as "child" retains the "parent".. Here a retain cycle is established between parent and child. After releasing the Grandparent both the parent and child become orphaned but the retain count of parent will not be zero as it is being retained by the child and hence causes a memory management issue.

There are two possible solutions:

1) Use weak pointer to parent , ie a child should be using weak reference to parent, which is not retained.

2) Use "close" methods to break retain cycles.

http://www.cocoawithlove.com/2009/07/rules-to-avoid-retain-cycles.html

In a simple case, consider two objects A and B where A creates and retains B. When A is created, it creates B. When whoever created A finally releases it, A's retain count drops to zero and it gets deallocated. If A's dealloc method calls release on B, B's retain count also drops to zero and it also gets deallocated. [This assumes that nobody else has retained A or B, because I'm keeping things simple.]

But what happens if B needs a reference back to A, and it retains A? Whoever created A might release it. But since B has also retained A, A's retain count won't go to zero. Likewise, since A retains B, B's retain count also won't go to zero. Neither will be deallocated. Even if B calls A's release method in its own dealloc it doesn't matter, because that method is never going to be called.

At this point you have a memory leak, because you don't have any reference to A or B even though they both still exist. If A or B is doing anything processor intensive then you might also be leaking CPU time to unwanted objects.

In your case A is parent and B is child and whosoever created A is grandparent.

A retain cycle is a loop that happens when Object A retains Object B, and Object B retains Object A. In that situation, if either object is released:

  • Object A won't be deallocated because Object B holds a reference to it (retain count > 0).
  • Object B won't ever be deallocated as long as Object A has a reference to it (retain count > 0).
  • But Object A will never be deallocated because Object B holds a reference to it (retain count > 0).
  • till infinity

Thus, those two objects will just hang around in memory for the life of the program even though they should, if everything were working properly, be deallocated.

当祖父母释放父母时,父母仍然活着,因为孩子保留父母。

Grandparent : John Parent : Ted Child : Mary

Here is my example using a telephone call for illustration:

  • John calls Ted and wants to do a conference call with Mary.

  • Ted Says to John: "Hang on the line, and I will dial in Mary"

  • Ted leaves John on hold and calls Mary who promptly answers the phone.

  • Mary says to Ted: "Merge my call in with John and I WILL NOT hang up until I'm through"

  • Ted, having not heard back from John in a while, leaves the call to do something else.

  • John goes to merge the calls with Ted and Mary and then suddenly dies.

  • Mary is stuck on the line to John but will never hang up cause John ain't coming back!

Retain Cycle is the condition when 2 objects keep a reference to each other and are retained , it creates a retain cycle since both objects try to retain each other, making it impossible to release.


Example: A person lives in a department, a department has one person.

@class Department;

@interface Person:NSObject
@property (strong,nonatomic)Department * department;
@end

@implementation Person
-(void)dealloc{
    NSLog(@"dealloc person");
}

@end
@interface Department: NSObject
@property (strong,nonatomic)Person * person;
@end

@implementation Department
-(void)dealloc{
    NSLog(@"dealloc Department");
}
@end

Then call it like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    Person * person = [[Person alloc] init];
    Department * department = [[Department alloc] init];
    person.department = department;
    department.person = person;
}

You will not see dealloc log, this is the retain circle.

Since the P object has retainCount of 1, when it is released, its retainCount goes to 0, and its dealloc method is called; This in turn calls release on C object, whose retain count also goes to 0; and its dealloc method is called.

Both objects P and C will get freed.

When C object's dealloc method is called, in turn GP object's release is called, but since GP holds a retain count of 2, the retain count is decremented to 1, and it continues to hang around.

Retain cycle is a deadlock condition. Real Life Example of Retain Cycle: If two object hold a reference each other and no other object is released.

Example: Rummy Game

When two objects keep references of each other in such a way the objects create cycle and are retained. Both objects try to retain each other, in this case they are strongly connected with each other and impossible to release is called retain cycle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM