“Yeah It’s on. ”
前言
学无止境
自动释放池
主要结构是__AtAutoreleasePool
、__AtAutoreleasePoolPage
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class AutoreleasePoolPage
{
// EMPTY_POOL_PLACEHOLDER is stored in TLS when exactly one pool is
// pushed and it has never contained any objects. This saves memory
// when the top level (i.e. libdispatch) pushes and pops pools but
// never uses them.
# define EMPTY_POOL_PLACEHOLDER ((id*)1)
# define POOL_BOUNDARY nil
static pthread_key_t const key = AUTORELEASE_POOL_KEY;
static uint8_t const SCRIBBLE = 0xA3; // 0xA3A3A3A3 after releasing
static size_t const SIZE =
#if PROTECT_AUTORELEASEPOOL
PAGE_MAX_SIZE; // must be multiple of vm page size
#else
PAGE_MAX_SIZE; // size and alignment, power of 2
#endif
static size_t const COUNT = SIZE / sizeof(id);
magic_t const magic;
id *next;
pthread_t const thread;
AutoreleasePoolPage * const parent;
AutoreleasePoolPage *child;
uint32_t const depth;
uint32_t hiwat;
}
1
2
3
4
5
6
7
8
9
struct __AtAutoreleasePool {
__AtAutoreleasePool() {
atautoreleasepoolobj = objc_autoreleasePoolPush();
}
~__AtAutoreleasePool() {
objc_autoreleasePoolPop(atautoreleasepoolobj);
}
void * atautoreleasepoolobj;
};
{
__AtAutoreleasePool __autoreleasepool;
Person *person = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init")), sel_registerName("autorelease"));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void * objc_autoreleasePoolPush(void)
{
return AutoreleasePoolPage::push();
}
void objc_autoreleasePoolPop(void *ctxt)
{
AutoreleasePoolPage::pop(ctxt);
}
void * _objc_autoreleasePoolPush(void)
{
return objc_autoreleasePoolPush();
}
void _objc_autoreleasePoolPop(void *ctxt)
{
objc_autoreleasePoolPop(ctxt);
}
// 可用此函数输入释放池,在外面 extern 这个函数,再直接调用
void_objc_autoreleasePoolPrint(void)
{
AutoreleasePoolPage::printAll();
}
释放时机
main
函数中的@autoreleasepool
会在程序运行过程中一直存在,所以变量的自动释放池不是在main
iOS 在主线程的Runloop中创建了两个监听
- 第1个监听了
kCFRunLoopEntry
事件,会调用objc_autoreleasePoolPush()
- 第2个监听了
kCFRunLoopBeforeWaiting | kCFRunLoopBeforeExit
- 当触发
kCFRunLoopBeforeWaiting
时,会调用objc_autoreleasePoolPop()
和objc_autoreleasePoolPush()
- 当触发
kCFRunLoopBeforeExit
时,会调用objc_autoreleasePoolPop()
- 当触发