1.1 Reference count Reference Count
1.2 Automatic reference count,ARC(Automatic Reference counting)
- 1 Reference Count
Reference count ( Reference Count) is a simple and effective way to manage the object life cycle. When we create A new object, it has a reference count of 1, and when a new pointer points to the object, we reference it Count plus 1, when a pointer no longer points to this object, we subtract its reference count by 1, and when the reference count of the object becomes 0 , This means that the object is no longer pointed at by any pointers, so we can destroy the object and reclaim the memory .
Memory management Five Ways of thinking:
- Own generated objects, own hold (self-sustaining)
- Not self-generated objects, you can also hold (non-native)
- Do not need to be held by their own object release
- Objects not owned by themselves cannot be freed
- do not send a message to an object that has already been disposed Interest
1. Self-sustaining
generate objects by themselves in the following ways ( count plus one ) :
- a lloc,new,copy,mutablecopy
- Allocmyobject,newthatobject,copythis, Mutablecopyyourobject
ID obj=[nsobject Alloc]init] ; ( count plus 1 )
2. non-self-sustaining
use retain to make non-self-sustaining
I d obj=[nsmutablearray array];( count does not add 1 )
[obj retain]; ( count plus 1 )
3. do not need to be held by their own object release
Use a lloc,new,copy,mutablecopy , or retain method holds the object, and once it is not required, use The release method to free
[obj release] ;
4. objects not owned by themselves cannot be freed
Id Obj=[obj0 Object];
[obj1 release]; (Obj1 without self-possession, the release of words would cause abnormal collapse of the program )
5. do not send a message to an object that has already been disposed Interest
we send a retaincount message to an object that has been recycled , so its output should be indeterminate, and if the memory used by the object is reused, it could cause the program to collapse unexpectedly.
Automatic release pooling principle:
calling release will immediately reduce the retention count by 1, possibly reclaiming objects, using authorelease, The count will be reduced by 1 at a later time, usually in the next "event loop". Useful when a method returns an object.
2. ARC
- because ARC is an auto-execute retain,release,autorelease,dealloc that cannot be called.
- the method naming rules that ARC must follow:
at the beginning of alloc,new,copy,mutablecopy . The object that it returns is called by the caller. (self-sustaining).
- ARC the Way to manage the life of the object is basically to insert the "retain", "release" action in the appropriate place.
- C Ore Foundation Object Not attributed to ARC management
ARC helped us solve the reference count. 9 0% problem, but still and 10% memory management, which needs to be handled by itself .
You need to maintain the reference count for these objects yourself.
The main issues for the problem are:
- Excessive use Block after that, the circular reference problem cannot be resolved.
- encounter the underlying Core Foundation objects, which require their own manual management of their reference counts, appear helpless.
1. Circular Reference (Reference cycle) Q Questions
1) reference each other as a member of their own variable amount
Workaround:
- active disconnection of the cycle lead with
It is clear that there will be a circular reference here, active disconnection in a reasonable position in the loop of an introduction .
actively disconnecting circular references this operation relies on the programmer's own manual and explicit control , the equivalent of returning to the previous " who applies for release " of the memory management year , so this workaround does not often use
- using weak primers with
weak references, while holding objects, do not increase the reference count, thus avoiding the production of cyclic references
two x Viewcontroller a and b,viewcontroller a needs to eject Viewcontroller b , let the user enter some content, when the user input completes,viewcontroller B needs to return the content to viewcontroller A. At this point,the delegate member variable of the View Controller is usually a weak reference to avoid two Viewcontroller cross-referencing each other causing circular citation questions.
Use Xcode's instruments to detect loop primers
: Product, and then select "Leaks",
2. Memory tube for Core Foundation objects Management
for Core Foundation object, because it is not under ARC Management, we still need to continue the previous manual management of reference counting method
- Cfretain and Cfrelease Two methods, the reader can intuitively think that this is equivalent to the retain and release methods of the Objective-c object.
So for the underlying Core Foundation object, we only need to continue to manually manage the reference count in the previous way.
This introduces bridge-related keywords, and the following are descriptions of these keywords :
- __bridge: Do only type conversions, do not modify the reference count of related objects, the original Core Foundation object needs to call the Cfrelease method when not in use .
- __bridge_retained: After the type conversion, the reference count of the related object is + 1, and the original Core Foundation object needs to call the Cfrelease method when it is not used .
- __bridge_transfer: After a type conversion, the reference count of the object is given to ARC management, and the Core Foundation object no longer needs to call the Cfrelease method when it is not in use .
iOS Learning memory Management