1. Why memory management?
If the system resources are limited, 30 mb of memory will be allocated to each program. If the memory usage exceeds 20 mb, the system will receive a memory warning. If the memory usage exceeds 30 mb, the sorry system will terminate your application.
1) Memory Management Mechanism
Objc provides a mechanism to implement the logic model mentioned above. It is called "reference count" (retain
Counting ):
Each object has a reference count (retain count)
When an object is created, the reference count value is 1.
When the reference count value is 0, the object will be destroyed by the system (call the delloc method)
[Retaincount] returns the value of retaincount.
2) how to obtain ownership:
Alloc: allocates memory for a new object, and its reference count is 1. By calling the alloc method, you have ownership of the new object.
Copy: Creates a copy of an object. The reference count of this copy is 1, and the caller has ownership of the copy.
Retain: Adds 1 to the reference count of the object and obtains the ownership of the object.
3) How to discard ownership:
Release: reduce the reference count of an object by 1 and discard the ownership of the object.
Autorelease: reduces the reference count of an object by 1 at some time in the future (when the pool is automatically released), and gives up the ownership of the object at that time.
2. Memory Management Principles
Responsible for the objects you own. You can only release the objects you own.
All objects that have obtained ownership through retain, alloc, copy, and other means must call release, autorelease, and other means to release their ownership when you are not using him.
Within a certain segment of code, the number of times that alloc and retain perform the copy operation on the same object should be equal to the number of release and autorelease operations.
You can release your occupied instance variables in the dealloc method of the class.
For the convenience constructor and access, you do not obtain the ownership of the object through the above means, so in these cases, you do not need to release the object.
Autorelease only means "sending a release message with a predetermined delay", and the current reference counter has not changed
1) Attribute and Memory Management
. H file
@ Property (retain, non atomic) nsstring * Name; // The attribute declared by name is retain, which must be release in delloc.
. M file
@ Synthsize name = _ name
-(Void) dealloc
{
[Name Release]; // release attributes
[Super dealloc];
}
2) convenient constructor Memory Management
Autoreleasepool)
As long as the object uses autorelease, it is added to the Auto Release pool.
When the automatically released pool release is added to the automatically released pool, it will be release, and the automatically released pool will also be destroyed. Drain is used to release the object release in the released pool.
Autorelease is rarely used, and features are generally used in the convenience constructor.
Ios5 new arc (automatic memory release)