Sunday, April 6, 2014

OC Memory Management

Reference counting

the counting number means the number of users who is using it.









1. each object has its own retain counting.
2. reference counting is 1 when object is created.
3. sending retain message, object's reference counting add 1.
4. sending release message, object's reference counting minus 1.
5. dealloc is revoked when object's reference counting is 0.

example:

Person *person = [[Person alloc] init];



NSLog(@"%ld", [person retainCount])& // 1


[person retain] ;


NSLog(@"%ld", [person retainCount])& // 2


[person retain] ;


NSLog(@"%ld", [person retainCount])& // 3


[person release] ;


NSLog(@"%ld", [person retainCount])& // 2


[person release] ;


NSLog(@"%ld", [person retainCount])& // 1


[person release] ;


NSLog(@"%ld", [person retainCount])& // 0 




No comments:

Post a Comment