A brief talk on memory management in Swift

Source: Internet
Author: User
Tags bcbc

Swift uses an automatic reference count (ARC (Automatic Reference count)) to manage the memory usage of the application. This means that memory management is already part of Swift, and in most cases you do not need to consider memory management. When instances are no longer needed, ARC automatically frees the memory used by these instances.

Memory Management : For the management of the memory footprint of the instance (placed inside the heap)

Example: 1: Instance constructed by class type, 2: Closure object

Now let's write an example to prove

class When person {    var name:string    init (name:string)        {= name    }    // class is destroyed, This method is called     deinit {        print ("\ (name) person Deinit")    }} // " Zhang San ")  
p = Nil
// "Zhang San" person Deinit

When an instantiated object is given nil, the Deinit () is called, which means that the object is destroyed.

 class   person {var name:string init (name:s Tring) {self.name  = name}  //  Deinit {print (  " \ (name) person deinit  "  )}}}  // var p:person? = Person (name:   Zhang San   " " p  = person (name:  " banzhang  ) //  "Zhang San Persondeinit"  

when an object references another new object, the old object is destroyed by the pin.

func Testarc () {        "Chen")        print (p.name)}testarc ()  //  Chen Person deist

If it is a function, the object will be destroyed by the action of the scope.

So, with a few examples above, it can be concluded that if an object is not referenced, it will be destroyed automatically .

Next, let's take a look at the circular reference,

In short, you can call a circular reference as a property or method of two functions to each other, so that there is likely to be a memory leak, let's take a look at the circular reference solution, in the description of the solution, we first say the meaning of two nouns:

null-dangling pointer : (appears only in weak references) when you specify each location, the data stored at that location is no longer present.

There are only two ways to solve an empty hover pointer: one is to assign its value to nil, and the other to point to another new object (this method is not possible).

In Swift, the assignment to nil is only used. Or! A declared class member can resolve an empty hover pointer problem

A (A1:B) B (B1:A)

//? ?

//? !

//! ? (This situation is the same as above)

//! !

1. Both sides? And?

classStudent {var name:string var t:teacher?Init (name:string) {self.name=name} deinit {print ("\ (name) student Deinit")    }}classTeacher {var name:string weak var banzhang:student?Init (name:string) {self.name=name} deinit {print ("\ (name) teacher Deinit")}}var Teacher:teacher? = Teacher (Name:"CJ") var bc:student? = Student (Name:"Yeweidong") Teacher?. Banzhang =BCBC?. t =Teacherteacher=NILBC= Nil//Yeweidong Student Deinit//CJ Teacher Deinit

2. One side is a nullable type, one side is a non-nullable type (unowned)

classStudent2 {var name:string var t:teacher2?Init (name:string) {self.name=name} deinit {print ("\ (name) student Deinit")    }}classTeacher2 {var name:string//unowned decoration on non-optional type, cannot use Weak,weak can only be used in optional typeunowned var banzhang:student2 init (name:string,stu:student2) {self.name=name Self.banzhang=Stu} deinit {print ("\ (name) teacher Deinit")}}var S1:student2? = Student2 (Name:"CH") var teac:teacher2? = Teacher2 (Name:"David", stu:s1!) S1?. t =TEACTEAC?. Banzhang = s1!S1=NILTEAC=Nil//CH Student Deist//David Teacher Deinit

3. Both sides are non-available types

classStudent2 {var name:string unowned var t:teacher2 init (name:string, t:teacher2) {self.name =name self.t=T} deinit {print ("\ (name) student Deinit")    }}classTeacher2 {var name:string//unowned decoration on non-optional type, cannot use Weak,weak can only be used in optional typeunowned var banzhang:student2 init (name:string,stu:student2) {self.name=name Self.banzhang=Stu} deinit {print ("\ (name) teacher Deinit")}}var S1:student2? = Student2 (name: "Ax", t:teac!)) var teac:teacher2? = Teacher2 (Name:"David", stu:s1!) S1?. t =TEACTEAC?. Banzhang = s1!S1=NILTEAC= Nil

If both sides are non-available types, the instance cannot be created and an error is made

memory leaks : When an object is not useful, it also occupies memory.

For memory leaks, let's write an example of a cyclic strong reference to see

classStudent {var name:string var t:teacher?Init (name:string) {self.name=name} deinit {print ("\ (name) student Deinit")    }}classTeacher {var name:string var banzhang:student?Init (name:string) {self.name=name} deinit {print ("\ (name) teacher Deinit")}}var Teacher:teacher? = Teacher (Name:"CJ") var bc:student? = Student (Name:"Yeweidong") Teacher?. Banzhang =BCBC?. t =Teacherteacher=NILBC= Nil

If this is called, the Deinit () of two classes will not be called. When these two classes are not functioning, they still occupy memory, which creates a memory leak

To solve this situation,

1. Using Weak references

Just add the Banzhang variable of the example teacher class to the keyword weak, or the T variable of the student class plus the keyword weak.

classStudent {var name:string var t:teacher?Init (name:string) {self.name=name} deinit {print ("\ (name) student Deinit")    }}classTeacher {var name:string weak var banzhang:student?Init (name:string) {self.name=name} deinit {print ("\ (name) teacher Deinit")}}var Teacher:teacher? = Teacher (Name:"CJ") var bc:student? = Student (Name:"Yeweidong") Teacher?. Banzhang =BCBC?. t =Teacherteacher=NILBC= Nil//yeweidong Student Deinit

CJ Teacher Deinit

If this is called, the Deinit () of two classes is called.

Well, memory management and circular references are here.

A brief talk on memory management in Swift

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.