Dark Horse programmer------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
For object-oriented language, the program needs to constantly create objects. Initially, all programs that are created usually have pointers pointing to it, programs may need to access their instance variables or methods to invoke them, and as the program executes, the program creates new objects again, and those old objects are no longer called, and there are no pointers pointing to them. If the program does not reclaim the memory they occupy, a memory leak occurs. If the program has been leaking memory, then the available memory will be less, until there is not enough memory, the program will crash.
Currently, there are two main techniques for managing memory, one is reference count and the other is garbage collection. The iOS platform currently only supports reference counting, and Mac platforms support garbage collection.
The reference count records the number of times the object is currently referenced by maintaining a reference counter for each object. When a reference is added to an object, the counter is incremented by 1, the counter loses 1 when a reference is lost, and the object is automatically reclaimed when the reference count is 0 o'clock, marking the end of the object's life cycle.
The reference technology for iOS is divided into manual reference count and Auto reference count (ARC).
1 Manual reference count
The manual reference count, which is the reference count of the right developer's own control object, is related to the following methods:
1. When you create a new object using Alloc, new, or copy, the new object's reference counter defaults to 1.
2, send an retain message to the object, you can make reference counter value +1
3, send a release message to the object, you can make reference counter value-1
4, can send Retaincount message to the object to get the current reference counter value
When an object's reference counter value is 0 o'clock, it is destroyed and the memory it consumes is reclaimed by the system. When an object is destroyed, an DEALLOC message is automatically sent to the object. So it is common to rewrite the Dealloc method, where the related resources are released, dealloc like the object's last words. Once you have overridden the Dealloc method, you must call [Super Dealloc] and put it on the last side of the call. To pay special attention, do not call the Dealloc method directly, or the program will crash when the system calls the Dealloc method.
[OBJC] View plaincopy on code to see the chip derivation to my Code slice//person.h @interface person:nsobject @property int age; @end //person.m @implementation person //When a person object is recycled, this method is called automatically -(void) Dealloc { NSLog (@ "Person object is recycled"); Super Dealloc must be called and put on the last side [Super Dealloc]; } @end //main.c int main () {person *p = [[Person alloc] init]; After Alloc, the object's counter is 1 NSLog (@ "counter:%ld", [P retaincount]); View the reference count value for this object [P release]; Release the object or cause a memory leak return 0; }
2 reference count for member variables
The above is just a simple class of memory management, and if the member variables of a class also refer to other classes, things become complicated. In order to do a good job in memory management, there are 4 basic principles;
1. To use (Occupy) an object, you should let the object counter +1 (Let the object do a retain operation)
2. Do not want to use (occupy) an object, you should let the object counter-1 (Let the object do a release)
3. Who retain, who release
4. Who alloc, who release
person.h,car is already defined class @interface Person:nsobject {Car *_car; }-(void) Setcar: (car *) Car; -(car *) car; @end//person.m @implementation Person-(void) Setcar: (Car *) Car {if (car! = _car)//must be judged, otherwise the object that the _car points to will have a memory leak {//To do a release [_car release] for the vehicle currently in use (old car); Do a retain operation on the new car _car = [car retain]; }}-(car *) car {return _car; }-(void) Dealloc//must override this method, otherwise the object that _car points to cannot be released {//when the person is absent, the representative does not use the car//To do a release operation on the vehicle [_car release]; NSLog (@ "%d-year-old person object is recycled", _age); [Super Dealloc]; } @end int Main () {person *p = [[Person alloc] init]; The reference count for the person object is 1 car *c = [[Car alloc] init]; The reference count for the car object is 1 p.car = c; The reference count of the car object becomes 2 [C release]; The reference count of the car object becomes 1 [p release]; The reference count of the car object becomes the reference count of the 0,person object into 0 return 0; }
3 Synthetic access Methods with memory management
The OC language provides @property, @synthesize directives for simplifying setter and getter methods, The corresponding indicator is also provided for memory management.
1.set method Memory Management related parameters
* retain:release old value, retain new value (for OC object type)
* Assign: Direct assignment (default, for non-OC object type)
* Copy: Release old value, copy new value (typically used for nsstring*)
2. Do you want to generate a set method
* ReadWrite: Generate a setter and getter Declaration, implementation (default)
* ReadOnly: Only the Getter Declaration, implementation
3. Multithreaded Management
* Nonatomic: High performance (usually with this)
* Atomic: Low performance (default)
4.setter and the name of the Getter method
* Setter: Determines the name of the set method, must have a colon:
* getter: Determines the name of the Get method (typically used in bool type)
Person.h,car is an already defined class @interface Person:nsobject {Car *_car; }//Retain: Inside the generated set method, release old value, retain new value @property (nonatomic,retain) Car *car; @end//person.m @implementation Person-(void) Setcar: (Car *) Car/* This code, automatically generated by the system {if (Car! = _car)//must be judged, otherwise _c The object that the AR points to will cause a memory leak {//To do a release [_car release] for the currently in-use car (old car); Do a retain operation on the new car _car = [car retain]; }}-(car *) car {return _car;}/-(void) Dealloc//must override this method, otherwise _car point to the object cannot be released {//when the person is gone, the representative does not use the car// The car does a release operation [_car release]; NSLog (@ "%d-year-old person object is recycled", _age); [Super Dealloc]; } @end int Main () {person *p = [[Person alloc] init]; The reference count for the person object is 1 car *c = [[Car alloc] init]; The reference count for the car object is 1 p.car = c; The reference count of the car object becomes 2 [C release]; The reference count of the car object becomes 1 [p release]; The reference count of the car object becomes the reference count of the 0,person object into 0 return 0; }
23-Dark Horse programmer------OC Language Learning notes---memory management