Auto Reference Count (Automatic Reference counting, ARC)
Garbage collector:
Starting with Mac OS X 10.8, the " Garbage collector " (Gargae collector) has officially been deprecated.
Each object has a counter that indicates how many things are currently trying to keep the object alive. It is called a retention count (retain count), or it can be called a reference count (reference count).
A counter is operated by three methods:
Reatain Increment
Release Decrement
autorelease Descending, auto-release pool (autorelease, 34th)
To view the retention count method:Retaincount, not recommended.
When an object is created, its retention count is at least 1.
When the hold count is zero, the object is recycled (deallocated), that is, the memory it consumes is marked as " reusable " (reuse).
After the memory is "deallocated" (deallocated), it is simply put back into the " Free Memory Pool " (avaiable pool). If the memory is not overwrite, then the object is still valid, and the program does not crash.
To avoid inadvertently using invalid objects, the pointer is emptied after the meal release is generally called. It is guaranteed that there will be no pointers to invalid objects, often called " dangling Pointers " (dangling pointer, wild pointers ).
Cases:
[XXXX release];
xxxx = nil;
If an object holds a strong reference to another object (strong reference), the former " owns " (own) the latter.
The "root object" of the reference
NSApplication objects in MAC OS x applications
The UIApplication object in the iOS app.
Both are the singleton that is created when the application starts.
memory Management in the property access method:
Cases:
-(void) Setfoo: (ID) foo {
[Foo retain]; Keep the new value first
[_foo release]; Release old values again
_foo = foo; Update the instance variable to point to the new value
[_foo release];
_foo = [foo retain];
}
Order is important.
If the new value is not retained and the old value is released, and two values point to the same object, then the release operation that is performed first may cause the system to recycle the object permanently. And the subsequent retain operation is not a decree that has been completely recycled object resurrection, so the instance variable into a hanging pointer.
Auto Free pool:
Autorelease, this method decrements the count at a later time, usually in the next event loop, but may also be executed earlier (the auto-free pool that you created).
Autorelease can extend the lifetime of an object so that it can survive for a period of time after crossing the method invocation boundary.
Typically used when returning an object in a method.
Cases:
-(NSString *) StringValue {
NSString *str = [NSString alloc]initwithformat:@ "%@", self];
Retrun [str autorelease];
}
Retention ring (retain cycle):
Circular reference, cannot be freed
1, usually use " weak reference " (weak reference, see article 33rd)
2, or from the external command (??? An object in the loop no longer retains another object.
Both of these methods can break the retention ring, thus avoiding memory leaks.
29th: Understanding Reference count