Answer:
- Memory Management is the process of controlling the lifetime of an object based on its necessity
var car: Car? = Car()
car.move()
car.blowHorn()
car.stop()
car = nil //release
Answer:
- Prior to ARC, developer was responsible for retaining and releasing the objects’ memory using release, retain and autorelease.
Answer:
- Automatic Reference Counting is a concept of managing the memory automatically by the runtime system.
- But still there are three different places where developer has to take of releasing the memory.
- Closures
- Delegates
- Outlets
Answer:
- Memory Leak is a situation where the runtime system fails to release the memory of unused objects.
Answer:
- Strong shares the ownership of the object and increases the reference count by 1.
class Car {
func move() {
print("Car is moving")
}
}
var car: Car? = Car()
car?.move()
// Strong Reference Occured
var anotherCar: Car? = car
car?.move() // Car is moving
anotherCar?.move() // Car is moving
car = nil
car?.move() // Not executing
anotherCar?.move() // Car is moving, not released
anotherCar!.move() // Car is moving, not released
Answer:
- weak shared the ownership of the object without increasing the reference count.
class Car {
func move() {
print("Car is moving")
}
}
var car: Car? = Car(
// Strong Reference Occured
weak var anotherCar: Car? = car
car?.move() // Car is moving
anotherCar?.move() // Car is moving
car = nil
car?.move() // Not executing
anotherCar?.move() // Not executing. released
anotherCar!.move() // Crash happens
Answer:
- strong and weak are memory management related keywords.
- When you assign a variable to another:
- strong shares the ownership of the object and increases the reference count by 1.
- weak shares the ownership and doesn’t increase the reference of that object.
Answer:
- When two objects are pointing each other strongly and ARC fails to release memory. It leads to memory leaks. This situation is called Retain / Reference Cycle.
Answer:
- Closures
- Delegates
- Outlets
class Human {
var heart: Heart? = nil
deinit {
print("Human is released")
}
}
class Heart {
var owner: Human? = nil
deinit {
print("Heart is released")
}
}
var person: Human? = Human()
var heart: Heart? = Heart()
person?.heart = heart
heart?.owner = person
person = nil // "Human is released" is not printed
heart = nil // "Heart is released" is not printed
class Human {
weak var heart: Heart? = nil
deinit {
print("Human is released")
}
}
class Heart {
weak var owner: Human? = nil
deinit {
print("Heart is released")
}
}
var person: Human? = Human()
var heart: Heart? = Heart()
person?.heart = heart
heart?.owner = person
person = nil // Human is released
heart = nil // Heart is released
Answer:
- weak and unowned are keywords used to avoid retain cycles.
- weak variable might become nil. So weak variables are declared as var and Optional.
- When weak is used in closures, weak references act like optionals. So when you use them in closures, you have to use optional chain with them.
- unowned variables never become nil. So unowned variables can be declared as let and stored.
- You can use unowned when the other instance has the same life time or the longer lifetime.
- Use unowned, when you are sure it is not nil.
Answer:
- Compile Time
Answer:
- GC takes more memory to run its algorithm.
- ArC takes less memory to function.
- GC follows Mark and Sweep algorithm.
- ARC follows the reference count of the object.
- GC impacts the performance.
- ARC doesn’t impact performance.
- GC resolves retain cycles automatically.
- ARC doesn’t resolve retain cycles automatically.
Answer:
- When two objects are pointing each other strongly and ARC fails to release memory.
Answer:
- By using instrument tool we can identify Retain Cycles.
Section 3, Conditional Statement
Section 10, static type vs dynamic type
Section 15, higher order function
Section 18, Memory Management