How does Objective-C manage the memory ?, Objective-c
Objective-C memory management mainly includes three methods: ARC (Automatic Memory count), Manual memory count, and memory pool.
1. (Garbage Collection) Automatic Memory count: This method is similar to java in the execution process of your program. There is always a high person behind the scenes to help you clean up the garbage accurately, you don't have to consider when it starts to work, how to work. You only need to understand that I have applied for a piece of memory space. When I no longer use the memory and it becomes garbage, I will completely forget it, the tall guy will help me clean up the garbage. Unfortunately, that person needs to consume a certain amount of resources. In the carrying devices, the resources are very popular, so the iPhone does not support this function. Therefore, "Garbage Collection" is not the scope of this Getting Started Guide. For example, if you are interested in the internal mechanism of "Garbage Collection", you can refer to other materials, but to be honest, "Garbage Collection" is not suitable for beginners.
Solution: If you create an instance in alloc-initial mode, the reference count is + 1 after creation, and the reference count is + 1 every retain. Then, you can set the corresponding number of release in the program.
2. (Reference Counted) manual memory count: that is to say, after a memory segment is applied, there is a variable used to save the number of times the memory is used. We call it a counter for the time being, when the counter changes to 0, the memory is released. For example, after A memory segment in program A is successfully applied for, the counter changes from 0 to 1 (we call this process alloc ), then program B needs to use this memory, and the counter is changed from 1 to 2 (we call this process retain ). Then, when program A no longer needs the memory, program A will reduce the counter by 1 (we call this process release); and program B no longer needs the memory, then, reduce the counter by 1 (this process is still release ). When the system (that is, the Foundation) finds that the counter has changed to 0, it will call the memory recycle program to recycle the memory (we call this process dealloc ). By the way, if there is no Foundation, you need to manually maintain the counter, release the memory, and so on.
Solution: It is generally created by a static method of the class. The function name does not contain alloc or init words, such as [NSString string] and [NSArray arrayWithObject:]. reference count + 0 after creation, released after function exit stack, that is, equivalent to a local variable on the stack. of course, you can also extend the lifetime of an object through retain.
3. Memory Pool: You can create and release a memory pool to control the timing of memory application and recovery.
Solution: autorelease is added to the system memory pool, and the memory pool can be nested. Each memory pool requires a creation and release pair, just as it is written in the main function. it's easy to use, for example, [[NSString alloc] initialWithFormat: @ "Hey you! "] Autorelease]: adds an NSString object to the inmost system memory pool. When we release this memory pool, all objects will be released.