Swift Memory Management-Sample tutorial

Source: Internet
Author: User

in particular, ARC memory management in Swift is the management of reference types, that is, the objects created by the class are managed by arc. For value types, such as Integer, Float, Boolean, string, tuple, collection, enumeration, and struct, are automatically managed by the processor, and programmers do not need to manage their memory.
One, reference counting
Each Swift class creates an object that has an internal counter that tracks the number of references to the object, called the reference count (Reference count, or RC). When the object is created, the reference count is 1, each time the object is referenced, the reference count is added 1, and if it is not needed, the object reference is broken (assigned to nil), and its reference count is reduced by 1. When the object's reference count is 0, the object's memory is freed.
Is the principle of memory reference counting. The room in the picture is like the memory of the object, a person enters the room to turn on the light, is to create an object, when the object's reference count is 1. Someone enters the room, the reference count plus 1; someone leaves the room, the reference count minus 1. The last person leaves the room, the reference count is 0, the room light is off, and the object memory is released.

II. Example: Swift Automatic reference counting
Let's look at an example of how automatic reference counting works in Swift. is the life cycle of the object created by the employee class, which describes the objects being assigned to 3 variables, and their release process.

The sample code is as follows:
Class Employee {①var no:intvar name:stringvar job:stringvar salary:doubleinit (No:int, name:string, job:string , salary:double) {②    self.no = no    self.name = Nameself.job = Jobself.salary = salaryprintln ("Employee \ (name) has been constructed successfully. ") ③    }deinit {④println (" Employee \ (name) has been destructor succeeded. ") ⑤    }    }var ref1:employee?⑥var ref2:employee?⑦var ref3:employee?⑧ref1 = Employee (no:7698, Name:" Blake ", Job:" Salesman ", salary:1600) ⑨ref2 = REF1⑩REF3 = Ref1?ref1 = Nil?ref2 = NIL?REF3 = nil?


The preceding code, ①, declares the employee class, the ② line code is a constructor, initializes the storage property in the constructor, and constructs the success information in the Code section ③. The ④ line of code is the definition of the destructor, and the output of the destruction information in the Code section ⑤.

The code ⑥~⑧ line is a declaration of 3 employee type variables, at which time the employee object has not been created to allocate memory space. The ⑨ line of code is to actually create an employee object to allocate memory space and assign the object's reference to the REF1 variable, ref1 a "strong reference" relationship with the object, and the "strong reference" relationship guarantees that the object is not freed in memory, when its reference count is 1. The ⑩ line Code REF2 = Ref1 is to assign a reference to the object to Ref2,ref2 and to establish a "strong reference" relationship with the object, when its reference count is 2. The first line of code REF3 = Ref1 is to assign a reference to an object to REF3,REF3 also to establish a "strong reference" relationship with the object, when its reference count is 3.
The reference count of the employee object is then broken by the REF1 = Nil statement in the code line, when it is referenced by 2. And so on, Ref2 = nil when its reference count is 1,REF3 = nil when its reference count is 0, when the reference count is 0, the employee object is freed.
We can test to see the effect, if you set a breakpoint to step through debugging, you will find that the code runs out of line ⑨ after the console output:
Employee Blake has been constructed successfully.
The contents of the destructor output are not output until you run the line code:
Employee Blake has been deconstructed successfully.

This means that the destructor is called only if the reference count is 0, and the object is disposed.


For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485

Welcome to Luxgen iOS Classroom public Platform


Swift Memory Management-Sample tutorial

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.