iOS. Autorelease源碼

前言

AutoreleasePoolPage 結(jié)構(gòu)

AutoreleasePoolPage 是一個C++實現(xiàn)的類, 實現(xiàn)雙向鏈表。

class AutoreleasePoolPage 
{

#if PROTECT_AUTORELEASEPOOL
        PAGE_MAX_SIZE;  // must be multiple of vm page size
#else
//#define I386_PGBYTES        4096        /* bytes per 80386 page */
//#define PAGE_MAX_SIZE           PAGE_SIZE
        PAGE_MAX_SIZE;  // size and alignment, power of 2
#endif

    static size_t const COUNT = SIZE / sizeof(id);
//magic用來校驗AutoreleasePoolPage結(jié)構(gòu)是否完整
    magic_t const magic;
//next指向第一個可用的地址
    id *next;
//thread指向當(dāng)前的線程;
    pthread_t const thread;
//parent指向父類
    AutoreleasePoolPage * const parent;
//child指向子類
    AutoreleasePoolPage *child;
    uint32_t const depth;
    uint32_t hiwat;
......
}

AutoreleasePoolPage每個對象會開辟4096字節(jié)內(nèi)存(也就是虛擬內(nèi)存一頁的大小),除了上面的實例變量所占空間,剩下的空間全部用來儲存autorelease對象的地址

autoreleasepool的創(chuàng)建

void *objc_autoreleasePoolPush(void)
{
    if (UseGC) return nil;
    return AutoreleasePoolPage::push();
}

static inline void *push() 
    {
        id *dest;
        if (DebugPoolAllocation) {
            // Each autorelease pool starts on a new pool page.
            dest = autoreleaseNewPage(POOL_BOUNDARY);
        } else {
            dest = autoreleaseFast(POOL_BOUNDARY);
        }
        return dest;
    }

static inline id *autoreleaseFast(id obj)
    {
        AutoreleasePoolPage *page = hotPage();
        if (page && !page->full()) {
            return page->add(obj);
        } else if (page) {
            return autoreleaseFullPage(obj, page);
        } else {
            return autoreleaseNoPage(obj);
        }
    }

這個方法就是AutoreleasePoolPage具體初始化的地方,看實現(xiàn)知道有三種情況.

  • hotPage存在并且容量沒有滿,直接添加對象
  • hotPage存在但是容量已經(jīng)滿了,調(diào)用autoreleaseFullPage方法,初始化一個AutoreleasePoolPage并把page傳入,并標(biāo)記為hotPage;
  • hotPage不存在,調(diào)用autoreleaseNoPage創(chuàng)建一個AutoreleasePoolPage,并標(biāo)記為hotePage,并且添加一個POOL_SENTINEL(哨兵對象)

關(guān)鍵inline,其實就是內(nèi)聯(lián)函數(shù),可以提供執(zhí)行速度

對象如何加入到autoreleasepool 中的

  • 當(dāng)對象調(diào)用【object autorelease】的方法的時候就會加到autoreleasepool中。
+(id) autorelease {
    return self;
}

// Replaced by ObjectAlloc
- (id)autorelease {
    return ((id)self)->rootAutorelease();
}
inline id 
objc_object::rootAutorelease()
{
    if (isTaggedPointer()) return (id)this;
    if (prepareOptimizedReturn(ReturnAtPlus1)) return (id)this;

    return rootAutorelease2();
}
__attribute__((noinline,used))
id 
objc_object::rootAutorelease2()
{
    assert(!isTaggedPointer());
    return AutoreleasePoolPage::autorelease((id)this);
}
public:
    static inline id autorelease(id obj)
    {
        assert(obj);
        assert(!obj->isTaggedPointer());
        id *dest __unused = autoreleaseFast(obj);
        assert(!dest  ||  dest == EMPTY_POOL_PLACEHOLDER  ||  *dest == obj);
        return obj;
    }
  • autoreleaseFast(obj); 上面有。 autoreleasepool push時的主要方法。
  • 只有push操作會產(chǎn)生POOL_SENTINEL 哨兵;
  • POOL_SENTINEL的個數(shù)就是autoreleasepool的個數(shù),實際開發(fā)中會有嵌套使用的情況

autoreleasepool 中的Pop

    static inline void pop(void *token)   // token指針指向棧頂?shù)牡刂?    {
        AutoreleasePoolPage *page = pageForPointer(token);   // 通過棧頂?shù)牡刂氛业綄?yīng)的page
        id *stop = (id *)token;
      
        if (PrintPoolHiwat) printHiwat();   // 記錄最高水位標(biāo)記

        page->releaseUntil(stop);   // 從棧頂開始操作出棧,并向棧中的對象發(fā)送release消息,直到遇到第一個哨兵對象

        // 刪除空掉的節(jié)點
       if (page->child) {
        if (page->lessThanHalfFull()) {
            page->child->kill();
        } else if (page->child->child) {
            page->child->child->kill();
        }
      }
        
    }

該靜態(tài)方法總共做了三件事情:
使用pageForPointer 獲取當(dāng)前 token(哨兵對象), 所在的 AutoreleasePoolPage
調(diào)用 releaseUntil方法釋放棧中的對象,直到 stop,
調(diào)用 childkill 方法

AutoreleasePool創(chuàng)建和釋放

  • App啟動后,蘋果在主線程 RunLoop 里注冊了兩個 Observer,其回調(diào)都是 _wrapRunLoopWithAutoreleasePoolHandler()。
  • 第一個 Observer 監(jiān)視的事件是 Entry(即將進入Loop),其回調(diào)內(nèi)會調(diào)用 _objc_autoreleasePoolPush() 創(chuàng)建自動釋放池。其 order 是-2147483647,優(yōu)先級最高,保證創(chuàng)建釋放池發(fā)生在其他所有回調(diào)之前。
  • 第二個 Observer 監(jiān)視了兩個事件: BeforeWaiting(準(zhǔn)備進入休眠) 時調(diào)用_objc_autoreleasePoolPop() 和 _objc_autoreleasePoolPush() 釋放舊的池并創(chuàng)建新池;Exit(即將退出Loop) 時調(diào)用 _objc_autoreleasePoolPop() 來釋放自動釋放池。這個 Observer 的 order 是 2147483647,優(yōu)先級最低,保證其釋放池子發(fā)生在其他所有回調(diào)之后。
  • 在主線程執(zhí)行的代碼,通常是寫在諸如事件回調(diào)、Timer回調(diào)內(nèi)的。這些回調(diào)會被 RunLoop 創(chuàng)建好的 AutoreleasePool 環(huán)繞著,所以不會出現(xiàn)內(nèi)存泄漏,開發(fā)者也不必顯示創(chuàng)建 Pool 了。

也就是說AutoreleasePool創(chuàng)建是在一個RunLoop事件開始之前(push),AutoreleasePool釋放是在一個RunLoop事件即將結(jié)束之前(pop)。

AutoreleasePool里的Autorelease對象的加入是在RunLoop事件中,AutoreleasePool里的Autorelease對象的釋放是在AutoreleasePool釋放時。

/***********************************************************************
* call_load_methods
* Call all pending class and category +load methods.
* Class +load methods are called superclass-first. 
* Category +load methods are not called until after the parent class's +load.
* 
* This method must be RE-ENTRANT, because a +load could trigger 
* more image mapping. In addition, the superclass-first ordering 
* must be preserved in the face of re-entrant calls. Therefore, 
* only the OUTERMOST call of this function will do anything, and 
* that call will handle all loadable classes, even those generated 
* while it was running.
*
* The sequence below preserves +load ordering in the face of 
* image loading during a +load, and make sure that no 
* +load method is forgotten because it was added during 
* a +load call.
* Sequence:
* 1. Repeatedly call class +loads until there aren't any more
* 2. Call category +loads ONCE.
* 3. Run more +loads if:
*    (a) there are more classes to load, OR
*    (b) there are some potential category +loads that have 
*        still never been attempted.
* Category +loads are only run once to ensure "parent class first" 
* ordering, even if a category +load triggers a new loadable class 
* and a new loadable category attached to that class. 
*
* Locking: loadMethodLock must be held by the caller 
*   All other locks must not be held.
**********************************************************************/
void call_load_methods(void)
{
    static bool loading = NO;
    bool more_categories;

    loadMethodLock.assertLocked();

    // Re-entrant calls do nothing; the outermost call will finish the job.
    if (loading) return;
    loading = YES;

    void *pool = objc_autoreleasePoolPush();

    do {
        // 1. Repeatedly call class +loads until there aren't any more
        while (loadable_classes_used > 0) {
            call_class_loads();
        }

        // 2. Call category +loads ONCE
        more_categories = call_category_loads();

        // 3. Run more +loads if there are classes OR more untried categories
    } while (loadable_classes_used > 0  ||  more_categories);

    objc_autoreleasePoolPop(pool);

    loading = NO;
}

AutoreleasePool對象什么時候釋放?

在沒有手加Autorelease Pool的情況下,Autorelease對象是在當(dāng)前的runloop迭代結(jié)束時釋放的,而它能夠釋放的原因是系統(tǒng)在每個runloop迭代中都加入了自動釋放池PushPop.

子線程Autorelease

在子線程你創(chuàng)建了 Pool 的話,產(chǎn)生的 Autorelease 對象就會交給 pool 去管理。如果你沒有創(chuàng)建 Pool ,但是產(chǎn)生了 Autorelease 對象,就會調(diào)用 autoreleaseNoPage 方法。在這個方法中,會自動幫你創(chuàng)建一個 hotpage(hotPage 可以理解為當(dāng)前正在使用的 AutoreleasePoolPage,如果你還是不理解,可以先看看 Autoreleasepool 的源代碼,再來看這個問題 )

什么對象自動加入到 autoreleasepool中

雖然在程序入口,已經(jīng)幫我們加上了 autoreleasepool,但是并不是說大括號內(nèi)的所有
對象都會交給autoreleasepool來處理

第一種:
當(dāng)使用alloc/new/copy/mutableCopy開始的方法進行初始化時,會生成并持有對象(也就是不需要pool管理,系統(tǒng)會自動的幫他在合適位置release)

例如: NSObject *stu = [[NSObject alloc] init]; 

那么對于其他情況,例如

id obj = [NSMutableArray array];

這種情況會自動將返回值的對象注冊到autorealeasepool,代碼等效于:

@autorealsepool{
    id __autorealeasing obj = [NSMutableArray array];
}

編譯器會通過objc_autoreleaseReturnValue和objc_retainAutoreleasedReturnValue和個兩個函數(shù)不將對象注冊到autoreleasepool里而直接傳遞,所以說這種情況并沒有把對象添加到autoreleasepool? [NSMutableArray array];返回的對象后,如果外部是強引用, 則編譯器優(yōu)化了, 并不會添加進入pool中!

第二種
__weak修飾符只持有對象的弱引用,而在訪問引用對象的過程中,該對象可能被廢棄。那么如果把對象注冊到autorealeasepool中,那么在@autorealeasepool塊結(jié)束之前都能確保對象的存在。

最新的情況是weak修飾的對象不會再被加入到Pool中了,具體可參考:https://stackoverflow.com/questions/40993809/why-weak-object-will-be-added-to-autorelease-pool

id __weak obj1 = obj0;
NSLog(@"class=%@",[obj1 class]);

對應(yīng)的模擬源碼為

id __weak obj1 = obj0;
id __autorealeasing tmp = obj1;
NSLog(@"class=%@",[tmp class]);

第三種
id的指針或?qū)ο蟮闹羔樤跊]有顯式指定時會被附加上__autorealeasing修飾符.

    + (nullable instancetype)stringWithContentsOfURL:(NSURL *)url
                                        encoding:(NSStringEncoding)enc
                                           error:(NSError **)error;

等價于

   NSString *str = [NSString stringWithContentsOfURL:
                                         encoding:
                                            error:<#(NSError * _Nullable __autoreleasing * _Nullable)#>]

參考:
自動釋放池的前世今生 ---- 深入解析 autoreleasepool-InfoQ
oc篇-深入理解@autoreleasepool
你不知道的TaggedPointer
AutoreleasePool 的總結(jié)
深入理解 AutoreleasePool(iOS)
自動釋放池的前世今生 ---- 深入解析 Autoreleasepool
引用計數(shù)帶來的一次討論
does NSThread create autoreleasepool automatically now?does NSThread create autoreleasepool automaticly now?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容