First, what is memory management
Because the memory of the mobile device is extremely limited, so the memory of each app is also limited, when the app consumes more memory, the system will issue a memory warning, you need to reclaim some no longer need to continue to use the memory space, such as recycling some of the unused objects and variables.
Managed Scope: Any object that inherits NSObject and is not valid for other basic data types.
second, the Operation
The only way to determine whether an object should be recycled is if the counter is 0, or if not 0. So the management of memory is the management of counter
1> Retain: Counter +1 returns the object itself
2> Release: Counter-1, no return value
3> Retaincount: Gets the current counter
4> Dealloc
* When an object is to be recycled, it will be called
* Be sure to call [Super Dealloc], this call to be placed on the last side
Iii. Destruction of objects
When an object's reference counter is 0 o'clock, it is destroyed and the memory it consumes is reclaimed by the system.
When the object is destroyed, the system automatically sends an DEALLOC message to the object, typically overriding the Dealloc method, where the associated resources are freed, dealloc like the object's "last words." Once the Dealloc method is overridden, [super Dealloc] must be called and placed in the last call of the code block (the Dealloc method cannot be called directly).
Once the object is recycled, the storage space he occupies is no longer available, and sticking to it can cause the program to crash (a wild pointer error).
Iv. What is a wild pointer, zombie object
1> Zombie Object: Objects occupied by memory have been reclaimed, zombie objects can no longer be used (exc_bad_access)
2> pointer: A pointer to a zombie object (memory not available). Sending a message to the wild pointer will give an error
3> NULL pointer: There is no pointer to anything (the stored thing is nil, NULL, 0), sending a message to the null pointer does not return the error.
Five, Memory Management code specification:
1. The code specification for the release (Autorelease) 2.set method is required whenever alloc is called
1> Basic Data type: Direct copy
<pre name= "code" class= "OBJC" >-(void) Setage: (int) age{_age = age;}
2> OC Object Type
<pre name= "code" class= "OBJC" >-(void) Setcar: (Car *) car{//1. First determine if the new incoming object is the IF (Car! = _car) {//2. Do it once for the old object release[_ Car release];//3. Do the new object once Retain_car = [car retain];}}
3.dealloc of code
1> must be [Super Dealloc], and put it on the last side.
2> do a release for other objects owned by self (current)
<pre name= "code" class= "OBJC" >-(void) Dealloc{[_car release];[ Super Dealloc];}
OC Memory Management