简体   繁体   English

iOS Memory 泄漏,memory 图形调试器显示没有泄漏,但并行 Xcode-Instruments-leaks 显示泄漏

[英]iOS Memory leaks , memory graph debugger shows no leaks but in parallel Xcode-Instruments-leaks shows leaks

在此处输入图像描述 in my Project When I started to look for leaks in memory graph debugger, I found few and fixed them, and now with memory graph no leaks is found.在我的项目中当我开始在 memory 图形调试器中查找泄漏时,我发现很少并修复了它们,现在使用 memory 图形没有发现泄漏。 the problem with Instruments->leaks, sometimes it show leaks and sometimes not, the leaks appears immediately from the beginning as described in the photo, which I barely understand what causes the leaks. Instruments->leaks 的问题,有时它显示泄漏,有时不显示,泄漏从一开始就立即出现,如照片中所述,我几乎不明白导致泄漏的原因。 if the memory graph shows no leaks can I trust it?如果 memory 图表显示没有泄漏,我可以相信它吗? or there is a kind of a leaks which is not caught by memory graph.或者存在一种未被 memory 图捕获的泄漏。 The code how do I initialize the mainViewController:我如何初始化 mainViewController 的代码:

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    self.window = UIWindow()
    
    let controller = MainViewController()
    let navigationController = UINavigationController(rootViewController: controller)
    let rootViewController = navigationController
    self.window?.rootViewController = rootViewController
    self.window?.makeKeyAndVisible()
           
    return true
        
}

In the 2013 video, Fixing Memory Issues ), Apple made a distinction between “leaks”, “abandoned memory”, and “cached memory”, discussed below.在 2013 年的视频Fixing Memory 问题中,Apple 对“泄漏”、“废弃内存”和“缓存内存”进行了区分,如下所述。 For more contemporary discussions, see WWDC 2021 video Detect and diagnose memory issues , WWDC 2019 video Getting Started with Instruments , and WWDC 2018 video iOS Memory Deep Dive .有关更多当代讨论,请参阅 WWDC 2021 视频检测和诊断 memory 问题、WWDC 2019 视频开始使用仪器和 WWDC 2018 视频iOS Z4789F3B8D643B1BEF1FA8DZ643B3AEF198

  • Leaked memory is that which can't be accessed again, memory for which there are no references left, ie, memory that has been malloc 'ed but never free 'd. Leaked memory is that which can't be accessed again, memory for which there are no references left, ie, memory that has been malloc 'ed but never free 'd.

  • Abandoned memory consists of memory that which does have references, but which won't be accessed again.废弃的 memory 由 memory 组成,其中确实有引用,但不会再次访问。

  • Cached memory is that which might not be used again, held in memory for fast retrieval in case the app needs it again.缓存的 memory可能不会再次使用,保存在 memory 中,以便在应用再次需要时快速检索。

The “debug memory graph” excels at finding and visualizing issues arising from strong reference cycles. “调试 memory 图”擅长发现和可视化由强参考周期引起的问题。 The “Leaks” instrument will not identify these issues. “泄漏”工具不会识别这些问题。 As strong reference cycles and the like, are more prevalent in Swift code, often the “Debug Memory Graph” is a more fruitful first line of defense.由于强参考周期等在 Swift 代码中更为普遍,因此“调试 Memory 图表”通常是更有成效的第一道防线。

When debugging memory issues, we worry less about the memory usage between the first and second iterations as we cycle through the app, but rather focus on subsequent iterations.在调试 memory 问题时,我们较少担心第一次和第二次迭代之间的 memory 使用情况,因为我们循环浏览应用程序,而是关注后续迭代。

Anyway, the “Leaks” tool and the “debug memory graph” were focusing on different problems and would generate different results.无论如何,“Leaks”工具和“debug memory graph”关注的是不同的问题,会产生不同的结果。 Leaks does not find strong reference cycles.泄漏没有找到强参考周期。 At the same time, the “debug memory graph” feature has gotten much better at finding traditional leaks.同时,“debug memory graph”功能在发现传统漏洞方面也有了很大的进步。

FWIW, in Swift, strong reference cycles are far more common than traditional malloc -but-no- free leaks. FWIW,在 Swift 中,强参考周期比传统的malloc更常见,但free泄漏。 Your Swift code is unlikely to have traditional leaks unless you start delving into manual allocation of buffers, unmanaged Core Foundation API, etc.您的 Swift 代码不太可能存在传统泄漏,除非您开始研究手动分配缓冲区、非托管核心基础 API 等。

And if you are seeing memory growth in your app, before you worry about leaks, make sure it's not the third memory issue identified in the aforementioned video, namely, cached memory, that which might not be used again, but will be automatically reclaimed when the device runs low on memory.如果您在您的应用程序中看到 memory 增长,在您担心泄漏之前,请确保它不是上述视频中确定的第三个 memory 问题,即缓存的 ZCD69B4957F06CD818DZZ 被自动回收,可能会在不再使用时自动回收,但9该设备在 memory 上运行不足。


As an aside, occasionally there are reported leaks buried in the OS or frameworks.顺便说一句,偶尔会报告隐藏在操作系统或框架中的泄漏。 If (a) you do not see your target referenced in the stack traces;如果 (a) 您没有看到堆栈跟踪中引用了您的目标; or (b) the leaks are inconsequental, then I might suggest not worrying too much about them at all.或(b)泄漏是无关紧要的,那么我可能建议不要太担心它们。 In your case, we're talking about 384 bytes, not something I'd worry too much about.在您的情况下,我们谈论的是 384 字节,我不会太担心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM