Ios learning notes memory management, ios learning notes
1. Memory Management Type Definition
1. Any C type of the basic type. For example:
Int, short, char, long, long, struct, enum, union, etc. are basic types or struct
Memory Management is invalid for the basic type of C Language
2. OC type (non-basic type)
Any object inherited from the NSObject class belongs to the OC type.
That is, other types except C
Ii. OC object structure
All OC objects have a counter that retains the number of referenced objects. If the counter is 0, the object is actually released.
Iii. alloc retain release function
1. The alloc function is used to create an object. After creation, the counter is 1 (only once)
Retain is the counter + 1 for an object (can be called multiple times)
Release is an object counter-1 (when it is reduced to 0, it will be released from the memory)
2. The reference counter is implemented in the oc class, and the object knows the number of times currently referenced.
If you need to reference an object, you can send a retain message to the object, so that the object counter is + 1;
If you do not need to reference an object, you can send a release message to the object so that the object counter is-1;
When the counter is reduced to 0, the dealloc function of the object is automatically called, and the object will release the memory;
Objects whose counter is 0 cannot use release or other methods.
4. automatically release the pool
The automatic release pool is a memory automatic recovery mechanism of OC. Some temporary variables can be recycled and released through the automatic release pool.
When the automatically released pool is destroyed, all objects in the pool will perform a release operation.
Any OC object that calls the autorelease method puts the object in the Auto Release pool closest to itself (the release pool at the top of the stack ).
5. objects created using alloc, retain, and copy cannot be released using release.