First, the basic principle 1. What is memory management
1> mobile device memory and its limited, each app can occupy a limited amount of memory
2> when the app consumes more memory, the system issues a memory warning, which requires you to reclaim some unnecessary memory space. such as retrieving objects and variables that you do not need to use, etc.
3> Memory Management Scope: Any object that inherits NSObject, invalid for other base data types (int, char, float, double, struct, enum)
2. Basic structure of the object
1> 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
2> 4 bytes of storage within each OC object to store reference counters
3. The role of reference counters
1> When you create a new object using Alloc, new, or copy, the reference counter for the object defaults to 1.
2> 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
1> sends an retain message to the object, allowing the reference counter value +1(the Retain method to return the object itself)
2> send a release message to the object, you can make the reference counter value -1
3> can send an Retaincount message to an object to get the current reference counter value
5. Destruction of objects
1> when an object has a reference counter value of 0 o'clock, it is destroyed and the memory it consumes is reclaimed by the system
2> when an object is destroyed, an DEALLOC message is automatically sent to the object
3> will generally rewrite the Dealloc method, releasing the related resources here, dealloc like the object's last words.
4> Once you have overridden the Dealloc method, you must call [Super Dealloc] and put it on the last call to not call the Dealloc method directly
5> 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)
II. Memory management principles 1. Who created, who release
1> If you create an object by Alloc, new, or [mutable]copy], you must call release or Autorelease
2> in other words, you don't create it, you don't have to go [auto]release
2. Who retain, who release
1> as long as you call retain, no matter how this object is generated, you have to call release
3. Summary
1> the beginning and ends, there is a reduction
2> once let the object counter +1, it must be at the end to let the object counter-1
Third, set method memory management
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];
}
}
Implementation of the 2.dealloc method
-(void) dealloc
{
[_book release];
[Super Dealloc];
}
3. Referencing each other
1> Cycle 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
2> Solutions
When both ends are referenced, one end should be retain, one end with assign
Iv. Autorelease1.autorelease
1> an autorelease message is sent to an object, the object is added to an auto-release pool
2> when the auto-release pool is destroyed, a release message is sent to all objects inside the pool
3> calls the Autorelease method does not alter the object's counter, and it returns the object itself
4> Autorelease actually just delays the call to release, and for each autorelease, the system simply puts the object into the current Autorelease pool, and when the pool is released, the All objects in the pool are called release
2. Automatic release of Pool creation
@autoreleasepool
{
....
}
1> during a program run, you can create multiple auto-release pools, which are in memory in the form of stacks
2> OC Object only needs to send a autorelease message, this object will be added to the nearest auto-release pool (the release pool at the top of the stack)
3. Rules
1> generally, objects created in addition to Alloc, new, or copy are declared Autorelease
2> such as the following objects are already autorelease, no need to release
NSNumber *n = [NSNumber numberwithint:100];
NSString *s = [NSString stringwithformat:@ "Jack"];
NSString *s2 = @ "Rose";
OC language-memory management