iOS-内存管理知识点整理

内存管理.png

image.png

image.png

indexed标识isa是否仅仅为一个内存指针,如果为1的话就仅是一个内存指针,如果为0的话则意味着内存的64位不仅仅用于存储内存指针

has_assoc代表该对象是否有关联属性
has_cxx_dtor代表对象是否有和c++相关的属性
shiftcls代表对象实际的内存地址
weakly_referenced代表对象是否有弱引用指向
deallocating标识对象是否正在被销毁
has_sidetable_rc代表对象是否有额外的引用计数表
extra_rc代表对象的引用计数(当对象的引用计数很小的时候对象的引用计数就记录在当前对象的isa指针中)


image.png
image.png

为什么不是一个sidetable呢??
如果只有一个表的话系统的操作对象的引用计数时其他的对象就在等待,这就降低了效率 。


image.png

使用64张table存储的话就能实现并发操作。

image.png

自旋锁是循环访问的机制,只适用于轻量访问的情况。

image.png

引用计数表的前两位分别标识该对象是否有弱引用以及是否处于正在销毁状态,所以计算引用计数时要向右偏移两位

image.png

弱引用表是一张Hash表,key为当前对象的指针地址,vlaue为指向当前对象的弱引用对象链表

MRC和ARC的区别

image.png
image.png

ARC的本质是编译器和Runtime协作的结果

image.png

引用计数的管理机制

image.png

alloc的时候并没有将对象的引用计数设置为1,而是retain的初始值为1

image.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.

image.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后的销毁过程

image.png

要判断对象是否有isa指针,弱引用,关联对象,c++相关和引用计数表并对应执行销毁操作。

image.png

object_dispose 实现

image.png

objc_destructInstance实现


image.png

销毁c++和关联对象后再销毁对象的引用计数和弱引用

image.png

弱引用初始化时Runtime会调用obc_initWeak函数初始化一个新的weak指针指向对象,经过objc_storeWeak调整参数,最终由weak_register_no_lock创建弱引用链表存储对象的指针。


image.png

image.png

清除weak变量并设置nil的过程如下


image.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释放。


image.png

问题2.autoreleasePool的实现原理是什么??
问题3.autoreleasePool是如何实现嵌套使用的??
问题4.在实际开发过程中autoreleasePool的应用场景是怎样的??

image.png

在实际的开发过程中@autoreleasePool{}会被改写成下图的代码会在要执行的代码前后分别加上autoreleasePoolPush和autoreleasePoolPop

image.png
image.png
image.png

autoreleasePoolPage的本质是以栈为节点通过双向链表的形式组合而成结构体,而且autoreleasePoolPage与具体的线程相关联

image.png

autoreleasePoolPush操作是向autoreleasePoolPage栈中插入哨兵对象标识本次的autorelease开始存储数据位置,autoreleasePoolPop首先找到相应的autoreleasePoolPage并对栈进行出栈操作,每出栈一个对象的同时会调用对象的release方法直到出栈至最上方的哨兵对象停止出栈。

image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png

NSTimer产生循环引用的原因和解决方案

[NSTimer scheduledTimerWithTimeInterval:interval target:object selector:@selector(fire:) userInfo:userInfo repeats:repeats];

NSTimer注册到当前线程的runloop中后 当前的runloop会持有timer的强引用,同时timer强引用当前对象形成环引用。


image.png

解决方法:在timer和object中间添加中间对象,中间对象分别弱引用timer和object,在每次执行timer的方法时都判断oject是否存在,存在则调用方法不存在则消除timer


image.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

你可能感兴趣的:(iOS-内存管理知识点整理)