Lifetime:Automatic Reference Counting

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:

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。

Us gun2 references assigned gun1,So gun1 original reference was overwritten,The corresponding object will only waiting to be recycled it!

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:

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。

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 Gerber drop-off:R0uter's Blog » Lifetime:Automatic Reference Counting

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1135.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *