内存管理

  • OC一样,Swift也是采取基于引用计数的ARC内存管理方案(针对堆空间)
  • SwiftARC中又三种引用
  • 强引用strong refrence:默认情况下,引用都是强引用
  • 弱引用weak refrence:通过weak定义弱引用
  1. 必须是可选类型的var因为实例销毁后,ARC会自动将弱引用设置为nil
    2.ARC自动给弱引用设置nil时,不会触发属性观察器
  • 无主引用unowned refrence:通过unowned定义无主引用
    1.不会产生强引用,实例销毁后仍然存储着实例的内存地址(类似OC中的unsafe_unretained)
    2.视图在实例销毁后访问无主引用,会产生运行时错误(野指针)

weak unowned的使用限制

  • weak unowned只能用在类实例上面
public Livable: AnyObject {}

class Person { }
weak var p0: Person?
weak var p1: AnyObject?
weak var p2: Livable?

unowned var p10: Person?
unowned var p11: AnyObject?
unowned var p12: Livable?

Autoreleasepool

public func autoreleasepool(involing body: () throws -> Result) rethrows -> Result

autoreleasepool {
    let p = Person(age: 20,name: "Jack")
    p.run()
}

循环引用 Reference Cycle

  • weak unowned都能解决循环引用的问题,unowned要比weak少一些性能消耗.
  • 在生命周期中可能会变为nil的使用weak
  • 初始化赋值后再也不会变为nil的使用unowned

闭包的循环引用

  • 闭包表达式默认会对用到的外层对象产生额外的强引用(对外层对象进行了retain操作)
  • 下面代码会产生循环引用,导致Person对象无法释放(看不到Persondeinit被调用)
class Person {
    var fn: (() -> ())?
    func run() {
        print("run")
    }
    deinit {
        print("deinit")
    }
    
}

func test() {
    let p = Person()
    p.fn = {
        p.run()
    }
}
  • 在闭包表达式的捕获列表声明weakunowned引用,解决循环引用问题
class Person {
    var fn: (() -> ())?
    func run() {
        print("run")
    }
    deinit {
        print("deinit")
    }
    
}

func test() {
    let p = Person()
    p.fn = { [weak p] in
        p?.run()
    }
}

func test() {
    let p = Person()
    p.fn = { [unowned p] in
        p.run()
    }
}

p.fn = {
    [weak wp = p,unowned up = p , a = 10 + 20] in
    wp?.run()
}
  • 如果想在定义闭包属性的同时引用self,这个闭包必须是lazy的(因为实例初始化完毕之后才能引用self)
  • 下面闭包fn内部如果用到了实例成员(属性,方法),编译器会强制要求明确写出self
class Person {
    lazy var fn: (() -> ()) = {
        [weak self] in
        self?.run()
    }
    func run() {
        print("run")
    }
    deinit {
        print("deinit")
    }
}
  • 如果lazy属性是闭包调用的结果,那么不用考虑循环引用的问题(因为闭包调用后,闭包的生命周期就结束了)
class Person {
    var age: Int = 0
    lazy var getAge: Int = {
        self.age
    }()
    deinit {
        print("deinit")
    }
}

@escaping

  • 非逃逸闭包,逃逸闭包,一般都是当做参数传递给函数
  • 非逃逸闭包:闭包调用发生在函数结束前,闭包调用在函数作用域内
  • 逃逸闭包: 闭包有可能在函数结束后调用,闭包调用逃离了函数的作用域,需要通过@escaping声明.
import Dispatch
typealias Fn = () -> ()
//Fn是非逃逸闭包
func test1(_ fn: Fn) {
    fn()
}
//fn是逃逸闭包
var gFn : Fn?
func test2(_ fn: @escaping Fn){
    gFn = fn
}
//fn是逃逸闭包
func test3(_ fn: @escaping Fn) {
    DispatchQueue.global().async {
        fn()
    }
}

class Person {
    var fn : Fn
    //fn是逃逸闭包
    init(fn: @escaping Fn) {
        self.fn = fn
    }
    func run() {
        //DispatchQueue.global().async也是一个逃逸闭包
        //他用到了实例成员(属性,方法),编译器会强制要求明确写出self
        DispatchQueue.global().async {
            self.fn()
        }
    }  
}

逃逸闭包的注意点

  • 逃逸闭包不可以捕获inout参数
typealias Fn = () -> ()
func other1(_ fn: Fn) {
    fn()
}
func other2(_ fn: @escaping Fn){
    fn()
}
func test(value: inout Int) -> Fn {
    other1 { value += 1 }
    
    //error: 逃逸闭包不能捕获inout参数
    other2 { value += 1 }
    
    func plus() { value += 1 }
    //error: 逃逸闭包不能捕获inout参数
    return plus
}

内存访问冲突 Conflicting Access to Memory

  • 内存访问冲突会在两个访问满足下列条件是发生:
    1.至少一个是写入操作
    2.他们访问的是同一块内存
    3.他们的访问时间重叠(比如在同一个函数内)

  • 不存在访问冲突

func plus(_ num: inout Int) -> Int {
    num + 1
}
var number = 1
number = plus(&number)
  • 存在内存访问冲突
//Simultaneous accesses to 0x100001030, but modification requires exclusive access.
var step = 1
func increment(_ num: inout Int) { num += step}
increment(&step)
  • 解决内存访问冲突
var step = 1
func increment(_ num: inout Int) { num += step}

var copyOfStep = step
increment(©OfStep)
step = copyOfStep
func balance(_ x: inout Int,_ y: inout Int) {
    let sum = x + y
    x = sum / 2
    y = sum - x
    
}
var num1 = 42
var num2 = 30
balance(&num1, &num2)//OK
//Inout arguments are not allowed to alias each other
//Overlapping accesses to 'num1', but modification requires exclusive access; consider copying to a local variable
balance(&num1, &num1)//Error
func balance(_ x: inout Int,_ y: inout Int) {
    let sum = x + y
    x = sum / 2
    y = sum - x
    
}
struct Player {
    var name: String
    var health: Int
    var energy: Int
    mutating func shareHealth(with teammate: inout Player) {
        balance(&teammate.health, &health)
    }
}
var oscar = Player(name: "Oscar", health: 10, energy: 10)
var maria = Player(name: "Maria", health: 5, energy: 10)
oscar.shareHealth(with: &maria)//ok
oscar.shareHealth(with: &oscar)//Error
var tuple = (health: 10 ,energy: 20)
//Error
balance(&tuple.health, &tuple.energy)

var holly = Player(name: "Holly", health: 10, energy: 10)
//Error
balance(&holly.health, *holly.energy)
  • 如果满足下面条件,就说明重叠访问结构体的属性是安全的
  • 只访问实例存储属性,不是计算属性或者类属性
  • 结构体是局部变量而非全局变量
  • 结构体要么没有被闭包捕获,要么只被非逃逸闭包捕获
func balance(_ x: inout Int,_ y: inout Int) {
    let sum = x + y
    x = sum / 2
    y = sum - x
    
}
struct Player {
    var name: String
    var health: Int
    var energy: Int
    mutating func shareHealth(with teammate: inout Player) {
        balance(&teammate.health, &health)
    }
}
func test() {
    var tuple = (health: 10,energy: 20)
    balance(&tuple.health, &tuple.energy)
    
    var holly = Player(name: "Holly", health: 10, energy: 10)
    balance(&holly.health, &holly.energy)
}
test()

你可能感兴趣的:(内存管理)