iOS - NSTimer循環(huán)引用的解決辦法
發(fā)生場(chǎng)景
在 Controller B 中有一個(gè) NSTimer
- @property (strong, nonatomic) NSTimer *timer;
你創(chuàng)建了它,并掛載到 main runloop
- self.timer = [NSTimer scheduledTimerWithTimeInterval:1
- target:self selector:@selector(timerAction:) userInfo:nil repeats:true];
然后退出 Controller B 的時(shí)候,忘記關(guān)掉 timer 了
Controller B 將不會(huì)釋放,B 與 timer 循環(huán)引用。因?yàn)閯?chuàng)建 timer 的時(shí)候把 self 直接寫進(jìn)去了。
方法一
既然不能直接傳 self,那傳 weakSelf 試試
- __weak typeof(self) weakSelf = self;
- self.timer = [NSTimer scheduledTimerWithTimeInterval:1
- target:weakSelf selector:@selector(timerAction:) userInfo:nil repeats:true];
測(cè)試結(jié)果還是發(fā)生了循環(huán)引用,B 沒有釋放,timer 對(duì) weakSelf 這個(gè)變量是強(qiáng)引用的,timer -> weakSelf -> B -> timer,三者之間形成循環(huán)引用。
方法二
設(shè)置一個(gè)包裝類,包著 Controller B 放進(jìn) timer 中,像這樣
我認(rèn)為 Controller B 有幾 MB 那么大,泄露了很浪費(fèi)內(nèi)存。
WeakWrap 只有幾百個(gè)字節(jié)那么小,泄露了也沒關(guān)系。
WeakWrap 中對(duì) Controller B 弱引用,WeakWrap 包著 Controller B,傳進(jìn) timer 中,就算忘記關(guān) timer,也只是泄露了 WeakWrap 和 timer。
理論上還是有內(nèi)存泄露,只不過比較少,如果一個(gè) Controller 是頻繁進(jìn)出的,進(jìn)出一次,丟失一個(gè),如果有幾十個(gè)泄露的 timer 掛在 main runloop 上會(huì)影響性能和流暢性,你想幾十個(gè) timer 一起 fire,又調(diào)用了 timer 事件響應(yīng)方法,開銷還是挺大的。
方法三
NSTimer 已知是會(huì)強(qiáng)引用參數(shù) target:self 的了,如果忘記關(guān) timer 的話,傳什么進(jìn)去都會(huì)被強(qiáng)引用。干脆實(shí)現(xiàn)一個(gè) timer 算了,timer 的功能就是定時(shí)調(diào)某個(gè)方法,NSTimer 的調(diào)用時(shí)間是不精確的!它掛在 runloop 上受線程切換,上一個(gè)事件執(zhí)行時(shí)間的影響。
利用 dispatch_asyn() 定時(shí)執(zhí)行函數(shù)??聪旅娲a。
- - (void)loop {
- [self doSomething];
- ......
- // 休息 time 秒,再調(diào) loop,實(shí)現(xiàn)定時(shí)調(diào)用
- [NSThread sleepForTimeInterval:time];
- dispatch_async(self.runQueue, ^{
- [weakSelf loop];
- });
- }
dispatch_async 中調(diào) loop 不會(huì)產(chǎn)生遞歸調(diào)用
dispatch_async 是在隊(duì)列中添加一個(gè)任務(wù),由 GCD 去回調(diào) [weakSelf loop]
這辦法解決了timer 不能釋放,掛在 runloop 不能移除的問題。
利用這方法,我寫了個(gè)不會(huì)發(fā)生循環(huán)引用的 timer,controller 釋放,timer 也自動(dòng)停止釋放,甚至 timer 的 block 里面可以直接寫 self,也不會(huì)循環(huán)引用。github下載地址
方法四
NSTimer 我之前沒遇到過循環(huán)引用的問題,因?yàn)槲乙恢倍际桥鋵?duì)使用,在 viewWillAppear 開啟,在 viewWillDisappear 關(guān)閉,不關(guān)閉的話那么多 timer 掛載在 runloop 上感覺挺影響性能和流暢性的,就像管理內(nèi)存一樣,申請(qǐng)和釋放配對(duì)使用,就不會(huì)泄露了,誰申請(qǐng)誰釋放的原則。但是很大的團(tuán)隊(duì)的話,別人可能會(huì)寫錯(cuò),造成泄露,可以從技術(shù)上,團(tuán)隊(duì)編程規(guī)范上解決他。
比如定一些規(guī)范,Controller 退出一定要成功銷毀,不能泄露內(nèi)存。Block 里不能寫 self 等等。