I. OC memory management process
OC provides an internal counter for each object. This counter tracks the reference count of objects. when an object is created or copied, the reference count is 1. Each time an object is maintained, the retain interface is called, add 1 to the reference count. If you do not need this object, call release. The reference count is reduced by 1. When the reference count of the object is 0, the system will release this memory, call dealloc to release an object.
For example:
1. when an object is created, a reference counter is automatically created internally. This counter is the only basis for the system to determine whether to recycle objects. When the reference count retaincount is 0, the system will not hesitate to recycle the current object
2. [object release] reatincount-1
3. [object retain] reatincount + 1, return self
4. The objects with reference count retaincount = 0 will be destroyed.
5. dealloc function. when an object is to be destroyed, the system automatically calls the dealloc function to notify the object that you are about to be destroyed.
Memory Management Principle (pairing principle): As long as new, alloc, and retain appear, a release and autorelease will be paired.
2. Memory Management for a single object
// Defines a person class // person. h @ interface person: nsobject @ property int age;-(void) run; @ end // person. m-(void) dealloc {[Super dealloc]; nslog (@ "person destroyed");} // rewrite the description method-(nsstring *) description {return [nsstring stringwithformat: @ "age = % d", _ age];}-(void) run {nslog (@ "people are running ");}
So I started the experiment in the main function.
Test 1:
Void test1 () {// retaincount = 1 person * P = [[person alloc] init]; // retaincount = 0 // The system has recycled the object pointed to by P. // exc_bad_access has accessed the inaccessible memory space. // The object recycled by the system is called a zombie object // by default, to improve the coding efficiency, xcode, the zombie object [P release] is not always checked; // The run method [p run] cannot be called;}
Conclusion: After the system recycles an object, the object becomes a zombie object and the object method cannot be called.
Solution: If you are sure that the currently acting object will no longer be used, to prevent the wild pointer operation, we usually assign the unused pointer variable to nil P = nil;
Test 2
Void Test2 () {// Memory leakage first case // 1 person * P = [[person alloc] init]; p. age = 20; nslog (@ "% @", P); // 2 [p retain]; // 3 [p retain]; // 2 [P release]; // only the retaincount of the object is required! = 0 will always exist in the memory // Memory leakage refers to the objects that are no longer in use, no memory is destroyed. // Memory leakage. Case 2 // retaincount = 1 person * P = [[person alloc] init]; p. age = 20; [p run]; P = nil; [P release]; // [nil release]; */
Conclusion: Two memory leaks are listed in the above experiment. 1. retaincount is not equal to 0 2. Empty pointer release
Test 3:
void test4(){ //1 Person * p = [[Person alloc] init]; p.age = 20; NSLog(@"%@",p); //0 [p release]; [p retain];}
Conclusion:
When the retaincount of an object is already 0, the retain method is called, which will not bring the object back to life, and a wild pointer operation exception will also occur.
3. Memory Management for multiple objects
Just now there is only one person object. Now we add a car object so that the person class contains the car class.
@interface Person : NSObject{ Car * _car;}- (void)setCar:(Car *)car;- (Car *)car;- (void)drive;@end
To prevent memory leakage, the car is also destroyed when the person class is destroyed, so the person class's
-(Void) dealloc {// The purpose is to ensure that when the P object exists, the car object must exist // when the P object is destroyed, [_ car release] [Super dealloc]; nslog (@ "person destroyed ");}
4. Mutual reference between classes during manual Memory Management
@property(nonatomic,retain) Car * car;@property (nonatomic,assign)Person * person;
When two classes reference each other, you must use assign to indicate weak references. If both classes use retain, mutual references will occur !!
Dark Horse programmer-memory management