Basic Principles:
1. What is memory management?-The memory of mobile devices is extremely limited, and each app can occupy a limited amount of memory.-When the app occupies a large amount of memory, the system will issue a memory warning, in this case, we need to reclaim some memory space that is not needed, such as the unneeded objects and variables.-"
Management scope: any object that inherits nsobject. Other basic data types (INT, Char, float, double, struct, Enum, etc.) are put in the stack space in the memory, objects in the heap space are dynamically allocated and need to be manually recycled. We don't need to worry about stack space.
2. Basic Object Structure
Each OC object has its own reference counter, which is an integer that indicates the "Number of times the object is referenced", that is, how many people are using this OC object. (Allocate 4 bytes of storage space to store reference counters ). When an object is generated, the reference counter is 1.
3. Function of referencing counterWhen you use alloc, new, or copy to create a new object, the Reference Counter of the new object is 1 by default.
When the reference counter value of an object is 0, the memory occupied by the object will be recycled by the system. In other words, if the counter of an object is not 0, the memory occupied by the object will not be recycled throughout the program running, unless the entire program has exited.
4. Reference Counter operations
-"Send a retain message to the object. The reference counter value can be + 1 (the retain method returns the object itself)
-Send a release message to the object. The reference counter value is-1 (no return value)
-> You can send a retaincount message to an object to obtain the current reference counter value.Turn off arc (automatic reference counting): select the project name → building settings → levels → Apple llvm 6.0-language-Objective C and change the following to no
Nsuinteger: equivalent to long. % LD is used as the placeholder for output.
Zombie object: an object that references a counter to 0, which cannot be used. It is called a zombie object.
Wild pointer: pointer to a zombie object (memory unavailable). An error is returned when a message is sent to the wild pointer.
Null Pointer: no pointer pointing to anything (nil, null, and 0 are stored). No error is returned when messages are sent to null pointers.Error:
Exc_bad_access: access to a bad memory (recycled and unavailable );To prevent access to bad memory errors, we can assign a null value to the pointer after the counter value of the object is reduced to 0.
P = nil; // convert pointer P to NULL pointer
No NULL pointer error exists in OC. No error is reported when a message is sent to the NULL pointer.
-[Person setage:]: Message sent to deallocated instance 0x100202230
A setage: message is sent to the released object.
5. Object destruction-When the reference counter value of an object is 0, it will be destroyed and the memory occupied by it will be recycled by the system.-when an object is destroyed, the system will automatically send a dealloc message to the object-"
The dealloc method is rewritten to release related resources. dealloc is like the last words of an object.-"
Once the dealloc method is rewritten, you must call [Super dealloc] and put it at the end of the call.-"
Do not directly call the dealloc Method-"Once an object is recycled, the memory occupied by it cannot be reused. sticking to it will cause the program to crash (the wild pointer is incorrect)
Memory Management PrinciplesIf you perform a retain operation on an object, you need to release the object at the end of use.
Who created and who release.If you create an object through alloc or new, you must call release
Who retain, who release.If you call retain, no matter how the object is generated, you must call release. You want to use (occupy) an object, you should make the counter of the Object + 1 (let the object do a retain operation) You do not want to use (occupy) an object, let the object designer-1 (let the object do a release) Get the member variable value in the class implementation method: _ speed: directly access the member variable self-> _ speed: directly access the member variable self. speed: Get method [self speed]: Get Method
Memory Management Code specification:
1. As long as alloc is called, there must be release (aotorelease)
If the object is not generated through alloc, release is not required.
2. Code specification for the Set Method
-Basic Data Type: direct value assignment
-(Void) setage :( INT) Age
{
_ Age = age;
}
-OC object type
-(Void) setcar :( car *) Car
{
// 1. Determine if it is a new object.
If (car! = _ Car)
{
// 2. Perform a release operation on the old object
[_ Car release];
// 3. Make a retain for the new object
_ Car = [Car retain];
}
}
3. Code specification of the dealloc Method
-"Must be [Super dealloc] and placed at the end
-Perform a release for other objects owned by self (current ).
-(Void) dealloc
{
[_ Car release];
[Super dealloc];
}
@ Property Memory Management
@ Property (retain) Book * book;
The generated set method is as follows:
-(Void) setbook :( book *) book
{
If (_ book! = Book)
{
[_ Book release];
_ Book = [Book retain];
}
}
1. Set Method Memory Management parameters:
Retain: Old value of release, new value of retain (applicable to OC object type)
Assign: direct value assignment (default, applicable to non-OC object types)
Copy: Old value of release, new value of copy
2. Whether to generate the Set Method
Readwrite: generate the declaration and implementation of setter and getter simultaneously (default)
Readonly: Only getter declarations and implementations are generated.
3. multi-thread management
Nonatomic: do not add multi-threaded code when generating the set method. High Performance (this is generally used)
Atomic: Add multi-threaded code when generating the set method. Low performance (default)
4. Names of setter and getter Methods
Setter: determines the name of the set method. There must be a colon (not commonly used)
Getter: determines the name of the get method.
(Generally used in the bool type)
@ Property int age; the generated method is age, setage:
@ Property (getter = ABC, setter = setabc:) int age;
Generated Method Name: Get method: ABC
Set Method: setabc:
Long indicates how many milliseconds have elapsed since 00:00:00, January 1 ,.
Loop reference:
When circular references are involved (you include me and I have you), use the @ Class class name when adding references.
1. @ Class: only tells the compiler that a name is a class.
@ Class card; // only tells the compiler that card is a class.
2. Reference a class specification during development
-In the. h file, @ class is used to declare classes.
-# Import is used in the. M file to include everything of the class.
Using @ class can improve compiler compilation efficiency (when a class is referenced by multiple classes, if the class changes, you need to copy it again)
3. Loop reference solution at both ends:
-Retain is used at one end.
-Assign
Autorelease: semi-automatic release
@ Autoreleasepool
{// {Start indicates that the release pool is created.
Person * P = [[person alloc] init] autorelease];
P. Age = 19;
} //} Indicates that the release pool is destroyed.Note: @ autorelease can be nested.
Stack (Advanced and later)Method of storage 1. The basic usage of autorelease-the object itself will be returned-the object will be placed in an automatic release pool-when the automatic release pool is destroyed, A release operation will be performed on all objects in the pool-"person * P = [[person alloc] init] autorelease]; after the autorelease method is called, the counter of the object remains unchanged. 2. Advantages of autorelease-no need to worry about the time when the object is released-no need to worry about when to call Release 3. Notes for using autorelease-a large memory usage do not use autorelease-to use autorelease for objects that occupy less memory, no major impact 4. incorrect syntax-call autorelease after alloc, and call release @ autoreleasepool again
{Person * P = [[person alloc] init] autorelease];
[P release];}-"Autorelease @ autoreleasepool {person * P = [[[person alloc] init]
Autorelease];}
5. automatically release the pool-countless pools will be created when the IOS program is running. These pools all exist as stack results (Advanced and later)-when an object calls the autorelease method, this object will be placed in the release pool at the top of the stack. 6. automatic release pool creation method-before ios5.0
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; person * PP = [[[person alloc] init] autorelease]; [pool release];
-Beginning with ios5.0 @ autoreleasepool
{Person * P = [[[person alloc] init] autorelease];}
1. The methods provided by the system do not contain alloc, new, or copy. This indicates that all returned objects are the nsstring * STR = @ "13423" of autorelease, the default value is autorelease. Memory Management is not required.
2. Some class methods are often provided in development to quickly create an object that has been autorelisted
-Do not directly use the class name when creating an object. Generally, use self+ (ID) person
{Return [[self alloc] init] autorelease];}
Summary: 1. Basic counter operation retain: + 1 release:-1 retaincount: Get Counter 2. Memory Management of the Set Method-Implementation of the Set Method
-(Void) setcar :( car *) Car {If (_ car! = Car) {[_ car release]; _ car = [Car retain];}
-"Dealloc method implementation
-(Void) dealloc {[_ car release]; [Super dealloc];}
3. @ property parameter-OC object @ property (nonatomic, retain) class name * attribute name; @ property (nonatomic, retain) car * car; @ property (nonatomic, retain) id car;
Attributes that have been retained must be release attributes in the dealloc method.-Non-OC object @ property (nonatomic, assign) type name attribute name; @ property (nonatomic, assign) int age; 4. autorelease-in the built-in method, if alloc, new, and copy are not included, all objects returned by these methods have already been autorelisted [nsstring stringwithform: ......] [nsdate date];-in development, some class methods are often used to quickly create an autorelease object. Do not directly use the class name when creating an object. Use self
Objective-C: 07 _ Memory Management