Memory Management Basics *********************
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
Memory management technology: Arc:automatic Reference Count
Automatic: By the language itself to help us destroy the memory, do not need you to manually destroy,
For example, in C, call Dealloc ()
Reference: let p = person () p is a reference to the person () object
Count: let p = person ()//One
//Let PP = p//two X
Use a destructor to determine whether to destroy
classPerson {varname:string Init (name:string) {self.name=name} deinit {print ("\ (name) person Deinit") }}varP:person? = Person (name:"David")//p = NilvarPP =pp=NILPP= Person (name:"Banzhang") pp= Nil
Reference increment: A constant assignment of an object to become a variable or attribute, etc.
Reference reduction: Manually assign a variable equal value to nil alive to make it reference a new object, the Live function is executed (e.g. below)
Class person { var name:string Init (name:string) { self.name = name } deinit { Print ("\ (name) person Deinit") }
}
Func Testarc () {
var p = person (name: "Chenhe") }testarc () //system automatically calls Deinit
Circular reference *************
The elaboration of circular references
classStudent {varname:stringvarT:teacher?Init (name:string) {self.name=name} deinit {print ("\ (name) student Deinit") }}classTeacher {varname:stringvarBanzhang:student?Init (name:string) {self.name=name} deinit {print ("\ (name) teacher Deinit") }}varTeacher:teacher? = Teacher (Name:"Cheng")varBc:student? = Student (Name:"Ling") Teacher?. Banzhang =BCBC?. t =Teacherteacher=Nil BC?. t =NILBC= nil//Because it is a two strong reference, so also Bc?,t=nil
Workarounds for circular references: weak (weak references) and unowned (non-owned)
Circular reference: In order to solve the problem of memory leak and empty hanging pointer
There are only two ways to solve a null-dangling 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! Declared class member only line
Strictly speaking, there are only 3 cases to consider
The first case: both sides?
classStudent {varname:stringvarT:teacher?Init (name:string) {self.name=name} deinit {print ("\ (name) student Deinit") }}classTeacher {varname:string WeakvarBanzhang:student?Init (name:string) {self.name=name} deinit {print ("\ (name) teacher Deinit") }}varTeacher:teacher? = Teacher (Name:"CJ")varBc:student? = Student (Name:"Yeweidong") Teacher?. Banzhang =BCBC?. t =TEACHERBC=Nilteacher= nil//Because teacher is weak
Second case: One side is a nullable type, one side is a non-nullable type (unowned)
classStudent2 {varname:stringvarT:teacher2?Init (name:string) {self.name=name} deinit {print ("\ (name) student Deinit") }}classTeacher2 {varname:string//unowned decoration on non-optional type, cannot use Weak,weak can only be used in optional typeunownedvarbanzhang:student2 Init (name:string,stu:student2) {self.name=name Self.banzhang=Stu} deinit {print ("\ (name) teacher Deinit") }}varS1:student2? = Student2 (Name:"CH")varTeac:teacher2? = Teacher2 (Name:"David", stu:s1!) S1?. t =TEACTEAC?. Banzhang = s1!S1= Nil
The third case
//third case: Both sides are non-nullable typesclassStudent3 {varname:stringvart:teacher3!//NilInit (name:string,teachername:string) {self.name= Name//This line of code runs after name has a value, then T has nil, so self can be usedSELF.T =Teacher3 (Name:teachername, stu:self)} deinit {print ("\ (name) student Deinit") }}classTeacher3 {varname:string//var banzhang:student3 = Student3 (name: "Xuesheng", teacher:self)//can't use self unless lazyunownedvarbanzhang:student3 Init (name:string,stu:student3) {self.name=name Self.banzhang=Stu} deinit {print ("\ (name) teacher Deinit") }}
If two classes, their properties are non-nullable types, and reference to each other
That will cause these two objects to never be created.
var s3:student3? = Student3 (name: "XXX", TeacherName: "ZhuZhu")
var t3 = Teacher3 (name: "ZhuZhu", stu:s3!)
S3?. t = Nil
S3 = Nil
The embodiment of circular references in closures
classClosureclass {varName ="asdf" //Use square brackets syntax (capture list) to handle circular references//multiple separated by commasLazyvarMyclosure:void->int = { //[unowned Self]//Direct modification of unowned or weak[unowned This= Self]//In addition to adornments, you can also take aliases//[unowned this = self,unowned self]() IntinchPrint ( This. Name) print (self.name)return 5} deinit {print ("Closer Deinit") }}//There are two objects, one is the Closureclass object, the other is the closure objectvarCc:closureclass? =Closureclass () cc?. Myclosure () cc=NILCC?. Myclosure ()//output nil, indicating that it has been destroyed
Memory management: Memory leaks and empty hover pointers