In front of so many bedding,Now finally we want to address this thing the garbage collector。
ARC
This is not the Ark ...... This is automatic reference counting (Automatic Reference Counting),This thing is used to manage Apple memory。
Its function is that the garbage collector on the garbage heap。It would not have been able to ensure that all heap objects that reside in the running to be released in the heap。It ensures that memory will be reallocated to other objects to be used。
What is released
Speaking of the object will not be taken away to the garbage collector,It depends on the reference will not be released:Us to distinguish whether an object is still in use - point to an object when there is no time reference,We say that it has been released - because it will never be accessed to。
The method of release
We generally have three ways to relieve a reference to an object:
- The reference method in place,The method of the stack,Reference finished - not a reference to an object,It will be recovered。
- Change the reference point to other objects - the original object has no references,It will be recovered。
- Empty direct quote - provided that you declare the type of storage is optional。
All right,Now we were to look at a few chestnuts:
1 2 3 4 5 |
func run() { _ = SmallGun(ammo1: 8, isSafe2: true) } run() |
The method is finished stack,Temporary variables did not object - a naturally freed。arc found object has no references,Then the object is recovered,Leaving room for others to use。
1 2 3 |
var gun1 = SmallGun(ammo1: 8, isSafe2: true) var gun2 = SmallGun(ammo1: 9, isSafe2: false) gun1 = gun2 |
Us gun2 references assigned gun1,So gun1 original reference was overwritten,The corresponding object will only waiting to be recycled it!
1 2 3 |
var gun1:Gun? gun1 = SmallGun(ammo1: 8, isSafe2: true) gun1 = nil |
We re-assigned to gun1 empty,So no more references,The object along with it finished。
Cross-references
There is such a situation,It will result in an automatic reference counting catch chicken,We take a look:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Aaa { var b:Gun? } var gun1:Gun? var aaa:Aaa? gun1 = SmallGun(ammo1: 8, isSafe2: true) aaa = Aaa() gun1?.ccc = aaa aaa?.b = gun1 aaa = nil gun1 = nil |
We just slightly modified class,It adds a reference to the class attribute Aaa,Then created the Aaa category,This class contains a reference to the class attribute Gun,This time we put it another two references,Then freed ...... etc.,Really you can relieve it?
However, not。
We recall the definition of an object to be released:When the object is no reference to it referred to be released - but are still refer to each other between the two objects do!
so,Although we can not access the two objects,But they also cited the two mutually,arc not do anything,Like you no longer have access to them - this is called the famous "memory leak"。
Weak references
At this moment,We want to break this loop,Let references can disconnect,So as to make this a reference to another object is released by one out。We need to be declared as one of the weak reference,The only way to let the arc loop disconnect between them。
1 2 3 |
class Aaa { weak var b:Gun? } |
That's all,Loop will be interrupted,Memory leaks have been looking back。
Weak reference requirements
- Weak references must be variable,It can not be a constant,Because it can be modified too。
- Weak references must be option,It must be cleared。
Original article written by LogStudio:R0uter's Blog » Lifetime:Automatic Reference Counting
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1135.html
Comments