Swift-05:内存管理

2021-04-01  本文已影响0人  恍然如梦_b700

swift中的内存管理,涉及引用计数、弱引用、强引用、循环引用、Runtime是什么样子的呢?

内存管理 - 强引用

在swift中也是使用ARC来追踪和管理内存的,下面我们通过一个案例来进行分析

class CJLTeacher {
    var age: Int = 18
    var name: String = "CJL"
}
var t = CJLTeacher()
var t1 = t
var t2 = t

在分析类时(参考这篇文章Swift-进阶 02:类、对象、属性)有这么一个类HeapObject,下面继续通过这个类来分析t的引用计数

struct HeapObject {
  HeapMetadata const *metadata;

  SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS;
  ...
}
👇
#define SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS       \
  InlineRefCounts refCounts

typedef RefCounts<InlineRefCountBits> InlineRefCounts;
👇
template <typename RefCountBits>
class RefCounts {
  std::atomic<RefCountBits> refCounts;
  ...
}

typedef RefCountBitsT<RefCountIsInline> InlineRefCountBits;

template <RefCountInlinedness refcountIsInline>
class RefCountBitsT {
    ...
      typedef typename RefCountBitsInt<refcountIsInline, sizeof(void*)>::Type
    BitsType;
    ...
    BitsType bits;
    ...
}
👇
template <>
struct RefCountBitsInt<RefCountNotInline, 4> {
  //类型
  typedef uint64_t Type;
  typedef int64_t SignedType;
};

其中bits其实质是将RefCountBitsInt中的type属性取了一个别名,所以bits的真正类型是uint64_t64位整型数组

然后来继续分析swift中对象创建的底层方法swift_allocObject

static HeapObject *_swift_allocObject_(HeapMetadata const *metadata,
                                       size_t requiredSize,
                                       size_t requiredAlignmentMask) {
    ...
    new (object) HeapObject(metadata);
    ...
}
👇
<!--构造函数-->
 constexpr HeapObject(HeapMetadata const *newMetadata) 
    : metadata(newMetadata)
    , refCounts(InlineRefCounts::Initialized)
  { }

  enum Initialized_t { Initialized };

  //对应的RefCounts方法
// Refcount of a new object is 1.
constexpr RefCounts(Initialized_t)
: refCounts(RefCountBits(0, 1)) {}

从这里看出真正干事的是RefCountBits

template <typename RefCountBits>
class RefCounts {
  std::atomic<RefCountBits> refCounts;
  ...
}

所以真正的初始化地方是下面这个,实际上是做了一个位域操作,根据的是Offsets

LLVM_ATTRIBUTE_ALWAYS_INLINE
constexpr
RefCountBitsT(uint32_t strongExtraCount, uint32_t unownedCount)
: bits((BitsType(strongExtraCount) << Offsets::StrongExtraRefCountShift) |
       (BitsType(1)                << Offsets::PureSwiftDeallocShift) |
       (BitsType(unownedCount)     << Offsets::UnownedRefCountShift))
{ }

分析RefCountsBit的结构,如下所示,

image

重点关注UnownedRefCountStrongExtraRefCount

分析SIL代码

//SIL中的main
alloc_global @main.t1 : main.CJLTeacher       // id: %8
%9 = global_addr @main.t1 : main.CJLTeacher : $*CJLTeacher // user: %11
%10 = begin_access [read] [dynamic] %3 : $*CJLTeacher // users: %12, %11
copy_addr %10 to [initialization] %9 : $*CJLTeacher // id: %11

//其中copy_addr等价于
- %new = load s*LGTeacher
- strong_retain %new
- store %new to %9

image

SIL官方文档中关于copy_addr的解释如下

image
//内部是一个宏定义
HeapObject *swift::swift_retain(HeapObject *object) {
  CALL_IMPL(swift_retain, (object));
}
👇
//本质调用的就是 _swift_retain_
static HeapObject *_swift_retain_(HeapObject *object) {
  SWIFT_RT_TRACK_INVOCATION(object, swift_retain);
  if (isValidPointerForNativeRetain(object))
    object->refCounts.increment(1);
  return object;
}
👇
void increment(uint32_t inc = 1) {
    auto oldbits = refCounts.load(SWIFT_MEMORY_ORDER_CONSUME);

    // constant propagation will remove this in swift_retain, it should only
    // be present in swift_retain_n
    if (inc != 1 && oldbits.isImmortal(true)) {
      return;
    }
    //64位bits
    RefCountBits newbits;
    do {
      newbits = oldbits;
      bool fast = newbits.incrementStrongExtraRefCount(inc);
      if (SWIFT_UNLIKELY(!fast)) {
        if (oldbits.isImmortal(false))
          return;
        return incrementSlow(oldbits, inc);
      }
    } while (!refCounts.compare_exchange_weak(oldbits, newbits,
                                              std::memory_order_relaxed));
  }

LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE
bool incrementStrongExtraRefCount(uint32_t inc) {
// This deliberately overflows into the UseSlowRC field.
// 对inc做强制类型转换为 BitsType
// 其中 BitsType(inc) << Offsets::StrongExtraRefCountShift 等价于 1<<33位,16进制为 0x200000000
//这里的 bits += 0x200000000,将对应的33-63转换为10进制,为
bits += BitsType(inc) << Offsets::StrongExtraRefCountShift;
return (SignedBitsType(bits) >= 0);
}

例如以trefCounts为例(其中62-33位是strongCount,每次增加强引用计数增加都是在33-62位上增加的,固定的增量为1左移33位,即0x200000000

为什么是0x200000000
因为1左移33位,其中4位为一组,计算成16进制,剩余的33-32位0x10,转换为10进制为2。其实际增加引用技术就是1

swift与OC强引用计数对比

内存管理 - 弱引用

以下面为例:

class CJLTeacher {
    var age: Int = 18
    var name: String = "CJL"
    var stu: CJLStudent?
}

class CJLStudent {
    var age = 20
    var teacher: CJLTeacher?
}

func test(){
    var t = CJLTeacher()
    weak var t1 = t
}

WeakReference *swift::swift_weakInit(WeakReference *ref, HeapObject *value) {
  ref->nativeInit(value);
  return ref;
}

void nativeInit(HeapObject *object) {
auto side = object ? object->refCounts.formWeakReference() : nullptr;
nativeValue.store(WeakReferenceBits(side), std::memory_order_relaxed);
}

template <>
HeapObjectSideTableEntry* RefCounts<InlineRefCountBits>::formWeakReference()
{
  //创建 sideTable
  auto side = allocateSideTable(true);
  if (side)
  // 如果创建成功,则增加弱引用
    return side->incrementWeak();
  else
    return nullptr;
}

template <>
HeapObjectSideTableEntry* RefCounts<InlineRefCountBits>::allocateSideTable(bool failIfDeiniting)
{
  // 1、先拿到原本的引用计数
  auto oldbits = refCounts.load(SWIFT_MEMORY_ORDER_CONSUME);

  // Preflight failures before allocating a new side table.
  if (oldbits.hasSideTable()) {
    // Already have a side table. Return it.
    return oldbits.getSideTable();
  } 
  else if (failIfDeiniting && oldbits.getIsDeiniting()) {
    // Already past the start of deinit. Do nothing.
    return nullptr;
  }

  // Preflight passed. Allocate a side table.

  // FIXME: custom side table allocator
  //2、创建sideTable
  HeapObjectSideTableEntry *side = new HeapObjectSideTableEntry(getHeapObject());
  // 3、将创建的地址给到InlineRefCountBits
  auto newbits = InlineRefCountBits(side);

  do {
    if (oldbits.hasSideTable()) {
      // Already have a side table. Return it and delete ours.
      // Read before delete to streamline barriers.
      auto result = oldbits.getSideTable();
      delete side;
      return result;
    }
    else if (failIfDeiniting && oldbits.getIsDeiniting()) {
      // Already past the start of deinit. Do nothing.
      return nullptr;
    }

    side->initRefCounts(oldbits);

  } while (! refCounts.compare_exchange_weak(oldbits, newbits,
                                             std::memory_order_release,
                                             std::memory_order_relaxed));
  return side;
}

0xc000000020809a6c为例,将62、63位清零,变成0x20809A6C,然后左移3位(即InlineRefCountBits初始化方法),变成0x10404D360HeapObjectSideTableEntry对象地址,即散列表地址,然后通过x/8g读取

image

问题:如果此时再加一个强引用t2
查看其refCounts,t2是执行了strong_retain

image

总结

对于HeapObject来说,其refCounts有两种:

HeapObject {
    InlineRefCountBit {strong count + unowned count }

    HeapObjectSideTableEntry{
        HeapObject *object
        xxx
        strong Count + unowned Count(uint64_t)//64位
        weak count(uint32_t)//32位
    }
}

内存管理 - 循环引用

主要是研究闭包捕获外部变量,以下面代码为例

var age = 10
let clourse = {
    age += 1
}
clourse()
print(age)

<!--打印结果-->
11

从输出结果中可以看出:闭包内部对变量的修改将会改变外部原始变量的值,主要原因是闭包会捕获外部变量,这个与OC中的block是一致的

class CJLTeacher {
    var age = 18
    //反初始化器(当前实例对象即将被回收)
    deinit {
        print("CJLTeacher deinit")
    }
}
func test(){
    var t = CJLTeacher()
}
test()

<!--打印结果-->
CJLTeacher deinit

class CJLTeacher {
    var age = 18
    //反初始化器(当前实例对象即将被回收)
    deinit {
        print("CJLTeacher deinit")
    }
}
var t = CJLTeacher()
let clourse = {
    t.age += 1
}
clourse()

<!--打印结果-->
11

class CJLTeacher {
    var age = 18
    deinit {
        print("CJLTeacher deinit")
    }
}

func test(){
    var t = CJLTeacher()
    let clourse = {
        t.age += 1
    }
    clourse()
}
test()

<!--运行结果-->
CJLTeacher deinit

运行结果发现,闭包对 t 并没有强引用

class CJLTeacher {
    var age = 18

    var completionBlock: (() ->())?

    deinit {
        print("CJLTeacher deinit")
    }
}

func test(){
    var t = CJLTeacher()
    t.completionBlock = {
        t.age += 1
    }
}
test()

从运行结果发现,没有执行deinit方法,即没有打印CJLTeacher deinit,所以这里有循环引用

image

循环引用解决方法

有两种方式可以解决swift中的循环引用

func test(){
    var t = CJLTeacher()
    t.completionBlock = { [weak t] in
        t?.age += 1
    } 
}

func test(){
    var t = CJLTeacher()
    t.completionBlock = { [unowned t] in
        t.age += 1
    } 
}

捕获列表

请问下面代码的clourse()调用后,输出的结果是什么?

func test(){
    var age = 0
    var height = 0.0
    //将变量age用来初始化捕获列表中的常量age,即将0给了闭包中的age(值拷贝)
    let clourse = {[age] in
        print(age)
        print(height)
    }
    age = 10
    height = 1.85
    clourse()
}

<!--打印结果-->
0
1.85

所以从结果中可以得出:对于捕获列表中的每个常量,闭包会利用周围范围内具有相同名称的常量/变量,来初始化捕获列表中定义的常量。有以下几点说明:

swift中Runtime探索

请问下面代码,会打印方法和属性吗?

class CJLTeacher {
    var age: Int = 18
    func teach(){
        print("teach")
    }
}

let t = CJLTeacher()

func test(){
    var methodCount: UInt32 = 0
    let methodList = class_copyMethodList(CJLTeacher.self, &methodCount)
    for i in 0..<numericCast(methodCount) {
        if let method = methodList?[i]{
            let methodName = method_getName(method)
            print("方法列表:\(methodName)")
        }else{
            print("not found method")
        }
    }

    var count: UInt32 = 0
    let proList = class_copyPropertyList(CJLTeacher.self, &count)
    for i in 0..<numericCast(count) {
        if let property = proList?[i]{
            let propertyName = property_getName(property)
            print("属性成员属性:\(property)")
        }else{
            print("没有找到你要的属性")
        }
    }
    print("test run")
}
test()

运行结果如下,发现并没有打印方法和属性

image

结论

objc源码验证

(由于xcode12.2暂时无法运行objc源码,下列验证图片仅供参考)

#if __has_attribute(objc_root_class)
__attribute__((__objc_root_class__))
#endif
SWIFT_RUNTIME_EXPORT @interface SwiftObject<NSObject> {
 @private
  Class isa;
  //refCounts
  SWIFT_HEAPOBJECT_NON_OBJC_MEMBERS;
}

struct swift_class_t : objc_class {
    uint32_t flags;
    uint32_t instanceAddressOffset;
    uint32_t instanceSize;
    uint16_t instanceAlignMask;
    uint16_t reserved;

    uint32_t classSize;
    uint32_t classAddressOffset;
    void *description;
    // ...

    void *baseAddress() {
        return (void *)((uint8_t *)this - classAddressOffset);
    }
};

问题:为什么继承NSObject?:必须通过NSObject声明,来帮助编译器判断,当前类是一个和OC交互的类

元类型、AnyClass、Self

AnyObject

class CJLTeacher: NSObject {
    var age: Int = 18
}

var t = CJLTeacher()

//此时代表的就是当前CJLTeacher的实例对象
var t1: AnyObject = t

//此时代表的是CJLTeacher这个类的类型
var t2: AnyObject = CJLTeacher.self

//继承自AnyObject,表示JSONMap协议只有类才可以遵守
protocol JSONMap: AnyObject { }

例如如果是结构体遵守协议,会报错

image

需要将struct修改成class

//继承自AnyObject,表示JSONMap协议只有类才可以遵守
protocol JSONMap: AnyObject {

}
class CJLJSONMap: JSONMap {

}

Any

//如果使用AnyObject会报错,而Any不会
var array: [Any] = [1, "cjl", "", true]

AnyClass

T.self & T.Type

//此时的self类型是  CJLTeacher.Type
var t = CJLTeacher.self

打印结果如下

image
var t = CJLTeacher()
//实例对象地址:实例对象.self 返回实例对象本身
var t1 = t.self
//存储metadata元类型
var t2 = CJLTeacher.self

image

type(of:)

<!--demo1-->
var age = 10 as NSNumber
print(type(of: age))

<!--打印结果-->
__NSCFNumber

<!--demo2-->
//value - static type 静态类型:编译时期确定好的
//type(of:) - dynamic type:Int
var age = 10
//value的静态类型就是Any
func test(_ value: Any){

    print(type(of: value))
}

test(age)

<!--打印结果-->
Int

实践

demo1

请问下面这段代码的打印结果是什么?

class CJLTeacher{
    var age = 18
    var double = 1.85
    func teach(){
        print("LGTeacher teach")
    }
}
class CJLPartTimeTeacher: CJLTeacher {
    override func teach() {
        print("CJLPartTimeTeacher teach")
    }
}

func test(_ value: CJLTeacher){
    let valueType = type(of: value)
    value.teach()
    print(value)
}
var t = CJLPartTimeTeacher()
test(t)

<!--打印结果-->
CJLPartTimeTeacher teach
CJLTest.CJLPartTimeTeacher

demo2

请问下面代码的打印结果是什么?

protocol TestProtocol {

}
class CJLTeacher: TestProtocol{
    var age = 18
    var double = 1.85
    func teach(){
        print("LGTeacher teach")
    }
}

func test(_ value: TestProtocol){
    let valueType = type(of: value)
    print(valueType)
}
var t = CJLTeacher()
let t1: TestProtocol = CJLTeacher()
test(t)
test(t1)

<!--打印结果-->
CJLTeacher
CJLTeacher

func test<T>(_ value: T){
    let valueType = type(of: value)
    print(valueType)
}

<!--打印结果-->
CJLTeacher
TestProtocol

从结果中发现,打印并不一致,原因是因为当有协议、泛型时,当前的编译器并不能推断出准确的类型,需要将value转换为Any,修改后的代码如下:

func test<T>(_ value: T){
    let valueType = type(of: value as Any)
    print(valueType)
}

<!--打印结果-->
CJLTeacher
CJLTeacher

demo3

在上面的案例中,如果class_getClassMethod中传t.self,可以获取方法列表吗?

func test(){
    var methodCount: UInt32 = 0
    let methodList = class_copyMethodList(t.self, &methodCount)
    for i in 0..<numericCast(methodCount) {
        if let method = methodList?[i]{
            let methodName = method_getName(method)
            print("方法列表:\(methodName)")
        }else{
            print("not found method")
        }
    }

    var count: UInt32 = 0
    let proList = class_copyPropertyList(CJLTeacher.self, &count)
    for i in 0..<numericCast(count) {
        if let property = proList?[i]{
            let propertyName = property_getName(property)
            print("属性成员属性:\(property)")
        }else{
            print("没有找到你要的属性")
        }
    }
    print("test run")
}
test()

从结果运行看,并不能,因为t.self实例对象本身,即CJLTeacher,并不是CJLTeacher.Type类型

总结

上一篇下一篇

猜你喜欢

热点阅读