ARC mechanism, arc
I. Basic Introduction
- ARC is a new feature added since iOS 5, completely eliminating the hassle of manual memory management. The compiler will automatically insert appropriate retain, release, and autorelease statements in appropriate places. You no longer need to worry about memory management, because the compiler handles everything for you.
- ARC is a compiler feature, not a runtime feature of iOS. It is not similar to the garbage collector in other languages. Therefore, the performance of ARC is the same as that of manual memory management, and sometimes it can be faster, because the compiler can also execute some optimizations.
Ii. Basic Principles 1. Rules
The ARC rule is very simple: as long as there is a strong pointer variable pointing to the object, the object will remain in the memory
2. Strong pointers and weak pointers
- By default, all instance variables and local variables are Strong pointers.
- After the objects pointed to by the weak pointer are recycled, the weak pointer will automatically change to the nil pointer without triggering a wild pointer error.
Iii. Usage notes
- Release, retain, autorelease, and retainCount cannot be called.
- You can override dealloc, but cannot call [super dealloc].
- @ Property: If you want to have an object for a long time, use strong and weak for other objects.
- Other basic data types still use assign
- When the two ends reference each other, one end uses strong and the other end uses weak