Memory management is an important topic in the development of software. If memory management is improper, light memory leaks, heavy program crashes.
Here's an important story about the arc (Automatic Reference counting) mechanism of iOS.
The historical origins of Arc
At the time of iOS1.0, Apple did not open the phone development interface, there is no concept of iOS developers.
In iOS2.0, we manage the number of technologies that memory uses in the MRC mechanism.
At the time of iOS5.0, Apple introduced the arc mechanism.
We know that when the MRC manages memory, we need to follow the golden rule of "who creates, who releases, who references, who manages", when we create objects, we need to consider when to release memory, and sometimes the memory management mechanism is unfamiliar, it is likely to lead to memory leaks and over-release problems.
Manual memory management can cause a lot of problems, in iOS5.0, Apple introduced automatic memory management arc, when the IDE is XCode4.0, when we create the project, there is an option, whether to use arc, to give developers a choice, but the introduction of arc, Go to very few people to use arc, when XCode5.0, there is no such option, we create the project by default is the arc mechanism, we want to use MRC, we need to manually configure the project.
In the launch of Arc, many people say that iOS arc and the Android GC mechanism is very similar, but they are still very much different. Where GCs is a run-time feature, ARC is a compile-time feature.
Use of Arc
Arc our literal translation is automatic reference counting, extended meaning we can understand as automatic memory management. Automatic memory management Don't we really need to manage it? Not also. The arc mechanism also causes memory leaks, which we need to be aware of when we use them.
In the MRC, we often use release,autorelease,retain these keywords to keep reference counts or to release objects. In arc, we can't handle this. If we use these keywords, our program cannot pass the basic compilation.
Since we created the object without our manual release, the system will be released for us when necessary, so when will the object be released?
Before we explore this issue, let's start by explaining the basic principles of using arc.
- The object pointed to by a strong reference is not freed.
- An object without a strong reference is released immediately.
- The object that the weak reference points to will be freed automatically when it is released.
Note: The objects we create are strongly referenced by default, such as: people = [people new]; equivalent __strong people = [people new];
What we're talking about here is all about these three guidelines.
First, local objects
First create a project, then add a class people, and the following is the contents of the people. m file.
1 @implementationpeople2-(ID) Init3 {4 if(self =[Super init])5 {6NSLog (@"%s", __function__);7 }8 returnSelf ;9 }Ten-(void) Dealloc One { ANSLog (@"%s", __function__); - } - @endPEOPLE.M
We add people *p = [people new] in viewdidload; NSLog (@ "%s", __function__);
Printing results are:
2015-10-19 13:16:10.589 textarc[2627:93432]-[people init]
2015-10-19 13:16:10.600 textarc[2627:93432]-[viewcontroller Viewdidload]
2015-10-19 13:16:10.600 textarc[2627:93432]-[people Dealloc]
We find that the People object is not released, and we can see from the print order that the People object was released after the Viewdidload execution was completed.
Why would a local object be freed after the function has finished executing?
In Viewdidload, p is a strong reference, the object is not freed, and the contents of the NSLog are printed. However, after the function has finished executing, the strong reference pointer does not point to the object. According to the above guidelines, no strong pointer to the object will be released immediately, so after the execution of Viewdidload, the object people will be released.
If we write this in viewdidload:
__weak people *p = [people new];
NSLog (@ "%s", __function__);
We can guess the print results will be different from the above.
2015-10-19 13:22:10.783 textarc[2682:96983]-[people init]
2015-10-19 13:22:10.783 textarc[2682:96983]-[people Dealloc]
2015-10-19 13:22:10.784 textarc[2682:96983]-[viewcontroller Viewdidload]
Because the object does not have a strong pointer reference, people is freed, and then the following print is performed.
Second, global variables
Now we define the people as a global object, named _people.
We write the following code in Viewdidload:
_people = [people new];
NSLog (@ "%s", __function__);
Printing results are:
2015-10-19 13:28:32.076 textarc[2764:100790]-[people init]
2015-10-19 13:28:32.077 textarc[2764:100790]-[viewcontroller Viewdidload]
We find that the Dealloc method of people is not called, because there is a strong reference pointer to people when the Viewdid function is executed, and the object is not freed according to the criteria above.
If we precede the global object with a __weak: __weak people *_people;
Then execute the above code and print the result:
2015-10-19 13:31:47.816 textarc[2803:102843]-[people init]
2015-10-19 13:31:47.827 textarc[2803:102843]-[people Dealloc]
2015-10-19 13:31:47.828 textarc[2803:102843]-[viewcontroller Viewdidload]
This result doesn't have to be explained, although it is a global object, but it is a weak reference, without a strong reference, the object will be released.
Third, global and local mixed use
Now we define a global row strong pointer to the People object P, while creating a local strong pointer object P1.
In the Viewdidload:
People *p1 = [people new];
p = p1;
The result of the execution is:
2015-10-19 13:49:32.380 textarc[3088:111876]-[people init]
2015-10-19 13:49:32.383 textarc[3088:111876]-[viewcontroller Viewdidload]
Because P1 is a local strong reference, it is normally released after the function has been executed, but before it is released, there is a global strong reference that executes it, and all people are not released.
If we write this in Viewdid:
__weak people *p1 = [people new];
p = p1;
NSLog (@ "%s", __function__);
Printing results are:
2015-10-19 13:50:43.647 textarc[3115:112734]-[people init]
2015-10-19 13:50:43.651 textarc[3115:112734]-[people Dealloc]
2015-10-19 13:50:43.651 textarc[3115:112734]-[viewcontroller Viewdidload]
Because the local object is a weak reference, the object is freed, and before it is assigned to a global strong reference, it is empty and the object is freed.
Iv. creating a Nested object
Suppose we also have a class, the class named Car,people has an attribute, @property (nonatomic,strong) Car *car;
Now we're going to declare people a global object p.
In the Viewdidload:
p = [People new];
Car *c = [car new];
P.car = C;
NSLog (@ "%s", __function__);
Printing results:
2015-10-19 13:57:21.883 textarc[3220:116572]-[people init]
2015-10-19 13:57:21.886 textarc[3220:116572]-[car init]
2015-10-19 13:57:21.886 textarc[3220:116572]-[viewcontroller Viewdidload]
Although the car is a local reference, it is not released after the function has been executed because P has a strong reference to it, and all car will not be released.
If we release the people, car will naturally release.
If we want to release a variable, we can simply assign the variable to null and it will be released.
For example:
p = [People new];
Car *c = [car new];
P.car = C;
p = nil;
NSLog (@ "%s", __function__);
Printing results are:
2015-10-19 14:01:14.703 textarc[3265:118993]-[people init]
2015-10-19 14:01:14.706 textarc[3265:118993]-[car init]
2015-10-19 14:01:14.706 textarc[3265:118993]-[people Dealloc]
2015-10-19 14:01:14.706 textarc[3265:118993]-[viewcontroller Viewdidload]
2015-10-19 14:01:14.707 textarc[3265:118993]-[car Dealloc]
V. Circular reference Issues
Add the property of car in people, there is people this attribute in car. (Note cross-reference issues)
We are in the Viewdidload:
People * p = [people new];
Car *c = [car new];
P.car = C;
C.people = p;
NSLog (@ "%s", __function__);
The result is:
2015-10-19 14:05:50.296 textarc[3320:121202]-[people init]
2015-10-19 14:05:50.298 textarc[3320:121202]-[car init]
2015-10-19 14:05:50.298 textarc[3320:121202]-[viewcontroller Viewdidload]
We find that none is released, which can lead to memory leaks. Of course we can assign C, or p to nil, to release it. But it's bad to write, we need to judge what release decides to release.
How to solve this problem?
When the property is decorated, do not use strong at the same time, you can use a weak adornment, the result is as follows:
2015-10-19 15:45:53.289 textarc[3937:150043]-[people init]
2015-10-19 15:45:53.298 textarc[3937:150043]-[car init]
2015-10-19 15:45:53.298 textarc[3937:150043]-[viewcontroller Viewdidload]
2015-10-19 15:45:53.298 textarc[3937:150043]-[people Dealloc]
2015-10-19 15:45:53.298 textarc[3937:150043]-[car Dealloc]
Arc Summarywhen using arc, we can simplify our programming operation to some extent, but there will also be a memory leak in the process of use, we need to summarize the problem in the actual use process, so that our program has fewer bugs and potential bugs.
IOS-ARC mechanism