OBJECTIVE-C provides three ways to manage memory: manual retain-release (MRR, manual Management), automatic reference counting (ARC, auto reference count), garbage Collection (garbage collection). iOS does not support garbage collection; As Apple's new technology, Apple recommends that developers use ARC technology to manage memory; This note is mainly about manual management.
The purpose of memory management is to:
1. Do not release or overwrite the memory that is still in use, which can cause the program to crash;
2. Release memory that is no longer in use and prevent memory leaks. The memory resources of iOS programs are valuable.
The MRC manually manages memory based on reference counting, but requires the developer to send a message to a block of memory (or object) to change the reference count for memory management (the ARC technology is the compiler to do the job instead of the developer). A piece of memory if the count is zero, i.e. no user (owner), then the OBJECTIVE-C environment will automatically reclaim the memory.
OBJECTIVE-C's memory management follows this simple strategy:
Note: The operation of the reference count plus 1 in the document is called "owning" (own, or take ownership of) an object/memory; The operation of reducing the reference count by 1 is called discarding (relinquish) the object/memory. When you have an object, you can safely read and write or return the object, and when the object is discarded by everyone, the OBJECTIVE-C environment reclaims the object.
1. You have the object you created
This means that the initial reference count for the created object (using methods such as Alloc,new,copy or mutalbecopy) is 1.
2. After sending the retain message to the object, you have the object
3. When you do not need to use the object, send release or autorelease message discard this object
4. Do not send "discard" messages to objects that you do not own
iOS memory management