AutoreleasePool

如何調試

示例代碼:

// 1. 聲明方法
extern void _objc_autoreleasePoolPrint(void);

// 2. 打印信息
_objc_autoreleasePoolPrint();

案例

由於使用autoreleasepool需要autorelease方法
需要將Build Settings -> Objective-C Automatic Reference Counting設置爲NO

示例代碼1:

@autoreleasepool {
    NSObject *object_1 = [[[NSObject alloc] init] autorelease];
    NSObject *object_2 = [[[NSObject alloc] init] autorelease];
    NSObject *object_3 = [[[NSObject alloc] init] autorelease];
    NSObject *object_4 = [[[NSObject alloc] init] autorelease];
    NSObject *object_5 = [[[NSObject alloc] init] autorelease];

    _objc_autoreleasePoolPrint();
}

運行結果:

##############
AUTORELEASE POOLS for thread 0x1000cfd40
6 releases pending.
[0x101003000]  ................  PAGE  (hot) (cold)
[0x101003038]  ################  POOL 0x101003038
[0x101003040]       0x10065f620  NSObject
[0x101003048]       0x10065bf80  NSObject
[0x101003050]       0x10065e5b0  NSObject
[0x101003058]       0x10065a850  NSObject
[0x101003060]       0x10065b2c0  NSObject
##############

示例代碼2:

@autoreleasepool {
    NSObject *object_1 = [[[NSObject alloc] init] autorelease];
    NSObject *object_2 = [[[NSObject alloc] init] autorelease];
    _objc_autoreleasePoolPrint();
    @autoreleasepool {
        NSObject *object_3 = [[[NSObject alloc] init] autorelease];
        NSObject *object_4 = [[[NSObject alloc] init] autorelease];
        _objc_autoreleasePoolPrint();
    }
    NSObject *object_5 = [[[NSObject alloc] init] autorelease];
    _objc_autoreleasePoolPrint();
}

運行結果:

##############
AUTORELEASE POOLS for thread 0x1000cfd40
3 releases pending.
[0x100806000]  ................  PAGE  (hot) (cold)
[0x100806038]  ################  POOL 0x100806038
[0x100806040]       0x1005b6750  NSObject
[0x100806048]       0x1005b4a00  NSObject
##############
##############
AUTORELEASE POOLS for thread 0x1000cfd40
6 releases pending.
[0x100806000]  ................  PAGE  (hot) (cold)
[0x100806038]  ################  POOL 0x100806038
[0x100806040]       0x1005b6750  NSObject
[0x100806048]       0x1005b4a00  NSObject
[0x100806050]  ################  POOL 0x100806050
[0x100806058]       0x1005b4f00  NSObject
[0x100806060]       0x1005b2160  NSObject
##############
##############
AUTORELEASE POOLS for thread 0x1000cfd40
4 releases pending.
[0x100806000]  ................  PAGE  (hot) (cold)
[0x100806038]  ################  POOL 0x100806038
[0x100806040]       0x1005b6750  NSObject
[0x100806048]       0x1005b4a00  NSObject
[0x100806050]       0x1007000b0  NSObject
##############

示例代碼3:

@autoreleasepool {
    for (int i = 0; i < 800; ++i) {
        NSObject *object = [[[NSObject alloc] init] autorelease];
    }
    _objc_autoreleasePoolPrint();
}

運行結果:

##############
AUTORELEASE POOLS for thread 0x1000cfd40
801 releases pending.
[0x100804000]  ................  PAGE (full)  (cold)
[0x100804038]  ################  POOL 0x100804038
[0x100804040]       0x10053be60  NSObject
[0x100804048]       0x1005390c0  NSObject
[0x100804050]       0x100538d80  NSObject
...
[0x100804fe8]       0x100543b70  NSObject
[0x100804ff0]       0x100543b80  NSObject
[0x100804ff8]       0x100543b90  NSObject
[0x10080f000]  ................  PAGE  (hot) 
[0x10080f038]       0x100543ba0  NSObject
[0x10080f040]       0x100543bb0  NSObject
[0x10080f048]       0x100543bc0  NSObject
...
[0x10080f960]       0x100544df0  NSObject
[0x10080f968]       0x100544e00  NSObject
[0x10080f970]       0x100544e10  NSObject
##############
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章