------- iOS training, Android training, Java training , and looking forward to communicating with you! ----------
Management scope: All class objects that inherit NSObject
Memory leaks? The consequences of a memory leak?
Self-application of memory, no release will make the memory of a lot of garbage, waste unnecessary memory
1. Apply the counter:Used to calculate the number of times an object is used, which is an integer (each object has its own reference counter:
accounted for 4 bytes) (1) When you create a new object with Alloc, new, or copy, the reference counter for the new object is set to 1. (2) When the reference counter is 0 o'clock, the memory occupied by this object is reclaimed. (When sending the release message, the counter minus 1) (3) sends an RETAINCOUNT message to the object to get the current counter value Note: The Retain method returns the object itself (with a return value)
2. Destruction of Objects(1) The reference counter is 0, and the memory occupied by the object is retracted by the system. (The object becomes a zombie object.) Pointer to this method has a value of 0) (2) When the object is destroyed, the system automatically sends an DEALLOC message to the object. (Object words) (3) The Dealloc method is generally overridden. (You must call Super's Dealloc)
1 -(void) dealloc2 {3 [Super Dealloc]; 4 }5
3. Concept(1) Wild pointer: A pointer to a zombie object (memory not available). Note: Send a message to the wild pointer error, error hint: exc_bad_access (2) Zombie object: The object occupied by the memory has been reclaimed, zombie objects can no longer be used, (3) NULL pointer: No pointer to anything (stored things are nil, NULL, 0) Note: Sending a message to a null pointer does not give an error
4. Memory Management principles:(1) When you want to use (occupy) an object, you should let the object counter +1 (Let the object do a Ratain operation) (2) When you do not want to use (occupy) an object, you should let the object's counter-1 (Let the object do a release operation) (3 Who retain, who release, who Alloc, who release. Note: When an object is assigned a value in the Set method, a retain operation should be performed. (Basic data types do not need to manage memory)
5.set method Memory Management(1) If Alloc is called, release (or autorelease) is required and release is not required if the object is not generated through Alloc. (2) Set method Code Specification 1) Basic data type: Direct assignment (basic data types do not require memory management)
1 -(void) Setage: (int) age2 { 3 _age = Age ; 4 }
2) OC Object type
1- (void) Setcar: (Car *) Car2 { 3 if(Car! = _car)//1. First determine whether the new incoming object4 { 5[_car release];//2. Do a release of the old object once6_car = [car retain];//3. Do a retain on a new object7 } 8}
(3) DEALLOC Code specification 1> must have [super Dealloc], and put it to the end. 2> do a release for other objects owned by self (current)
1 -(void) dealloc 2 { 3 // Once the person is released, the car should also release4 // This paragraph must put the last 5 }
[email protected] memory management @property (parameters) object eg: @property (nonatomic,retain) Car *CAR; &NB Sp (1) Set method memory management related parameters retain: The generated setter method will release the old value, retain the new value. (For OC object type) Assin: Direct Assignment (default, for non-OC object types) Copy:release old value , copy the new value. (2) whether to generate set method readwrite: Read and write, and generate a setter and getter Declaration, Implement (default) readonly: Read only, generate getter declarations, implementations. (3) multithreading management (multiple threads calling a method at the same time) Nonatomic: Represents the method do not consider thread security issues, telling the system not to generate multithreaded code in the Set method. (High performance, prohibit multi-threading, recommended) atomic: Delegates lock the method to ensure thread safety (default, low performance) & nbsp; thread protection mechanism: prevents the method from being called by another thread when it is not written, resulting in a data error. (4) name of setter and getter method setter:setter = method name, determines SE The name of the T method, the method name must have a colon: &nBsp getter: Determines the name of the Get method. (generally used in type bool)
7. Cyclic retain and @class1> (1) @class usage: function: @class card just tells the compiler that the card is just a class use case: For. H declares this class, but does not introduce methods and member variables of this class. Note: Simply declaring this class does not import methods and member variables of this class, and if necessary, you should #import this class in the. m file. (2) Refer to the specification of a class 1> in the. h header file with @class to declare the class 2> in the. m source file with #import to contain all the things of the Class (3) @class Advantages 1> The solution loop contains The problem. -> improves performance by referencing only in. m source files (which can be looped, declared in a b,b declared in a) 2>. -If the header file of the introduced class is modified, it is not necessary to recompile all. (4) Differences between @class and #import1> #import方式会包含被引用类的所有信息, including variables and methods for referenced classes; (use all information about the class, including member variables and methods)
@class Method simply tells the compiler to declare a class in the A.h file , without knowing all the specific information about the class. (Just declare that there is this class) 2> If there are hundreds of header files,#import了同一个文件, then once the header file of the imported file is slightly changed, the following reference to thisThe header files of all the classes of the file need to be re-copied and less efficient. (A->b,b->c,c->d ..... Once a is changed, it needs to be recompiled later. Program only compiles. h files) using theThis problem does not occur @class way, and it improves efficiency by simply modifying the class in the source file. (Fixed loop containment problem) 3> inin the. m implementation file, you also need to use #import方式引入被引用类 if you need to refer to a member variable or method of the referenced class .
2> Both ends of the circular reference solution (loop retain)
One end with retain, one end with assign. , Car end: @property (nonatomic,retain) person *person; Person side: @propertor (nonatomic,assign) Car *car
8. AutoreleaseEg:person *p = [[Person alloc] init] autorelease]; There must be a release pool in order to write Autorelease function: 1>autorelease will place the object in an auto-release pool, and when the auto-free pool is destroyed, it will be released once for all objects in the pool. 2> returns the object itself without affecting the object counter. Note: When the pool is destroyed, the object is not necessarily destroyed, just once release. Release pool can be nested free pool @autoreleasepool {//{stands for Create release Pool}//} represents the destruction release advantage:1> not in the Care object release The time. 2> does not need to be concerned with what to call release. Disadvantage: The object destruction time cannot be controlled precisely. Use note:1> objects that occupy large memory do not use Autorelease (the object destruction time is not precisely controlled) 2> use of objects with less memory will not have much effect.
Dark Horse programmer-oc-Memory Management (non-arc mode)