First, the basic principle 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 takes up a lot of memory, the system issues a memory warning, and you have to reclaim some memory space that you don't need to reuse. such as retrieving some objects, variables, etc. that you do not need to use
- Managed Scope: Any object that inherits the NSObject and is not valid for other basic data types (int, char, float, double, struct, enum, etc.)
2. Basic structure of the object
- Each OC object has its own reference counter, which is an integer representing "the number of times the object is referenced", that is, how many people are using the OC object
- 4 bytes of storage space within each OC object to store reference counters
3. The role of reference counters
- When you create a new object using Alloc, new, or copy, the reference counter for the object defaults to 1.
- When an object has a reference counter value of 0 o'clock, the memory consumed by the object is reclaimed by the system. In other words, if the object's counter is not 0, the memory it consumes will not be recycled throughout the program, unless the entire program has exited
4. Actions that reference counters
- Send an retain message to the object to make the reference counter value +1(the Retain method returns the object itself)
- Send a release message to the object to make the reference counter value -1
- You can send an Retaincount message to an object to get the current reference counter value
5. Destruction of objects
- 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, the system automatically sends an DEALLOC message to the object
- Typically overrides the Dealloc method, releasing the associated resources here, dealloc like the object's last words.
- Once the Dealloc method is overridden, [super Dealloc] must be called and placed on the last side of the call
- do not call the Dealloc method directly
- Once the object is recycled, the memory it uses is no longer available, and sticking to it can cause the program to crash (wild pointer error)
Two, Xcode settings 1. Cancel Arc
To manually invoke retain, release and other methods, do not tick the arc when creating the project
2. Turn on Zombie object monitoring
By default, Xcode does not control zombie objects, and using a piece of freed memory does not give an error. For easy commissioning, zombie object monitoring should be turned on
Three, memory management principles 1. Principle Analysis
- QQ Hall Open Room Principle: As long as the room still someone in use, will not dissolve
- As long as someone else is using an object, the object will not be recycled.
- As long as you want to use this object, let the object's counter +1
- When you no longer use this object, let the object's counter-1
2. Who created, who release
- If you create an object by Alloc, new, or [mutable]copy], you must call release or Autorelease
- In other words, you didn't create it, you didn't have to go [auto]release
3. Who retain, who release
- As long as you call the retain, no matter how this object is generated, you have to call release
4. Summary
- After the beginning, there is a reduction
- Once let the object counter +1, it must be at the end to let the object counter-1
Iv. memory management of the Set method
If you have a member variable of OC object type, you must manage the memory of this member variable. Like a book *_book.
1. Implementation of Set method
-(void) Setbook: (book *) book{
if (book! = _book) {
[_book release];
_book = [book retain];
}
}
2. Implementation of the Dealloc method
-(void) Dealloc {
[_book release];
[Super Dealloc];
}
Five, @property parameter 1. Control the memory management of the Set method
- Retain:release old value, retain new value (for OC object)
- Assign: Direct assignment, no memory management (default, for non-OC object types)
- Copy:release old value, copy new value (typically used for NSString *)
2. Control need not generate set method
- ReadWrite: Generate both the Set method and the Get method (default)
- ReadOnly: Only the Get method is generated
3. Multithreading Management
- Atomic: Low performance (default)
- Nonatomic: High Performance
4. Control the name of the set method and the Get method
- Setter: Set the name of the set method, there must be a colon:
- Getter: Set the name of the Get method
Six, circular reference 1. @class
For circular dependencies, for example Class A refers to Class B, and Class B also refers to Class A
This code compilation will error. When using @class in two classes to declare each other, there will be no compilation error
Use @class class name; You can refer to a class to illustrate that it is a class
- The difference between the #import and the
L #import方式会包含被引用类的所有信息, including the variables and methods of the referenced class; @class just tell the compiler that B *b is just a class declaration in the A.h file, what information is there in this class, where you don't need to know, and when the implementation file really needs to be used, To actually see the information in Class B.
L If there are hundreds of header files are #import the same file, or these files are #improt, then once the initial header file changes slightly, the subsequent reference to this file all the classes need to be recompiled again, such efficiency is conceivable, and relatively speaking, This is not the case with the @class method.
In the. m implementation file, if you need to refer to an entity variable or method of the referenced class, you also need to use the #import method to introduce the referenced class
2. Cyclic retain
- For example a object retain a B object, B object retain a object
- This causes the A object and the B object to never be freed
3. Solution
- When both ends are referenced, one end should be retain, one end with assign
Seven, Autorelease1. Autorelease
- When a autorelease message is sent to an object, the object is added to an auto-free pool
- When the auto-free pool is destroyed, a release message is sent to all objects inside the pool
- Calling the Autorelease method does not alter the object's counter, and it returns the object itself
- Autorelease actually just delayed the call to release, and for each autorelease, the system simply placed the object in the current Autorelease pool, and when the pool was released, All objects in the pool are called release
2. Automatic release of Pool creation
@autoreleasepool
{
// ....
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// .....
[Pool release]; or [pool drain];
- During a program run, you can create multiple auto-release pools, which are in memory in the form of stacks
- The OC object only needs to send a autorelease message, which will add the object to the nearest auto-release pool (the release pool at the top of the stack)
3. Application examples
- A comparison with release
L Previously:
Book *book = [[Book alloc] init];
[Book release];
L Now:
Book *book = [[[Book Alloc] init] autorelease];
Do not call [book release] again;
- You can typically add a class method that quickly creates an object for a class
+ (ID) book {
return [[[Book Alloc] init] autorelease];
}
When you call [book Book], you don't have to consider when to release the returned book object
4. Rules
- In general, objects created in addition to Alloc, new, or copy are declared Autorelease
- For example, the following objects are already autorelease, no need to release
NSNumber *n = [NSNumber numberwithint:100];
NSString *s = [NSString stringwithformat:@ "Jack"];
NSString *s2 = @ "Rose";
OC Memory Management Manual memory management MRC (mannulreference counting)