• Skip to main content
  • Skip to primary sidebar

Ravi Shankar

Tweaking Apps

  • Swift
  • Tech Tips

ARC

Memory management in Swift

June 7, 2015 By Ravi Shankar Leave a Comment

Memory management in Swift is done by Automatic Reference Counting or ARC. Whenever a variables holds an instance of a object the memory count for that object increases by 1. And when variable become out of scope or set to nil, the memory count decreases 1.

[code language=”swift”]class Teacher {
var name: String?
var course: String?

init (name: String, course: String) {
self.name = name
self.course = course
println("Reference count increased by 1")
}

deinit{
println("Reference count decreased by 1")
}
}

let teacher1 = Teacher(name: "Ravi", course: "Swift")

func createTeacher() {
let teacher2 = Teacher(name: "John", course: "Java")
}

createTeacher()[/code]

 

In the above example, we are creating two instances of Teacher class and storing it in variables teacher1 and teacher2. Since teacher2 variable is created within the function, it becomes out of scope after the function call. You should be able to observe the two init messages and one deinit (teacher2) message in console log. This should give you some idea on how reference counting works in Swift.

Increasing and decreasing of reference count are automatically handled by ARC but problem occurs when we have a strong reference cycle. A strong reference cycle refers to cyclic relationship between the objects.

[code language=”swift”]class Teacher {
var name:String?
var course:String?
var student: Student?

init(name: String, course:String) {
self.name = name
self.course = course

println("Reference count of Teacher increases by 1")
}

deinit {
println("Reference count of Teacher decreases by 1")
}
}

class Student {
var name:String?
var mentor: Teacher?

init(name: String, course:String) {
self.name = name

println("Reference count of Student increases by 1")
}

deinit {
println("Reference count of Student decreases by 1")
}
}

func createInstance() {
let teacher = Teacher(name: "Jason", course: "Swift")
let student = Student(name: "Adam", course: "Swift")
teacher.student = student
student.mentor = teacher
}

createInstance()[/code]

In the above code snippet, Teacher and Sudent classes have a strong reference cycle and both student and teacher instances remain in memory even after the end of function call. A strong reference cycle can be avoided by declaring any one of the instance as weak or unowned

[code language=”swift”]weak var student: Student?
[/code]

You can also unown the reference when you know the reference cannot be nil

[code language=”swift”]unowned var mentor: Teacher[/code]

Download playground file from gitHub (Memory Management)

Filed Under: ios, iPad, iPhone, Xcode Tagged With: ARC, iPad, Memory Management, Xcode

Primary Sidebar

TwitterLinkedin

Recent Posts

  • How to block keywords in Jio broadband
  • How to disable opening an app automatically at login in Mac
  • How to set preferred Wifi network on Mac
  • Attribute Unavailable: Estimated section warning before iOS 11.0
  • How to recover Firefox password from Time Machine backup

Pages

  • About
  • Privacy Policy
  • Terms and Conditions

Copyright 2022 © rshankar.com

Terms and Conditions - Privacy Policy