Objective-C basics 3: Memory Management continued,
1. In the previous article, we talked about the basis of Memory Management in OC. We will summarize the time to use memory management.
1) when creating an object with new, alloc, or copy, you must release the object.
2) when an object is owned, if it is a temporary object, it does not need to be released; if it needs to be retained for a long time, it must be retained and released in the dealloc method.
2. Garbage collection mechanism in OC
OC2.0 introduces the garbage collection mechanism, that is, after the garbage collection mechanism is enabled, we forget to release the object. OC will help us release the object, this shows that OC is more powerful than C ++.
The main principle of the garbage collection mechanism is to regularly and dynamically track objects during running, check the reference Technology of objects and clean them up. Garbage collection only supports mac and ios, the reason is that the app is dynamically released during running and not applicable to mobile apps. Therefore, apple introduces ARC to solve the problem. Note that no garbage collection mechanism is set in the XCode6 settings I am currently using.
3. ARC mechanism in IOS
The automatic reference counting mechanism is similar to the garbage collection mechanism, which helps you manage the object lifecycle. The difference is that the garbage collection mechanism decides to release objects at runtime. ARC secretly adds retain and release methods to our code during compilation. You can select a project and use the menu Edit-> Refactor-> Convert to Objective-c arc to Convert the project to ARC. ARC only works for the drop-down object. For more information, see the OC basic tutorial.