About Memory Management
Memory management refers to the process in which your program allocates, uses, and releases memory during running. A carefully encoded program always uses as much memory as possible. In object-c, memory management is also seen as a process of reasonably allocating data and code to your limited memory resources. If you read this document carefully, you will be able to reasonably manage the object lifecycle and release the memory that is no longer used by the program in a timely manner.
Memory Management seems to be a single object operation, but in fact we are always managing object graphs.
LPSTUDY:
1. 1: Class A creates an object by calling the alloc/init method. Here I name this object as aObject. At this time, because aObject calls the alloc method, its reference count (retain count) yes 1
2. A retain operation is performed on aObject in Class B, which increases the reference count of aObject, that is, 2
3. Class C wants to use aObject, so it calls the copy operation, so Class C will have aObject copying, which is different from the retain operation. The retain operation only adds the reference count of the actual memory pointed to by the aObject. The copy operation applies for another piece of memory, and the reference count of this memory is 1. Through the copy operation, class C can freely process this block of memory (this is restricted by processing and restricted by the copy element attribute) and name this block of memory as cObject.
4. Class A calls the release method. In this case, the reference count of aObject is reduced by 1 and changed to 1.
5. Class B calls the release method again. The reference count is 0, and the memory is released.
6. Class C calls the release method for its own cObject. The value of retain minus 1 is 0, and the memory is released.
Overview:
Object-c provides three memory management methods
1. Retain-Release: MRR, which requires programmers to clearly track their own objects to manage the memory. It is managed by using the reference counting model provided by the base class NSobject. When the reference count is 0, the memory is released.
2. Automatic Reference of words: ARC for short, which also uses the reference counting mechanism. It automatically adds a memory management method during compilation. If you use ARC, you do not need to know many underlying mechanisms.
See Transitioning to ARC Release Notes
3. Garbage collection: the system automatically tracks object dependencies and then automatically releases objects that are no longer referenced. It uses a mechanism different from automatic reference counting, but it is only used in Mac OS X and cannot be used in IOS.
See GarbageCollection Programming Guide.
If you want to program on IOS, you must use explicit memory management (the topic of this document). If you go further, you plan to write some libraries or plug-ins, or shared code that can be used in garbage collection and non-garbage collection programs, you must read this document to use the memory management technology. (In xcode, you must test your code when using or disabling garbage collection)
Good habits can avoid memory problems
There are two main problems that cause incorrect memory management
1. Release or override data during data usage.
This may cause memory crashes. More typical examples include program crashes and user data errors.
Data is no longer used, and memory leakage may occur if the memory is not released.
2. Memory leakage is because you still do not release memory that will no longer be used. Memory leakage causes your program to occupy too much memory, resulting in a decline in system performance or forced termination in IOS.
Do not just look at memory management from the reference count, so that you will fall into the Implementation Details and get away from your real goals. You should consider more memory management from the object owner and object graph.
Cocoa uses a simple naming convention to indicate the memory type of the object returned by the method you call.
See "MemoryManagement Policy ."
Although the basic policy is quite clear, there are also some practical steps that can help you manage the memory more easily, but your program is more robust and reliable, reduce memory usage at the same time
See "Practical Memory Management ."
The automatic release Pool provides a mechanism for sending release messages to objects in a delayed manner, this mechanism is useful when you discard the ownership of an object and do not want it to be immediately released (for example, you return an object from a method. In some cases, you need to use an object to automatically release the pool.
See "Using autorelsponpools ."
Use code analysis tools to debug memory problems
To find out your code problems during compilation, you can use xcode's use the ClangStatic Analyzer
If memory management problems occur, there are also some tools and technologies to detect and find problems.
· Many Technical tools are described in Technical Note TN2239 and iOSDebugging Magic, especially NSZombie, to help you discover over-released objects.
· You can also use tools to track reference counting events to find out memory leaks. See "CollectingData on Your App ".
Author: lipeng08