iOS-内存管理知识点整理
![](https://img.haomeiwen.com/i2855123/ce0563ef2b4d47a3.png)
![](https://img.haomeiwen.com/i2855123/a42b7164eacfebc8.png)
![](https://img.haomeiwen.com/i2855123/1a9aa8188b040a0d.png)
indexed标识isa是否仅仅为一个内存指针,如果为1的话就仅是一个内存指针,如果为0的话则意味着内存的64位不仅仅用于存储内存指针
has_assoc代表该对象是否有关联属性
has_cxx_dtor代表对象是否有和c++相关的属性
shiftcls代表对象实际的内存地址
weakly_referenced代表对象是否有弱引用指向
deallocating标识对象是否正在被销毁
has_sidetable_rc代表对象是否有额外的引用计数表
extra_rc代表对象的引用计数(当对象的引用计数很小的时候对象的引用计数就记录在当前对象的isa指针中)
![](https://img.haomeiwen.com/i2855123/ca8a0ac29f6f3039.png)
![](https://img.haomeiwen.com/i2855123/7016df753a7f1d7f.png)
为什么不是一个sidetable呢??
如果只有一个表的话系统的操作对象的引用计数时其他的对象就在等待,这就降低了效率 。
![](https://img.haomeiwen.com/i2855123/d4095314d764499a.png)
使用64张table存储的话就能实现并发操作。
![](https://img.haomeiwen.com/i2855123/8ae08e799f5fdf34.png)
自旋锁是循环访问的机制,只适用于轻量访问的情况。
![](https://img.haomeiwen.com/i2855123/96b360d19cfb4a4f.png)
引用计数表的前两位分别标识该对象是否有弱引用以及是否处于正在销毁状态,所以计算引用计数时要向右偏移两位
![](https://img.haomeiwen.com/i2855123/46ef7308559886a6.png)
弱引用表是一张Hash表,key为当前对象的指针地址,vlaue为指向当前对象的弱引用对象链表
MRC和ARC的区别
![](https://img.haomeiwen.com/i2855123/3ade8e0687a3ac50.png)
![](https://img.haomeiwen.com/i2855123/cf7f65adf4cc9768.png)
ARC的本质是编译器和Runtime协作的结果
![](https://img.haomeiwen.com/i2855123/1487f28cbd99da9d.png)
引用计数的管理机制
![](https://img.haomeiwen.com/i2855123/3622cbae0cb535b0.png)
alloc的时候并没有将对象的引用计数设置为1,而是retain的初始值为1
![](https://img.haomeiwen.com/i2855123/dae9d7b4f7533ec6.png)
objc_object::sidetable_retainCount()
{
SideTable& table = SideTables()[this];
size_t refcnt_result = 1;
table.lock();
RefcountMap::iterator it = table.refcnts.find(this);
if (it != table.refcnts.end()) {
// this is valid for SIDE_TABLE_RC_PINNED too
refcnt_result += it->second >> SIDE_TABLE_RC_SHIFT;
}
table.unlock();
return refcnt_result;
}
retainCount的实现:先在sidetables表集合中根据对象的指针地址找到对应的sidetable,再sidetable中的通过hash查找找到对象对应的引用计数,如果当前的表中对象是新建的话,此时hash表中的it->second为0,返回默认值累加值1.
如果不为0,则将对应数值向右偏移两位后加1.
![](https://img.haomeiwen.com/i2855123/a7b92c7e8198c428.png)
id
objc_object::sidetable_retain()
{
#if SUPPORT_NONPOINTER_ISA
assert(!isa.indexed);
#endif
SideTable& table = SideTables()[this];
if (table.trylock()) {
size_t& refcntStorage = table.refcnts[this];
if (! (refcntStorage & SIDE_TABLE_RC_PINNED)) {
refcntStorage += SIDE_TABLE_RC_ONE;
}
table.unlock();
return (id)this;
}
return sidetable_retain_slow(table);
}
对象的调用dealloc后的销毁过程
![](https://img.haomeiwen.com/i2855123/a633e5bfcacfc14c.png)
要判断对象是否有isa指针,弱引用,关联对象,c++相关和引用计数表并对应执行销毁操作。
![](https://img.haomeiwen.com/i2855123/3af9db5c8f9d7640.png)
object_dispose 实现
![](https://img.haomeiwen.com/i2855123/80637f2b626901b2.png)
objc_destructInstance实现
![](https://img.haomeiwen.com/i2855123/b877e1fe76d3fd38.png)
销毁c++和关联对象后再销毁对象的引用计数和弱引用
![](https://img.haomeiwen.com/i2855123/e06c7bbd8f1f538c.png)
![](https://img.haomeiwen.com/i2855123/d11384532b22d34c.png)
弱引用初始化时Runtime会调用obc_initWeak函数初始化一个新的weak指针指向对象,经过objc_storeWeak调整参数,最终由weak_register_no_lock创建弱引用链表存储对象的指针。
image.png
image.png
清除weak变量并设置nil的过程如下
![](https://img.haomeiwen.com/i2855123/c00889ec03ebe2fc.png)
释放时,调用sidetable_clearDeallocating函数,然后调用weak_clear_no_lock()根据对象的地址获取到为weak表中的指向该对象的弱指针数组,遍历数组将数组中的所有项都设置为nil,最后把entry从weak表中删除。
总结一下 添加weak变量中会调用 initWeak(),storeWeak(),weak_register_no_lock()函数。清除weak变量置nil会调用learDeallocating(),weak_clear_no_lock()函数。在添加和删除的过程中都不断的使用hash查找定位变量在weak表中的位置。
自动释放池autoreleasePool
问题1.图中的array是什么时候被释放的??
在每次runloop开始都会调用一次autoreleasePoolPush操作,每次runloop结束的时候都会调用一次autoreleasePoolPop操作,array就是在pop操作的过程中被release释放。
![](https://img.haomeiwen.com/i2855123/099d79135c6cc95d.png)
问题2.autoreleasePool的实现原理是什么??
问题3.autoreleasePool是如何实现嵌套使用的??
问题4.在实际开发过程中autoreleasePool的应用场景是怎样的??
![](https://img.haomeiwen.com/i2855123/cde2a175d1f174d4.png)
在实际的开发过程中@autoreleasePool{}会被改写成下图的代码会在要执行的代码前后分别加上autoreleasePoolPush和autoreleasePoolPop
![](https://img.haomeiwen.com/i2855123/098f793f98d4cbb4.png)
![](https://img.haomeiwen.com/i2855123/82c18cd414b0bc33.png)
![](https://img.haomeiwen.com/i2855123/6092871e0aef6ef7.png)
autoreleasePoolPage的本质是以栈为节点通过双向链表的形式组合而成结构体,而且autoreleasePoolPage与具体的线程相关联
![](https://img.haomeiwen.com/i2855123/e8d843cb7d58bc7c.png)
autoreleasePoolPush操作是向autoreleasePoolPage栈中插入哨兵对象标识本次的autorelease开始存储数据位置,autoreleasePoolPop首先找到相应的autoreleasePoolPage并对栈进行出栈操作,每出栈一个对象的同时会调用对象的release方法直到出栈至最上方的哨兵对象停止出栈。
![](https://img.haomeiwen.com/i2855123/b7acf9e3809b58f3.png)
![](https://img.haomeiwen.com/i2855123/86a0e7a6319686e5.png)
![](https://img.haomeiwen.com/i2855123/569637c858da064c.png)
![](https://img.haomeiwen.com/i2855123/e9bbaf9c0964c690.png)
![](https://img.haomeiwen.com/i2855123/30ca0b65cc0f2cf9.png)
![](https://img.haomeiwen.com/i2855123/540faec7e1e5d4a0.png)
![](https://img.haomeiwen.com/i2855123/6ee6c3919d32294b.png)
![](https://img.haomeiwen.com/i2855123/bafea35be6bda3f9.png)
![](https://img.haomeiwen.com/i2855123/a0e11da48fe268fa.png)
![](https://img.haomeiwen.com/i2855123/d8ac1d1b234830f7.png)
![](https://img.haomeiwen.com/i2855123/a6c18c812576bf20.png)
![](https://img.haomeiwen.com/i2855123/d31aa741a44ae8b7.png)
![](https://img.haomeiwen.com/i2855123/659d2f6ef1811aef.png)
![](https://img.haomeiwen.com/i2855123/99d669721ef8fba9.png)
![](https://img.haomeiwen.com/i2855123/8df6568390d4915c.png)
NSTimer产生循环引用的原因和解决方案
[NSTimer scheduledTimerWithTimeInterval:interval target:object selector:@selector(fire:) userInfo:userInfo repeats:repeats];
NSTimer注册到当前线程的runloop中后 当前的runloop会持有timer的强引用,同时timer强引用当前对象形成环引用。
![](https://img.haomeiwen.com/i2855123/9c59f19a67af64a5.png)
解决方法:在timer和object中间添加中间对象,中间对象分别弱引用timer和object,在每次执行timer的方法时都判断oject是否存在,存在则调用方法不存在则消除timer
![](https://img.haomeiwen.com/i2855123/00713641a83c2339.png)
#import "NSTimer+WeakTimer.h"
@interface TimerWeakObject : NSObject
@property (nonatomic, weak) id target;
@property (nonatomic, assign) SEL selector;
@property (nonatomic, weak) NSTimer *timer;
- (void)fire:(NSTimer *)timer;
@end
@implementation TimerWeakObject
- (void)fire:(NSTimer *)timer
{
if (self.target) {
if ([self.target respondsToSelector:self.selector]) {
[self.target performSelector:self.selector withObject:timer.userInfo];
}
}
else{
[self.timer invalidate];
}
}
@end
@implementation NSTimer (WeakTimer)
+ (NSTimer *)scheduledWeakTimerWithTimeInterval:(NSTimeInterval)interval
target:(id)aTarget
selector:(SEL)aSelector
userInfo:(id)userInfo
repeats:(BOOL)repeats
{
TimerWeakObject *object = [[TimerWeakObject alloc] init];
object.target = aTarget;
object.selector = aSelector;
object.timer = [NSTimer scheduledTimerWithTimeInterval:interval target:object selector:@selector(fire:) userInfo:userInfo repeats:repeats];
return object.timer;
}
@end