首页 > 解决方案 > 为什么没有代码的单视图 swift 应用程序会泄漏内存?

问题描述

我创建了一个带有 a 的单视图 swift 应用程序UILabel

当我尝试分析此应用程序时,它似乎正在泄漏内存。为什么会这样?

内存泄露截图

我敢肯定,我在某处缺少步骤或标志,但我无法找到它。

我没有在这个应用程序中编写任何自己的代码。这是一个使用 XCode iOS Single View App 模板创建的应用程序。有人可以帮我理解这里发生了什么吗?

编辑1:

这是代码:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
}

class ViewController: UIViewController {
}

还附上了主故事板的屏幕截图。 主情节提要截图

标签: iosswiftmemory-leaksprofiling

解决方案


如果您的应用程序中没有代码,则逻辑上不应该有任何内存泄漏。

清除后让我们谈谈工具(绿色检查和红色 x 标记),它们通常表示内存使用量的变化并不总是意味着存在内存泄漏,这可能是由许多原因引起的。

正如您所看到的,每个时间戳都会进行检查,因此它会比较这些时间戳之间的内存使用情况并确定是否存在任何潜在的内存泄漏,内存中的这些跳跃并不总是与内存合理的,但更像是发生了变化此时间戳期间的内存,因此您可以检查它,如果它确实是内存泄漏或只是由于某些功能或分配的依赖对象而导致正常内存上升,它是一个监控工具,而不是检测错误工具,即用于监视我们的应用程序内存分配等的变化。


推荐阅读