A basic principle 1. What is memory management
Mobile devices have limited memory, and there is a limit to the amount of memory each app can occupy.
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 that you do not need to use, variables.
Management scope: Any inherited NSObject, 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 that represents "the number of times an object is referenced", that is, how many classes are using the OC object.
There are 4 bytes of storage space within each OC object to store reference counters.
Local variables are placed in the stack , and dynamic variables (objects) are placed in the heap . The code block executes, the local variables inside the stack are destroyed, but the contents of the heap are dynamically distributed, are not destroyed, must be destroyed manually, and will cause a memory leak if not destroyed. (There is no pointer variable in Java, then the object will be automatically recycled)
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 counter is not 0, the memory it consumes will not be recycled until the whole program is running.
4. Actions that reference counters
Sends an retain message to the object, allowing reference counter +1 (the Retain method to return the object itself).
Send an retain message to the object to make reference to the 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.
The Dealloc method is generally overridden to release related resources here. Dealloc is like an object's last words.
The Dealloc method is generally overridden, and you must call [Super Dealloc] and put it on the last side of the call.
Do not call the Dealloc method directly.
Summarize:
1) The Retain counter plus 1 returns the object itself.
2) Release: Counter minus 1, no return value.
3) Retaincount: Gets the value of the current counter.
4) Dealloc: When the object is recycled, it is called.
Be sure to call [Super Dealloc], this call is put to the end.
5) Zombie object: The occupied memory has been reclaimed and zombie objects cannot be used.
6) Wild pointer: pointer to zombie object (memory not available), send message to wild pointer error.
7) NULL pointer: No pointer to anything (the stored thing is nil, NULL, 0), sending a message to a null pointer does not give an error .
Reference knowledge:
Http://www.cnblogs.com/lianghui66/archive/2012/11/13/2768281.html
Http://www.cnblogs.com/iflewless/p/3912604.html
Http://www.cnblogs.com/langtianya/p/3722129.html
iOS Basics (10) memory management