gpt4 book ai didi

ios - 如何调查 ios 中的内存泄漏?

转载 作者:搜寻专家 更新时间:2023-10-31 22:24:19 24 4
gpt4 key购买 nike

我创建了一个简单的流程来测试 iOS 应用程序中的内存。我在导航堆栈中有两个 View Controller 。我在第一个 View Controller 中显示一个警报,以允许用户移动到下一个。以下是我正在使用的代码。

class ViewController: UIViewController {

@IBOutlet weak var labelInfo: UILabel!

override func viewDidLoad() {
super.viewDidLoad()

}

@IBAction func showNextScreen(_ sender: Any) {

let alert = UIAlertController(title: "Alert", message: "Go to next screen?", preferredStyle: .alert)

alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { [unowned self] (action) in

self.performSegue(withIdentifier: "showNextScreen", sender: nil)

}))

alert.addAction(UIAlertAction(title: "No", style: .default, handler: { [unowned self] (action) in

self.labelInfo.text = "You did not move to next screen"
alert.dismiss(animated: true, completion: nil)

}))

self.present(alert, animated: true, completion: nil)

}

}

阅读了提到不应该有强引用循环的资源后,我在代码中使用了unowned self。检查仪器中的泄漏,我得到了下图。

enter image description here

图表显示没有泄漏,如绿色刻度线所示。然而,当我在两个 View Controller 之间来回移动时,内存使用图正在增加。什么可能导致内存使用量增加(第一个问题)?

接下来,我还检查了将 unowned self 替换为 self 的效果。我得到的结果和以前一样。这表明没有强引用。引用这个例子(第二个问题),我们如何判断是否存在strong retain cycle?

enter image description here

最佳答案

转到 Edit Scheme -> Run -> Diagnostics现在像屏幕截图一样勾选 Malloc stackMemory debug config这样做之后,重建并运行 .现在打开 Debug memory graph,您将在发生内存泄漏的类列表中看到紫色图标。看这个截图Memory Debug Graph内存泄漏的示例 repo 是 Swift Memory Leak Demo这是带有真实示例的内存泄漏 View 以及附加的 repo Real Example picture with memory leak 您可以对您的项目执行相同的过程来识别内存泄漏

关于ios - 如何调查 ios 中的内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54978977/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com