内存管理
跟OC一样,Swift也是采取基于引用计数的ARC内存管理方案(针对堆空间)
-
Swift的ARC中有3种引用
- 强引用(strong reference):默认情况下,引用都是强引用
- 弱引用(weak reference):通过weak定义弱引用
- 必须是可选类型的var,因为实例销毁后,ARC会自动将弱引用设置为nil
- ARC自动给弱引用设置nil时,不会触发属性观察器
- 无主引用(unowned reference):通过unowned定义无主引用
- 不会产生强引用,实例销毁后仍然存储着实例的内存地址(类似于OC中的unsafe_unretained)
- 试图在实例销毁后访问无主引用,会产生运行时错误(野指针)
• Fatal error: Attempted to read an unowned reference but object 0x0 was already deallocated
weak、unowned的使用限制
- weak、unowned只能用在类实例上面
protocol 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<Result>(invoking body: () throws -> Result) rethrows -> Result
autoreleasepool {
let p = MJPerson(age: 20, name: "Jack")
p.run()
}
循环引用(Reference Cycle)
- weak、unowned 都能解决循环引用的问题,unowned 要比weak 少一些性能消耗
- 在生命周期中可能会变为nil 的使用weak
- 初始化赋值后再也不会变为nil 的使用unowned
闭包的循环引用
@escaping
逃逸闭包的注意点
- 逃逸闭包不可以捕获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)
内存访问冲突
指针
- Swift中也有专门的指针类型,这些都被定性为“Unsafe”(不安全的),常见的有以下4种类型
- UnsafePointer<Pointee> 类似于const Pointee *
- UnsafeMutablePointer<Pointee> 类似于Pointee *
- UnsafeRawPointer 类似于const void *
- UnsafeMutableRawPointer 类似于void *
var age = 10
func test1(_ ptr: UnsafeMutablePointer<Int>) {
ptr.pointee += 10
}
func test2(_ ptr: UnsafePointer<Int>) {
print(ptr.pointee)
}
test1(&age)
test2(&age) // 20
print(age) // 20
var age = 10
func test3(_ ptr: UnsafeMutableRawPointer) {
ptr.storeBytes(of: 20, as: Int.self)
}
func test4(_ ptr: UnsafeRawPointer) {
print(ptr.load(as: Int.self))
}
test3(&age)
test4(&age) // 20
print(age) // 20
指针的应用示例
// 仿照oc的遍历写法
var arr = NSArray(objects: 11, 22, 33, 44)
arr.enumerateObjects { (obj, idx, stop) in
print(idx, obj)
if idx == 2 { // 下标为2就停止遍历
stop.pointee = true
}
}
// Swift自带的遍历写法
var arr = NSArray(objects: 11, 22, 33, 44)
for (idx, obj) in arr.enumerated() {
print(idx, obj)
if idx == 2 {
break
}
}
获得指向某个变量的指针
var age = 11
var ptr1 = withUnsafeMutablePointer(to: &age) { $0 }
var ptr2 = withUnsafePointer(to: &age) { $0 }
ptr1.pointee = 22
print(ptr2.pointee) // 22
print(age) // 22
// 返回相应的初始化器UnsafeMutableRawPointer和UnsafeRawPointer
var ptr3 = withUnsafeMutablePointer(to: &age) { UnsafeMutableRawPointer($0) }
var ptr4 = withUnsafePointer(to: &age) { UnsafeRawPointer($0) }
ptr3.storeBytes(of: 33, as: Int.self)
print(ptr4.load(as: Int.self)) // 33
print(age) // 33
获得指向堆空间实例的指针
class Person {}
var person = Person()
var ptr = withUnsafePointer(to: &person) { UnsafeRawPointer($0) }
var heapPtr = UnsafeRawPointer(bitPattern: ptr.load(as: UInt.self))
print(heapPtr!)
创建指针一
var ptr = UnsafeRawPointer(bitPattern: 0x100001234)
// 创建
var ptr = malloc(16)
// 存
ptr?.storeBytes(of: 11, as: Int.self)
ptr?.storeBytes(of: 22, toByteOffset: 8, as: Int.self)
// 取
print((ptr?.load(as: Int.self))!) // 11
print((ptr?.load(fromByteOffset: 8, as: Int.self))!) // 22
// 销毁
free(ptr)
var ptr = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
ptr.storeBytes(of: 11, as: Int.self)
ptr.advanced(by: 8).storeBytes(of: 22, as: Int.self)
print(ptr.load(as: Int.self)) // 11
print(ptr.advanced(by: 8).load(as: Int.self)) // 22
ptr.deallocate()
var ptr = UnsafeRawPointer(bitPattern: 0x100001234)
创建指针二
var ptr = UnsafeMutablePointer<Int>.allocate(capacity: 3)
ptr.initialize(to: 11)
ptr.successor().initialize(to: 22)
ptr.successor().successor().initialize(to: 33)
print(ptr.pointee) // 11
print((ptr + 1).pointee) // 22
print((ptr + 2).pointee) // 33
print(ptr[0]) // 11
print(ptr[1]) // 22
print(ptr[2]) // 33
ptr.deinitialize(count: 3)
ptr.deallocate()
创建指针三
class Person {
var age: Int
var name: String
init(age: Int, name: String) {
self.age = age
self.name = name
}
deinit { print(name, "deinit") }
}
var ptr = UnsafeMutablePointer<Person>.allocate(capacity: 3)
ptr.initialize(to: Person(age: 10, name: "Jack"))
(ptr + 1).initialize(to: Person(age: 11, name: "Rose"))
(ptr + 2).initialize(to: Person(age: 12, name: "Kate"))
// Jack deinit
// Rose deinit
// Kate deinit
ptr.deinitialize(count: 3)
ptr.deallocate()
指针之间的转换
var ptr = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
ptr.assumingMemoryBound(to: Int.self).pointee = 11
(ptr + 8).assumingMemoryBound(to: Double.self).pointee = 22.0
print(unsafeBitCast(ptr, to: UnsafePointer<Int>.self).pointee) // 11
print(unsafeBitCast(ptr + 8, to: UnsafePointer<Double>.self).pointee) // 22.0
ptr.deallocate()
- unsafeBitCast是忽略数据类型的强制转换,不会因为数据类型的变化而改变原来的内存数据
类似于C++中的reinterpret_cast
class Person {}
var person = Person()
var ptr = unsafeBitCast(person, to: UnsafeRawPointer.self)
print(ptr)