ios - 适用于 iOS 的 Ionic 和 Cordova 中的奇怪内存泄漏

标签 ios angularjs cordova memory-leaks ionic-framework

我的 ionic 和 cordova 应用程序出现奇怪的内存泄漏。 chrome 中没有泄漏,但当我运行该应用程序时,它肯定存在。本质上,我需要遍历大量数据并将其设置在 $scope 上。

现实生活中的数据是从服务器上采集的,这里我只是用一个函数模拟了一下。此外,在实际应用中,$scope.vote 是通过按下按钮调用的,而不是通过执行 for 循环的按钮按下调用的。

也就是说这是一个很好的模拟。数据较小,但我使循环运行得更多,因此您实际上可以看到泄漏。当使用我从服务器收集的大型数据集时,泄漏要严重得多。

我目前正在运行 v1.0.0-beta.13(beta 14 给我带来了很多其他问题......)该包包含 angular 1.2.25。

我将其归结为以下测试用例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>


    <!-- compiled css output -->
    <link href="css/ionic.app.css" rel="stylesheet">

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <script>
        angular.module('starter', ['ionic'])
        .controller("testCtrl", function($scope){
            $scope.b = [];
            $scope.count = 0;
            function getBallots() {
                $scope.b.push({
                    _id: "54d7d680bdd622982e91a45f"
                });

                $scope.b.push({
                    _id: "54d7ef2ac659dd302a128924"
                });

                $scope.b.push({
                    _id: "54d7ef2ac659dd302a128929"
                });
            }

            getBallots();

            $scope.vote = function(){
                if($scope.b.length){
                    $scope.ballot = $scope.b.shift();
                    $scope.count ++;
                }
                if($scope.b.length<=0){
                    getBallots()
                }

            };

            $scope.start = function(){
                for(var i = 0; i < 10000; i++){
                    $scope.vote()
                }
            }

        })
    </script>

</head>
<body ng-app="starter" ng-controller="testCtrl">


{{ballot._id}}<br>
{{count}}
<br><br><br>
<button class="button button-large button-royal" ng-click="start()">BUTTON</button>

</body>
</html>

在我的 iPhone 5S 上分析应用程序时,Instruments 工具显示了这一点。我知道在此测试用例中泄漏的大小非常小,但在我的真实应用程序中,数据要大得多,所以这成为一个大问题。每个颠簸都是连续点击按钮 5 次的结果。

enter image description here

仪器跟踪文件可在以下网址下载:http://s000.tinyupload.com/?file_id=52410311803253693651

最佳答案

我不会选择这个作为“答案”,因为它不能解决问题,但我会分享我为减少应用内存问题所做的工作,以防对其他人有帮助。基本上我是这样做的:

  1. 升级到 ionic beta14,angular 1.3.6
  2. 重写逻辑以删除任何重复创建/销毁的“ng-ifs”。我用 ng-shows 或与 css 相关的东西替换了它们。

这显着减少了内存泄漏,但并未完全消除内存泄漏。

关于ios - 适用于 iOS 的 Ionic 和 Cordova 中的奇怪内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28424762/

相关文章:

javascript - 如何在 Controller 之前加载.run

android - fontawesome 图标未在 Android 应用程序中显示

ios - 使用 swift 从解析的 Json 中提取数据

iOS 应用程序安全性 : Which is the more secure option for storing license keys, Plist 文件或带有 "extern static"字符串的 Objective-C 文件

javascript - Angularjs 从模态获取数据并在表中发布

angularjs - 身份验证后不会调用卫星器

java - Android 通过 usb 反向网络共享(不是 root)

file - 无法在 Phonegap 中使用 inappbrowser 下载文件?

ios - Xcode 4.3.3 安装程序的备份

javascript - HTML5 视频无法在 iOS9 (iPad) 上启动