Advanced Debug Swift Memory Leak

leaking-pipes.jpg

In 2016 WWDC Apple introduced two new Visual Debugging tools on Xcode in the Session 410. Here I want share a story how the Memory Graph Debugging make life easier on debugging memory leak issue.

0. Let’s get start it

  1. Edit the scheme, and enable Debug Memory Graph


    memory debug scheme.png
  2. Open your app and play with it

  3. Click the Debug Memory Graph button on Xcode, Then the memory graph will show up.

memory graph button.png

1. A story

In the real world, before we jump into debugging tools, when we get started to meet memory leaks problem.

One day, user complained that the app crash on getting into detail page sometimes. The app got bunch of crash report on Fabric as well.

We checked the log is like this below, and

Fatal Exception: NSMallocException
0 CoreFoundation 0x18503ed8c __exceptionPreprocess
1 libobjc.A.dylib 0x1841f85ec objc_exception_throw
2 Foundation 0x185a6ad7c _NSErrnoMessage
3 CoreFoundation 0x184f9dfd8 __CFSafelyReallocate
4 CoreFoundation 0x184fb99f8 __CFDataGrow
5 CoreFoundation 0x184f328b8 CFDataSetLength
6 CoreGraphics 0x186a237e0 CGDataProviderCopyData
7 AlamofireImage 0x103ed5e98 (Missing)
8 AlamofireImage 0x103ed1004 (Missing)
9 AlamofireImage 0x103ecdeb0 (Missing)
10 AlamofireImage 0x103eb26cc (Missing)
11 Alamofire 0x103cb7688 (Missing)
12 Alamofire 0x103c9a4c0 (Missing)
13 Foundation 0x185a66e88 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__
......

hmm… It seems some wrong on AlamofireImage.

Hand-on a minute… NSMallocException would be a good indicator for memory issue.

We reproduced the crash with 20 times jump into detail screen from list screen. It looks fixable.

I created a demo here to demonstrate the memory issue:

https://github.com/lvjian700/swift-memory-leaks-demo

3. Checking the memory issue

Run the app from Xcode and play with Memory page. The you can see the memory keep growth up and never going down.

memory-leaks.gif

Aha! Memory leaks !!!

4. Debugging memory leaks

  1. Edit project scheme, enable Debug Memory Graph

  2. Re-run the app and play with it

  3. Open the Debug Memory Graph on Xcode

memory graph.png

Since Swift is ARC language. The major memory leaks issue is caused by Cycle reference and Cycle capture. You can see the two memory leaks on screenshot above.

5. Fixing the memory leaks

Debug Memory Graph provide a very powerful way to identify which line of code has memory leak. See the picture below for using it:

  1. Enable the filter for leak warning with purple icon in the navigator view

  2. Check the object on memory graph

  3. Check the code on inspector view, then jump into the code with click.

memory-leaks-fixing.gif

6. Try yourself

If you eager to try the Debug Memory Graph tool. Here is the demo, you can fix the memory issue with the tool.

https://github.com/lvjian700/swift-memory-leaks-demo

Have a fun !

The article was post here: https://medium.com/@lvjian700/advanced-debug-swift-memory-leak-eb134b061158
Here is a version for people who can not access Medium.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市坦敌,隨后出現(xiàn)的幾起案子坦胶,更是在濱河造成了極大的恐慌魔熏,老刑警劉巖逃片,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹄胰,死亡現(xiàn)場離奇詭異斩例,居然都是意外死亡窄赋,警方通過查閱死者的電腦和手機赡勘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進店門嫂便,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人闸与,你說我怎么就攤上這事毙替。” “怎么了践樱?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵厂画,是天一觀的道長。 經常有香客問我拷邢,道長袱院,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任瞭稼,我火速辦了婚禮忽洛,結果婚禮上,老公的妹妹穿的比我還像新娘环肘。我一直安慰自己欲虚,他們只是感情好,可當我...
    茶點故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布悔雹。 她就那樣靜靜地躺著复哆,像睡著了一般。 火紅的嫁衣襯著肌膚如雪腌零。 梳的紋絲不亂的頭發(fā)上梯找,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天,我揣著相機與錄音益涧,去河邊找鬼锈锤。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的牙咏。 我是一名探鬼主播臼隔,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼妄壶!你這毒婦竟也來了摔握?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤丁寄,失蹤者是張志新(化名)和其女友劉穎氨淌,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體伊磺,經...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡盛正,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了屑埋。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片豪筝。...
    茶點故事閱讀 40,742評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖摘能,靈堂內的尸體忽然破棺而出续崖,到底是詐尸還是另有隱情,我是刑警寧澤团搞,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布严望,位于F島的核電站,受9級特大地震影響逻恐,放射性物質發(fā)生泄漏像吻。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一复隆、第九天 我趴在偏房一處隱蔽的房頂上張望拨匆。 院中可真熱鬧,春花似錦昏名、人聲如沸涮雷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至样刷,卻和暖如春仑扑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背置鼻。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工镇饮, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人箕母。 一個月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓储藐,卻偏偏與公主長得像俱济,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子钙勃,可洞房花燭夜當晚...
    茶點故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內容