自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS - NSTimer循環(huán)引用的解決辦法

移動(dòng)開發(fā) iOS
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ī)范上解決他。

發(fā)生場(chǎng)景

在 Controller B 中有一個(gè) NSTimer

  1. @property (strong, nonatomic) NSTimer *timer; 

你創(chuàng)建了它,并掛載到 main runloop

  1. self.timer = [NSTimer scheduledTimerWithTimeInterval:1  
  2. 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 試試

  1. __weak typeof(self) weakSelf = self; 
  2.   
  3. self.timer = [NSTimer scheduledTimerWithTimeInterval:1  
  4. 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。

  1. - (void)loop { 
  2.     [self doSomething]; 
  3.     ...... 
  4.     // 休息 time 秒,再調(diào) loop,實(shí)現(xiàn)定時(shí)調(diào)用 
  5.     [NSThread sleepForTimeInterval:time]; 
  6.     dispatch_async(self.runQueue, ^{ 
  7.         [weakSelf loop]; 
  8.     });     

 

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 等等。 

責(zé)任編輯:龐桂玉 來源: CocoaChina
相關(guān)推薦

2015-07-10 10:27:37

iOS NSTimer

2009-06-03 16:41:21

Eclipse亂碼Eclipse

2011-03-04 13:07:47

Filezilla

2011-01-19 17:54:48

2009-05-31 09:07:35

Oracle鎖定

2011-06-17 11:10:51

Qt 中文 輸出

2009-12-07 18:38:16

WCF異常

2020-12-25 19:16:03

C++編程語言

2010-01-15 09:38:08

磁盤被寫保護(hù)解決辦法

2009-02-18 09:30:10

AJAX跨域XML

2020-05-09 10:59:33

git cloneLinux文件

2009-12-03 17:36:02

PHP Date()出

2009-11-30 10:55:16

PHP頁面亂碼

2009-05-26 14:34:55

Ubuntuwubi臨時(shí)

2011-04-21 16:42:40

傳真機(jī)

2012-03-14 10:58:27

Java

2012-07-31 16:06:28

Linux內(nèi)核編譯

2010-04-19 14:57:16

Oracle收縮表分區(qū)

2012-12-12 15:19:32

云安全

2009-08-19 22:36:08

Ubuntu安裝VMw
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)