Automatic reference counting in Swift programming to manage memory _swift

Source: Internet
Author: User
Tags closure

The Swift memory management feature is handled by using automatic reference count (ARC). ARC is used to initialize and cancel initialization of the system resources, thereby releasing the memory space of the class instance used when the instance is no longer needed. Arcs Track code instances to effectively manage information about the relationship between storage resources.

The function of Arc

    • Arc allocates a piece of memory to store information at each time a new class instance is created ()
    • Information about the instance type and its value is stored in memory
    • When a class instance no longer requires it to be freed automatically by Deinit (), the storage space for further class instances to be stored and retrieved
    • Arc holds properties, constants, and variables for the current reference class instance of the track, making Deinit () applicable only to those instances that are not used.
    • ARC maintains "strong references" class instance properties, constants, and variables to limit release when the current class instance is in use.

ARC procedure

Copy Code code as follows:

Class Studdetails {
var stname:string!
var mark:int!
Init (stname:string, Mark:int) {
Self.stname = Stname
Self.mark = Mark
}

Deinit {
println ("deinitialized \ (self.stname)")
println ("deinitialized \ (Self.mark)")
}
}

Let Stname = "Swift"
Let Mark = 98

println (Stname)
println (Mark)


When we use playground to run the above program, we get the following results.

Swift
98

ARC Strong reference Period class instance

Copy Code code as follows:

Class Studmarks {
Let name:string
var stud:student?

Init (name:string) {
println ("Initializing: \ (name)")
Self.name = Name
}

Deinit {
println ("deallocating: \ (self.name)")
}
}

Class Student {
Let name:string
var strname:studmarks?

Init (name:string) {
println ("Initializing: \ (name)")
Self.name = Name
}

Deinit {
println ("deallocating: \ (self.name)")
}
}

var shiba:studmarks?
var mari:student?

Shiba = Studmarks (name: "Swift")
Mari = Student (name: "ARC")

shiba!. Stud = Mari
mari!. strname = Shiba


When we use playground to run the above program, we get the following results.

Initializing:swift
Initializing:arc

Arc weak and unowned references
There are two ways to resolve a strong reference period for class type attributes:

    1. Weak reference
    2. No main reference

These references are used to make an instance refer to another instance of a reference cycle. The instance can then replace the strong reference cycle with each instance reference. When the user knows that some circumstances may return the ' nil ' value, we may point to the use of a weak reference. When an instance returns something that is not zero, it then uses the unowned reference declaration.

Weak reference Program

Copy Code code as follows:

class Module {
Let name:string
Init (name:string) {self.name = name}
var sub:submodule?
Deinit {println ("\ (name) is the Main Module")}
}

Class Submodule {
Let Number:int

Init (number:int) {self.number = number}

Weak var topic:module?

Deinit {println ("Sub-Module with their topic number is \")}
}

var toc:module?
var list:submodule?
TOC = Module (name: ARC)
List = Submodule (Number:4)
Toc!. Sub = List
list!. TOPIC = TOC

TOC = Nil
List = Nil


When we use playground to run the above program, we get the following results.

ARC is the Main Module
Sub Module with it topic number is 4

No main reference program

Copy Code code as follows:

Class Student {
Let name:string
var section:marks?

Init (name:string) {
Self.name = Name
}

Deinit {println ("\ (name)")}
}
Class Marks {
Let Marks:int
unowned Let Stname:student

Init (Marks:int, stname:student) {
Self.marks = Marks
Self.stname = Stname
}

Deinit {println ("Marks obtained by the student is \ (Marks)")}
}

var module:student?
module = Student (name: "ARC")
module!. Section = Marks (marks:98, stname:module!)
module = Nil


When we use playground to run the above program, we get the following results.

ARC
Marks obtained by the student is 98

Closed Baoqiang Reference period
When we assign a closure to a class instance property, the closure body is used to capture the strong reference cycle of a particular instance. Strong reference closure is defined by Self.someproperty or Self.somemethod (). A strong reference cycle is used as a closure reference type.

Copy Code code as follows:

Class HtmlElement {
Let samplename:string
Let text:string?

Lazy var ashtml: ()-> String = {
If let Text = self.text {
Return "<\ (self.samplename) >\ (text) </\ (self.samplename) >"
} else {
Return "<\ (self.samplename)/>"
}
}

Init (samplename:string, text:string = nil) {
Self.samplename = Samplename
Self.text = text
}

Deinit {
println ("\ (Samplename) is being deinitialized")
}
}

var paragraph:htmlelement? = HtmlElement (samplename: "P", Text: "Welcome to Closure SRC")
println (paragraph!. Ashtml ())


When we use playground to run the above program, we get the following results.

<p>welcome to Closure src</p>
Weak and unowned references
when closures and instances are referenced to each other, the user can be defined in a closed as a unowned reference capture. It does not allow users to deallocate instances at the same time. When an instance returns a "nil" definition at some point and uses the value of a weak instance.

Copy Code code as follows:

Class HtmlElement {
Let module:string
Let text:string?

Lazy var ashtml: ()-> String = {
[unowned Self] in
If let Text = self.text {
Return "<\ (self.module) >\ (text) </\ (self.module) >"
} else {
Return "<\ (self.module)/>"
}
}

Init (module:string, text:string = nil) {
Self.module = Module
Self.text = text
}

Deinit {
println ("\ (module) the Deinit ()")
}
}

var paragraph:htmlelement? = HtmlElement (module: "Inside", Text: "ARC Weak References")
println (paragraph!. Ashtml ())
Paragraph = nil


When we use playground to run the above program, we get the following results.

<inside>arc Weak references</inside>
Inside the Deinit ()

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.