iOS-底层原理34-内存管理(下)和RunLoop

《iOS底层原理文章汇总》
上一篇文章iOS-底层原理33-内存管理(上)介绍了内存管理,本文接着介绍内存管理(下)和RunLoop

class AutoreleasePoolPage : private AutoreleasePoolPageData
1.指针->栈的结构
2.对象释放 + POOL_BOUNDARY 哨兵 -> 边界
3.页的结构+双向链表
4.线程有关系

1.AutoreleasePool

I.Clang编译底层源码@autoreleasepool{},相当于__AtAutoreleasePool __autoreleasepool;,{}表示作用域,__AtAutoreleasePool为结构体,由构造函数和析构函数组成

 struct __AtAutoreleasePool {
   __AtAutoreleasePool() {atautoreleasepoolobj = objc_autoreleasePoolPush();}
   ~__AtAutoreleasePool() {objc_autoreleasePoolPop(atautoreleasepoolobj);}
   void * atautoreleasepoolobj;
 };

image.png

II.自动释放池的结构,AutoreleasePoolPage::push(),class AutoreleasePoolPage : private AutoreleasePoolPageData,::相当于类调用类方法
image.png

image.png

image.png

III.自动释放池的结构分析
AutoreleasePoolPage继承于AutoreleasePoolPageData,初始化方法来自于AutoreleasePoolPageData
image.png

image.png

image.png

image.png

内存平移56个字节
image.png

image.png

image.png

image.png

IV.自动释放池释放原理,################ POOL 0x103009038 哨兵对象 边界 越界,3*16+8 = 56 AutoreleasePool自身的begin()位置,哨兵对象表示边界
image.png

若放504个对象正好一页,504个对象加上一个哨兵对象505个对象
image.png

若放505个对象,重新开始一页,指针地址从38开始,新的一页,每一页的大小为4096个字节=505*8=4040+自身大小56=4096


image.png

define I386_PGBYTES 4096

define PAGE_SIZE I386_PGBYTES

define PAGE_MIN_SIZE PAGE_SIZE

image.png

从第二页开始可以压入多少个对象?哨兵对象在一个自动释放池中存在几个?
image.png

V.对象是怎么压栈的,页是怎么关联的?
0.判断页面存不存在
1.push:压栈进来一个边界+当前页,变为hotPage,表示当前正在操作的页面,coldPage表示当前没有被操作的页面
2.判断页是否满了,没满,继续添加页面,满了新建页,设置为coldPage
child是子的一页,新的一页
image.png

image.png

VI.autorelease怎么加入自动释放池中
image.png

image.png

image.png

VII.对象在自动释放池中的出栈,出栈会调用atautoreleasepoolobj = objc_autoreleasePoolPush();objc_autoreleasePoolPop(atautoreleasepoolobj);,压栈找child,出栈找parent
image.png

image.png

image.png

releaseUntil
image.png

kill
image.png

从子节点到父节点,不断的出栈,kill掉初始化数据,往父节点平移
image.png

2.Runloop

I.保持程序的持续运行
II.处理APP中的各种事件(触摸、定时器、performSelector)
III.节省cpu资源、提供程序的性能:该做事就做事,该休息就休息

image.png

runloop是一个do while循环,和普通的循环有区别,循环会持续占用cpu,runloop不会持续占用cpu
image.png

runloop中的item
image.png

timer类型响应runloop
image.png

IV.runloop是什么
通过主运行循环查询源码CFRunLoopRef mainRunloop = CFRunLoopGetMain()
image.png

image.png

线程和runloop绑定是怎么创建的呢?runloop是一个对象,有多个属性
image.png

image.png

V.runloop和线程的关系
image.png

VI.runloop的事务处理原理:子线程的runloop默认不启动
image.png

子线程的runloop不run,子线程中的timer不会执行,timer要加到对应模型的runloop中才能执行,timer是如何加到runloop中的呢?
image.png

存在多种类型的事务,timer,observer,source,main_dispatch_queue,block(),timer中的内容是如何回调的?
runloop中add(timer)会调用,什么时候执行呢?在调用runloop run的时候
image.png

__CFRunLoopDoTimers(rl, rlm, mach_absolute_time())->__CFRunLoopDoTimer(rl, rlm, rlt)
image.png

image.png

timer的执行分为四步
A.timer依赖的mode加入runloop
B.runloop run -> do timers 的时候把所有的timers执行
C.执行单个的timer, do timer
D.执行回调函数
同理,source和observer是一样的道理
image.png

image.png

VII.runloop底层原理
image.png

image.png

image.png

image.png

image.png

image.png

RunLoop在kCFRunLoopBeforeWaiting和kCFRunLoopAfterWaiting两个状态间进行切换

VIII.子线程中发送通知要添加runloop,否则会无法进入回调函数,无法收到通知

@property (nonatomic, strong) NSThread *thread;
- (NSThread *)thread {
    if (!_thread) {
        _thread = [[NSThread alloc] initWithBlock:^{
            NSRunLoop *ns_runloop = [NSRunLoop currentRunLoop];
            [ns_runloop addPort:[NSPort port] forMode:NSRunLoopCommonModes];

            CFRunLoopRef runloop = CFRunLoopGetCurrent();
            CFRunLoopObserverRef observer = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(), kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
                switch (activity) {
                    case kCFRunLoopEntry:
                        NSLog(@"进入runLoop");
                        break;
                    case kCFRunLoopBeforeTimers:
                        NSLog(@"处理timer事件");
                        break;
                    case kCFRunLoopBeforeSources:
                        NSLog(@"处理source事件");
                        break;
                    case kCFRunLoopBeforeWaiting:
                        NSLog(@"进入睡眠");
                        break;
                    case kCFRunLoopAfterWaiting:
                        NSLog(@"被唤醒");
                        break;
                    case kCFRunLoopExit:
                        NSLog(@"退出");
                        break;
                    default:
                        break;
                }
            });
            CFRunLoopAddObserver(runloop, observer, kCFRunLoopCommonModes);
            CFRelease(observer);
            [ns_runloop run];
        }];
        [_thread start];
    }
    return _thread;
}

- (void)postNotification:(UIButton *)sender {
    NSLog(@"postNotification:%@", [NSThread currentThread]);
    [self performSelector:@selector(postNotification) onThread:self.thread withObject:nil waitUntilDone:YES];
}

- (void)postNotification {
    NSLog(@"1");
    NSLog(@"%@", [NSThread currentThread]);
    //NSPostWhenIdle
    //NSPostASAP
    //NSPostNow
    NSNotification *notification = [NSNotification notificationWithName:@"JKRNO" object:nil];
    [[NSNotificationQueue defaultQueue] enqueueNotification:notification postingStyle:NSPostWhenIdle coalesceMask:NSNotificationNoCoalescing forModes:@[NSDefaultRunLoopMode]];
    NSLog(@"3");
}

- (void)receiceNotification:(NSNotification *)notification {
    NSLog(@"2");
    NSLog(@"%@", [NSThread currentThread]);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiceNotification:)
                                                 name:@"JKRNO"
                                               object:nil];
    NSLog(@"viewDidLoad addObserver:%@",[NSThread currentThread]);
}
//输出
2021-08-14 12:02:40.511246+0800 NotificationCenter[24109:26743192] 被唤醒
2021-08-14 12:02:40.514713+0800 NotificationCenter[24109:26743192] 处理timer事件
2021-08-14 12:02:40.514929+0800 NotificationCenter[24109:26743192] 处理source事件
2021-08-14 12:02:40.556199+0800 NotificationCenter[24109:26743192] 处理timer事件
2021-08-14 12:02:40.556345+0800 NotificationCenter[24109:26743192] 处理source事件
2021-08-14 12:02:40.556662+0800 NotificationCenter[24109:26743192] 处理timer事件
2021-08-14 12:02:40.556754+0800 NotificationCenter[24109:26743192] 处理source事件
2021-08-14 12:02:40.556938+0800 NotificationCenter[24109:26743192] 进入睡眠
2021-08-14 12:02:40.566032+0800 NotificationCenter[24109:26743192] 被唤醒
2021-08-14 12:02:40.566189+0800 NotificationCenter[24109:26743192] 处理timer事件
2021-08-14 12:02:40.566285+0800 NotificationCenter[24109:26743192] 处理source事件
2021-08-14 12:02:40.568599+0800 NotificationCenter[24109:26743192] postNotification:<NSThread: 0x600003c3ca00>{number = 1, name = main}
2021-08-14 12:02:50.351417+0800 NotificationCenter[24109:26743889] 被唤醒
2021-08-14 12:02:50.351604+0800 NotificationCenter[24109:26743889] 处理timer事件
2021-08-14 12:02:50.351728+0800 NotificationCenter[24109:26743889] 处理source事件
2021-08-14 12:02:50.351833+0800 NotificationCenter[24109:26743889] 1
2021-08-14 12:02:50.351997+0800 NotificationCenter[24109:26743889] <NSThread: 0x600003c7fc00>{number = 9, name = (null)}
2021-08-14 12:02:50.352976+0800 NotificationCenter[24109:26743889] 3
2021-08-14 12:02:50.353519+0800 NotificationCenter[24109:26743192] 处理timer事件
2021-08-14 12:02:50.353145+0800 NotificationCenter[24109:26743889] 退出
2021-08-14 12:02:50.353637+0800 NotificationCenter[24109:26743192] 处理source事件
2021-08-14 12:02:50.353749+0800 NotificationCenter[24109:26743192] 进入睡眠
2021-08-14 12:02:50.353701+0800 NotificationCenter[24109:26743889] 进入runLoop
2021-08-14 12:02:50.354506+0800 NotificationCenter[24109:26743889] 处理timer事件
2021-08-14 12:02:50.354949+0800 NotificationCenter[24109:26743889] 处理source事件
2021-08-14 12:02:50.355314+0800 NotificationCenter[24109:26743889] 进入睡眠
2021-08-14 12:02:53.405512+0800 NotificationCenter[24109:26743889] 2
2021-08-14 12:02:53.406716+0800 NotificationCenter[24109:26743889] <NSThread: 0x600003c7fc00>{number = 9, name = (null)}
2021-08-14 12:02:53.407182+0800 NotificationCenter[24109:26743889] 被唤醒
2021-08-14 12:02:53.407860+0800 NotificationCenter[24109:26743889] 处理timer事件
2021-08-14 12:02:53.408877+0800 NotificationCenter[24109:26743889] 处理source事件
2021-08-14 12:02:53.409602+0800 NotificationCenter[24109:26743889] 进入睡眠

runloop状态

/* Run Loop Observer Activities */
typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {
    kCFRunLoopEntry = (1UL << 0),
    kCFRunLoopBeforeTimers = (1UL << 1),
    kCFRunLoopBeforeSources = (1UL << 2),
    kCFRunLoopBeforeWaiting = (1UL << 5),
    kCFRunLoopAfterWaiting = (1UL << 6),
    kCFRunLoopExit = (1UL << 7),
    kCFRunLoopAllActivities = 0x0FFFFFFFU
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容