Odd Memory Leak in Ionic & Cordova for iOS

I have an odd memory leak in my ionic and cordova app. The leak is not there in chrome but when I ran the app, it is definitely there. Essentially, I need to loop through a large set of data and set it on $scope .

The data in real life is gathered from a server, but here I just simulated it with a function. Also, in the real app, $scope.vote is called by a button press, not a button press that enacts a for-loop.

That said this is a good simulation for it. The data is smaller but I made the loop run more so you can actually see the leak. The leak is much more significant when using the large datasets I'm gathering from the server.

I am currently running v1.0.0-beta.13 (beta 14 is causing a lot of other issues for me...) The bundle contains angular 1.2.25.

I've boiled this down to a test case below:

<!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>

The Instruments tool shows when analyzing the app on my iphone 5S shows this. I know the size of leak is pretty small here in this test case, but in my real app, the data is a lot larger and so this becomes a big problem. Each of the bumps is the result of 5 sequential clicks on the button.

在这里输入图像描述

Instruments trace file can be downloaded at: http://s000.tinyupload.com/?file_id=52410311803253693651


I will not select this as an "answer" because it doesn't solve the problem, but I will share what I did to reduce my app's memory problem in case it is helpful for anyone else. Essentially I did this:

  • upgrading to ionic beta14, angular 1.3.6
  • rewriting logic to remove any "ng-ifs" that were being repeatedly created/destroyed. I replaced these with ng-shows or something css-related.
  • This reduced memory leak significantly but did not remove the memory leak completely.


    I also experienced similar problems and I spent hours optimising my code to fix it :

  • switched to track by in ng-options, ie "activity.name for activity in activities track by activity.id" (<- this had the biggest impact)
  • removed as many ng-show / ng-if as possible
  • removed all $rootScope variables (there shouldn't have been (m)any in the first place)
  • removed all 'watch' tasks and replaced them with events - I destroyed ($destroy) those only needed once
  • reduced the number of $scope variables ... actually I will/should use the controllerAs-Syntaxt (http://toddmotto.com/digging-into-angulars-controller-as-syntax/) in the next step
  • 链接地址: http://www.djcxy.com/p/83626.html

    上一篇: 为什么使用Option Strict On允许Double to Single分配

    下一篇: iOS中的Ionic&Cordova奇怪的内存泄漏