One. Memory management includes memory allocation and memory cleanup
1. Scope of memory management : People and objects inheriting from the NSObject class need to be memory-managed, any object of non-object type (base data type such as int char float double struct Enum, etc.)
2. Why are only OC objects required for memory management?
The OC object is placed inside the heap non-OC objects (basic data types such as int char float double struct Enum, etc.) are generally placed inside the stack and the stack memory is automatically reclaimed by the system
3. Heap and Stack
Stack (operating system): Has the system automatically allocates the release, holds the function the parameter value, the local variable value and so on. It works like a stack in a data structure (advanced back-out)
Heap (operating system): usually manually assigned by the programmer release, if the programmer does not release, the program will be collected at the end of the system, distributed in a manner similar to the list
4. Reference counters
Each OC object has its own reference counter that can literally be interpreted as "the number of times an object is referenced" and it can be understood that there are many colds people using this object
The role of the reference counter: The reference counter indicates how many people are currently using this object and the system reclaims the object when no one is using it. That is, when the object's reference count is 0 o'clock, the memory used by the object is reclaimed by the system. If the system reference count is not 0 then during the entire program run, The memory he occupies cannot be recycled (unless the entire program is rolled out)
Any object that has just been created using the reference count is 1 when you create an object using Alloc init copy, the object's reference count defaults to 1
5. How to Manipulate reference counters
Send an retain message to the object to make the value of the reference counter +1
Sending a release message to an object causes the value of the reference counter to be -1, and release does not mean that the Destroy \ Recycle object is simply a reference count value-1
Sending a retaincount message to a one or two object can get a reference counter value for the current object sometimes inaccurate and seldom used because of a common error
It is important to note that release does not mean that the Destroy \ Recycle object is merely a reference count value-1 only if the reference counter value is reduced to 0.
6.dealloc method
When an object's reference counter is 0 o'clock, the object is about to be released and the memory it consumes is reclaimed by the system
When the object is about to be destroyed, the system automatically sends an DEALLOC message to the object (therefore, there is no call from the Dealloc method.) You can tell if the object is destroyed.
The Dealloc function is a function called by the system. It is not intended to be called directly by a programmer. In general, the Dealloc method is overridden to release the related resources, and the overridden Dealloc method is equivalent to the object's last words, which are used to deal with the objects after the object is destroyed. Once the Dealloc method is rewritten, You must call [Super Dealloc] and put it in the last call of the Dealloc method.
Note: The Dealloc method is the method that the system calls, the programmer cannot call directly, need to rewrite the Dealloc method. Once the object is recycled, the memory it consumes is no longer available, and if persisted it causes the program to crash (wild pointer error)
Two. MRC (Manual Reference counting): All objects need to be manually managed by programmers, requiring programmers to write their own release/retain and other methods
1. The principle of memory management : There's a minus once alloc one release at a time retain one release
2. Wild pointer null pointer zombie object
Zombie object: As long as an object reference count of 0 is released, this object is called a Zombie object
Wild pointer: When an object points to a zombie object, we will make this pointer a wild pointer (a pointer to an object with a reference count value of 0 is the pointer to the wild pointer). If you send a message to the wild pointer, you will get an error exc_bad_access
The monitoring of zombie objects must be turned on in enterprise development
Null pointer: In order to avoid giving the wild pointer message error, when an object is released we will set the object to null pointer nil/0. Because sending a message to a null pointer does not result in an error.
3. Multi-Object Memory management
If you want to use the C object in the A object and the B object, be sure to do a retain on the C object in the A and B objects. When a and B are used up, C is also released. In general, the retain of A and B to C are placed in the call function of A and B to C, The release of A and B to C is placed in the Dealloc method. The rest of a B C's self-release is in the original main function.
Just like several people enter a game hall to play games, each in a person to let the hall record +1, when someone left, the hall will be 1. When the number of the hall was recorded at 0 o'clock, the hall was released. As long as there are people, even a person can not release the hall.
[Email protected] modifier
ReadOnly: Generate Getter Method only
ReadWrite: Generate Getter/setter method At the same time, not write by default is ReadWrite
Getter: You can name the generated gutter method
Setter: Can name the generated setter method
Retain: will automatically help us generate code for Setter/gettter method memory management
Assign: The memory management method that does not generate the setter method for us, just produces the ordinary Gettter/setter method, the default does not write anything is assign
[Email protected]
@class can simply refer to a class that tells the system that this is a class that can be used with confidence and boldness.
For specific use, use the @class in the. h file to use the #import包含这个类的. h file in the. m file. @class do not copy this class just tell the compiler that this is a class that can be used. #import will copy the tired header file.
When using the @class, @class in the. h file and #import in. m files, not omitted.
Using @class for a relationship since the loop can avoid a dead loop because the @class in the. h file does not make any copies, and the #import in the. m file only copies the corresponding files, and does not form a dead loop.
6. Cyclic retain
If a object is to have a B object, and the B object also has a object a. A cyclic retain is formed at this time
Workaround: Let one side not do the retain operation
7.autorelease
Whenever a autorelease message is sent to an object, the object is placed in an auto-release pool, and when the auto-free pool is destroyed, all objects inside the pond are released once.
Note: This only sends a AUTORELEASE message, and if the reference count value at that time is 0, the object is destroyed, otherwise the object will not be freed. When you finish calling the Autorelease method, the object's reference count value does not change
Autorelease principle: Autorelease Actually just put the call to release delay, for each autorelease system just put the object into the current automatic release pool, block automatic release pool is released, all the objects in the eating will be release
Three. ARC (Automatic Reference counting): Does not require the programmer to manage the memory, the compiler will automatically give the program Bill House Release/retain and other code
1. Attention Points
The garbage collection mechanism in iOS is different. Arc in iOS is the compiler in the compilation phase automatically recognize the addition of Release/retain, and other methods in code execution code, Java garbage collection mechanism is the system in the Run program is timed to detect garbage and recycling, is the system to do .
the principle of 2.ARC judgment
When using arc, the object remains in memory as long as there is a strong pointer variable pointing to the object
Strong pointer: A pointer modified by __strong, if there is no modifier, the default is a strong pointer, the strong pointer to the object will remain in memory
Weak pointers: __weak decorated pointers, objects pointed by weak pointers are released immediately after creation, unable to save
Circular references in 3.ARC
In Arc if a owns B,b owns a, one with strong one with weak, a weak pointer must be used by a party
@property parameters in 4.ARC
Strong: For OC objects, equivalent to retain in MRC
Weak: For OC objects, equivalent to assign in MRC
Assign: For basic data types, equivalent to assign in MRC
IOS Memory Management