AutoreleasePool 自动释放池原理探索

1、通过 main.m 开始探索

int main(int argc, char * argv[]) {
    @autoreleasepool {
        
    }
    return 0;
}
1.1、 通过main.m 的 cpp 文件

clang -rewrite-objc main.m -o main.cpp

获取 autoreleasepool{} 源码:

struct __AtAutoreleasePool {
  __AtAutoreleasePool() {atautoreleasepoolobj = objc_autoreleasePoolPush();}
  ~__AtAutoreleasePool() {objc_autoreleasePoolPop(atautoreleasepoolobj);}
  void * atautoreleasepoolobj;
};
......省略
int main(int argc, const char * argv[]) {
    /* @autoreleasepool */
    {
        __AtAutoreleasePool __autoreleasepool;
    }
    return 0;
}

得出结论,struct __AtAutoreleasePool 是个结构体,包含构造函数、析构函数,调用实现两个方法 objc_autoreleasePoolPushobjc_autoreleasePoolPop.

为什么会调用析构函数:
因为花括号表示一个作用域,超出作用域就会析构。

1.2、通过汇编
@autoreleasepool{}.png

因此,从 objc_autoreleasePoolPushobjc_autoreleasePoolPop 入手。

2、objc源码分析

2.1 Autorelease pool implementation

A thread's autorelease pool is a stack of pointers. //以栈为节点
Each pointer is either an object to release, or POOL_BOUNDARY which is an autorelease pool boundary.//对象去释放或者边界(哨兵)
A pool token is a pointer to the POOL_BOUNDARY for that pool. When the pool is popped, every object hotter than the sentinel is released.
The stack is divided into a doubly-linked list of pages. Pages are added and deleted as necessary. // doubly-linked 双向链表
Thread-local storage points to the hot page, where newly autoreleased objects are stored.//tls 线程

通过文档分析得出:

  • 以栈为节点,先进后出
  • 有边界和对象释放有关联
  • 双向链表
  • tls 线程
2.2、查看私有变量 AutoreleasePoolPageData

objc_autoreleasePoolPush->AutoreleasePoolPage->AutoreleasePoolPageData

    magic_t const magic; //  16
    __unsafe_unretained id *next; //8
    pthread_t const thread; // 8
    AutoreleasePoolPage * const parent; //8
    AutoreleasePoolPage *child; //8
    uint32_t const depth; // 4
    uint32_t hiwat; // 4
//56 字节
  • magic 用来校验 AutoreleasePoolPage 结构是否完整;
  • next 指向最新添加的 autorelease 对象的下一个位置,初始化指向 begin();
  • thread 指向当前线程
  • parent 指向父结点,第一个父结点为nil
  • child 指向子结点,最后一个子结点为nil
  • depath 深度,从 0 开始,以 1 往后递增;
  • hiwat (high water mark) 最大入栈数量标记。
2.3、通过源码分析 push() 过程

找到开始方法 objc_autoreleasePoolPush

void *
objc_autoreleasePoolPush(void)
{
    return AutoreleasePoolPage::push();//0 开始
}

点击 push() 进入显示如下, 判断是否需要新建分页

if (slowpath(DebugPoolAllocation)) {//
            // Each autorelease pool starts on a new pool page.
            dest = autoreleaseNewPage(POOL_BOUNDARY);
        } else {
            dest = autoreleaseFast(POOL_BOUNDARY);
}

选择 ** autoreleaseFast** 进入

        AutoreleasePoolPage *page = hotPage();
        if (page && !page->full()) {//判断是否存在分页且是否满了
            return page->add(obj);
        } else if (page) {
            return autoreleaseFullPage(obj, page);
        } else {
            return autoreleaseNoPage(obj);
        }

判断:如果存在分页且当前页没有满就 add() ,否则存在分页且满了进入 autoreleaseFullPage 创建新的分页;如果都不满足,进入autoreleaseNoPage 创建第一个分页。

添加对象 add(obj) , 通过指针偏移移动对象添加位置

id *add(id obj)
    {
        ASSERT(!full());
        unprotect();
        id *ret = next;  // faster than `return next-1` because of aliasing
        *next++ = obj;//原位置向下偏移一位8字节
        protect();
        return ret;
    }

autoreleaseFullPage 方法 do while 循环实现

.......
        do {
            if (page->child) page = page->child;
            else page = new AutoreleasePoolPage(page);//3 步骤
        } while (page->full());

        setHotPage(page);
        return page->add(obj);
......

判断如果满了就寻找下一个分页 page->child , 如果找到了就继续 add, 否则创建新的分页 AutoreleasePoolPage

autoreleaseNoPage 创建第一个分页

......
// Install the first page.
AutoreleasePoolPage *page = new AutoreleasePoolPage(nil);
......

创建分页,调用 begin() 方法

......
AutoreleasePoolPageData(begin(),/**4 步骤*/
                                objc_thread_self(),
                                newParent,
                                newParent ? 1+newParent->depth : 0,
                                newParent ? newParent->hiwat : 0)
......

点击进入 begin() 方法

id * begin() {// 5 步骤 偏移 sizeof(*this)=56 个字节开始 添加对象
        return (id *) ((uint8_t *)this+sizeof(*this));
    }

打断点控制台打印 sizeof(*this) = 56,此处与 AutoreleasePoolPageData 变量字节数一致。

push() 流程总结如下图:

push().png

2.4、验证边界和分页
extern void _objc_autoreleasePoolPrint(void);
int main(int argc, const char * argv[]) {
      @autoreleasepool {
          NSObject *objc = [[NSObject alloc] autorelease];
          NSLog(@"objc = %@",objc);
          _objc_autoreleasePoolPrint();
      }

return 0;
}

源码执行上面代码,控制台打印如下:

ObjcTest[37939:4515356] objc =
objc[37939]: ##############
objc[37939]: AUTORELEASE POOLS for thread 0x1000d1dc0
objc[37939]: 2 releases pending.
objc[37939]: [0x101017000] ................ PAGE (hot) (cold)
objc[37939]: [0x101017038] ################ POOL 0x101017038
objc[37939]: [0x101017040] 0x10071f720 NSObject
objc[37939]: ##############
Program ended with exit code: 0

分析:添加一个对象,控制台打印 2 releases pending, 说明其中一个是边界,即 POOL 0x101017038;其中 PAGE (hot) (cold) 是标记当前分页是否满了,为此我们多添加一些对象看控制台打印结果

extern void _objc_autoreleasePoolPrint(void);
int main(int argc, const char * argv[]) {
      @autoreleasepool {
        for (int i=0; i<505; i++) {
            NSObject *objc = [[NSObject alloc] autorelease];
            NSLog(@"objc = %@",objc);
        }
        _objc_autoreleasePoolPrint();
      }

return 0;
}

打印结果如下:

objc[37983]: AUTORELEASE POOLS for thread 0x1000d1dc0
objc[37983]: 506 releases pending.
objc[37983]: [0x10280a000] ................ PAGE (full) (cold)
objc[37983]: [0x10280a038] ################ POOL 0x10280a038
objc[37983]: [0x10280a040] 0x101935750 NSObject
......
objc[37983]: [0x10280aff8] 0x10193ad50 NSObject
objc[37983]: [0x10280c000] ................ PAGE (hot)
objc[37983]: [0x10280c038] 0x10193ad60 NSObject
objc[37983]: ##############
Program ended with exit code: 0

再次增加505个对象打印结果如下:

extern void _objc_autoreleasePoolPrint(void);
int main(int argc, const char * argv[]) {
      @autoreleasepool {
        for (int i=0; i<505+505; i++) {
            NSObject *objc = [[NSObject alloc] autorelease];
            NSLog(@"objc = %@",objc);
        }
        _objc_autoreleasePoolPrint();
      }

return 0;

objc[38010]: ##############
objc[38010]: AUTORELEASE POOLS for thread 0x1000d1dc0
objc[38010]: 1011 releases pending.
objc[38010]: [0x10080f000] ................ PAGE (full) (cold)
objc[38010]: [0x10080f038] ################ POOL 0x10080f038
objc[38010]: [0x10080f040] 0x10064f240 NSObject
......
objc[38010]: [0x10080c000] ................ PAGE (full)
......
objc[38010]: [0x100810000] ................ PAGE (hot)
objc[38010]: [0x100810038] 0x100657af0 NSObject
objc[38010]: ##############
Program ended with exit code: 0

PAGE(hot) (cold) 表示当前分页为第一页,未满
PAGE(full) (cold) 表示当前分页为第一页,且已满
PAGE(full) 表示除第一页外的其它分页,且已满
PAGE(hot) 表示最后一页,未满

以上分析得出:autoreleasepool 添加对象存在分页,又因为第一个分页含有边界,所以除了第一页是504个对象外,其他分页都可添加505个对象。从源码里面也可以找到依据 PAGE_MIN_SIZE

2.5、通过源码分析 pop() 过程

找到方法 objc_autoreleasePoolPop

void
objc_autoreleasePoolPop(void *ctxt)
{
    AutoreleasePoolPage::pop(ctxt);
}

点击 pop() 方法进入, 查看 popPage() 方法

......
popPage(token, page, stop);

通过 stop 获取当前偏移位置对象,默认 stopbegin(),开始位置,即边界。 点击进入方法如下:

......
void releaseUntil(id *stop) 
    {
        while (this->next != stop) {
            // Restart from hotPage() every time, in case -release 
            // autoreleased more objects
            AutoreleasePoolPage *page = hotPage();
            // fixme I think this `while` can be `if`, but I can't prove it
            while (page->empty()) {
                page = page->parent;
                setHotPage(page);
            }

            page->unprotect();
            id obj = *--page->next;
            memset((void*)page->next, SCRIBBLE, sizeof(*page->next));
            page->protect();

            if (obj != POOL_BOUNDARY) {
                objc_release(obj);
            }
        }

this->next != stop 递归调用释放,POOL_BOUNDARY 边界,如果 next 没到边界,继续递归下一个,直到 next == begin();

3、autoreleasepool 嵌套

extern void _objc_autoreleasePoolPrint(void);
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSObject *objc = [[NSObject alloc] autorelease];
        NSLog(@"objc = %@",objc);
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            @autoreleasepool {
                NSObject *obj = [[NSObject alloc] autorelease];
                NSLog(@"obj = %@",obj);
                _objc_autoreleasePoolPrint();
            }
        });
        _objc_autoreleasePoolPrint();
}
    sleep(2);
    return 0;
}

控制台打印结果:

ObjcTest[38711:4664335] objc =
objc[38711]: ##############
objc[38711]: AUTORELEASE POOLS for thread 0x1000d2dc0
objc[38711]: 2 releases pending.
objc[38711]: [0x10180d000] ................ PAGE (hot) (cold)
objc[38711]: [0x10180d038] ################ POOL 0x10180d038
objc[38711]: [0x10180d040] 0x10071bd60 NSObject
objc[38711]: ##############
ObjcTest[38711:4664887] obj =
objc[38711]: ##############
objc[38711]: AUTORELEASE POOLS for thread 0x70000a00e000

objc[38711]: 3 releases pending.
objc[38711]: [0x103809000] ................ PAGE (hot) (cold)
objc[38711]: [0x103809038] ################ POOL 0x103809038
objc[38711]: [0x103809040] ################ POOL 0x103809040
objc[38711]: [0x103809048] 0x10134f5b0 NSObject
objc[38711]: ##############
Program ended with exit code: 0

结论:

  • @autoreleasepool 与线程是一对一关联的;
  • @autoreleasepool 嵌套,会创建一个page ,但是有两个哨兵(边界)(嵌套的子线程里面有两个花括号,即两个作用域空间,因此有两个边界)

你可能感兴趣的:(AutoreleasePool 自动释放池原理探索)