- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 174字
- 2021-07-02 14:45:07
A simple leak
Now, let's write a function that creates an instance, and does nothing else:
func createLeak() {
let leak = MemoryLeak()
}
createLeak()
When we create the new MemoryLeak instance, a self reference is set, and the retain count will be two for the duration of the function call. When the function returns, the local leak variable is not referenced anymore, so the retain count is still one, and, if there are no references to this instance in the program, we will have a leak.
In a garbage collected language, this would never happen, as unreachable objects are deallocated automatically.
Now, let's use the memory graph tool to investigate this issue:

Using this tool, at a glance, we can see the following:
- Leaks are denoted with the purple issue icon
- Upon clicking on the leaking object, we can see the issue
- The dark arrows in the memory graph are for strong references
In one go, it is now really easy for you to identify what might be wrong with your programs.
推薦閱讀
- 大數(shù)據(jù)技術(shù)基礎(chǔ)
- 大規(guī)模數(shù)據(jù)分析和建模:基于Spark與R
- 計算機(jī)信息技術(shù)基礎(chǔ)實驗與習(xí)題
- 揭秘云計算與大數(shù)據(jù)
- 數(shù)據(jù)庫應(yīng)用基礎(chǔ)教程(Visual FoxPro 9.0)
- Neural Network Programming with TensorFlow
- Hadoop 3.x大數(shù)據(jù)開發(fā)實戰(zhàn)
- 數(shù)據(jù)庫技術(shù)實用教程
- Oracle 12c云數(shù)據(jù)庫備份與恢復(fù)技術(shù)
- LabVIEW 完全自學(xué)手冊
- Flutter Projects
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第3版)
- 計算機(jī)組裝與維護(hù)(微課版)
- Oracle高性能SQL引擎剖析:SQL優(yōu)化與調(diào)優(yōu)機(jī)制詳解
- 數(shù)據(jù)庫原理與設(shè)計實驗教程(MySQL版)