- 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.
推薦閱讀
- 企業數字化創新引擎:企業級PaaS平臺HZERO
- LibGDX Game Development Essentials
- 數據產品經理高效學習手冊:產品設計、技術常識與機器學習
- Spark快速大數據分析(第2版)
- Visual Studio 2015 Cookbook(Second Edition)
- 使用GitOps實現Kubernetes的持續部署:模式、流程及工具
- Spark大數據編程實用教程
- Lego Mindstorms EV3 Essentials
- 大數據技術入門
- SQL Server深入詳解
- 中文版Access 2007實例與操作
- Oracle 11g+ASP.NET數據庫系統開發案例教程
- 云工作時代:科技進化必將帶來的新工作方式
- Oracle 11g數據庫管理員指南
- Practical Convolutional Neural Networks