[Swift拓展]4、指针&内存

2024-04-03  本文已影响0人  史记_d5da

1、内存管理

Swift是通过引用计数(ARC)来管理内存的

1.1、swift源码查看

查找过程如下
1、HeapObject类中的 refCounts变量记录引用计数

InlineRefCounts
2、InlineRefCounts为模板类,类型为RefCounts<InlineRefCountBits>
InlineRefCountBits
3、RefCounts类内部存储了InlineRefCountBits
InlineRefCountBits
4、查找InlineRefCountBitsInlineRefCountBits由泛型RefCountBitsT<RefCountIsInline>类型定义
RefCountBitsT<RefCountIsInline>
5、最终查找类RefCountBitsT,构造函数为RefCountBitsT()
RefCountBitsT类
6、通过查找64位结构体,查找到对应的值
RefCountBitOffsets
RefCountBitsT函数对应的入参位域运算分别如下
strongExtraCount: 0
unownedCount: 1
StrongExtraRefCountShift: 33,(左移33位)(强引用次数)
PureSwiftDeallocShift: 1,(左移0位)(析构次数)
UnownedRefCountShift: 1,(左移1位)(无主引用次数)

1.2、代码实际操作

1、如下的代码断点查看

demo
断点在108处,0x0000000400000003
内存值
结合上面的RefCountBitsT函数,0x0000000400000003,代表PureSwiftDeallocShift为1,UnownedRefCountShift为1,StrongExtraRefCountShift为2
位值
上一篇 下一篇

猜你喜欢

热点阅读