IOS memory management mechanism is the principle of the reference counting, when the memory is being created, its reference count + 1, said an object or a pointer holding the memory, have the ownership of the memory, if have another object or a pointer to the memory, so in order to show the later of the ownership of the object or a pointer to the memory, Reference counting 1 – > 2, if there is an object or a pointer pointing to no longer after the memory, the reference count – 1, said the object or pointer no longer have the ownership of the memory, when the reference count for a block of memory to 0, said there was no any object or a pointer to hold this piece of memory, system will immediately release the memory.

Alloc, new

Class initialization method, open up new memory space, reference count +1;

retain

Instance method, no new memory space opened, reference count +1;

copy

Instance method, copy an object to the new memory space, the new memory space reference count +1, the old memory does not; There are two types: shallow copy and deep copy. Shallow copy only copies addresses and does not create new memory space. Deep copy is copying content, creating new memory space.

strong

Strong reference, reference count +1;

release

Instance method, release object; Reference count -1;

autorelease

Delayed release; Autoreleasepool Automatic releasepool; When the execution is complete, reference count -1; InitWithFormat and stringWithFormat plus one if the string length is greater than nine;

assign

A weak reference. Weak Is also a weak reference. Assign applies to both objects and primitive data types, but when the object is destroyed, the current pointer to the object will not point to nil. Wild Pointers will be generated. Weak Works only on objects, but not on basic data types. When the pointed object is destroyed, the current pointer to the object will point to nil, preventing the generation of wild Pointers.

IOS development exchange technology group: 563513413, no matter you are big bull or small white are welcome to enter, share BAT, Ali interview questions, interview experience, discuss technology, we exchange learning and growth together!