ng-include是否有内存泄漏?

时间:2013-03-09 11:45:54

标签: memory-leaks angularjs

我使用ng-include来切换不同的数据页面,这些页面将进行大量的数据渲染。

我发现浏览器的内存使用量不断增长,并且永远不会减少。

代码非常简单。

HTML code:

<body ng-controller="MainCtrl">
  <div>
    <button ng-click="url='nodata.html'">No data</button>
    <button ng-repeat="i in getNumArray(10)" ng-click="loadData(i)">Load data {{i}}</button>
  </div>
  <hr/>
  [{{url}}]
  <div ng-include="url"></div>
</body>

它将显示“无数据”按钮,以及10个数据按钮以加载不同的页面。

角度代码:

app.controller('MainCtrl', function($scope) {
  $scope.url = "nodata.html";
  $scope.loadData = function(n) {
    $scope.url = "data" + n + ".html";
  }

  $scope.getNumArray = function(n) {
    var arr = [];
    for(var i =0;i<n;i++) {
      arr.push(i);
    }
    return arr;
  }
});

app.controller('DataCtrl', function($scope, $http){
  $http.get('data.json').success(function(data){
      $scope.data = data;
  })
});

以及“dataN.html”页面:

<div ng-controller="DataCtrl">
  <table ng-repeat="x in getNumArray(500)">
    <tbody>
      <tr>
        <td>{{data["key0"]}}</td>
        <td>{{data["key1"]}}</td>
        <td>{{data["key2"]}}</td>
        <td>{{data["key3"]}}</td>
        <td>{{data["key4"]}}</td>
        <td>{{data["key5"]}}</td>
        <td>{{data["key6"]}}</td>
        <td>{{data["key7"]}}</td>
        <td>{{data["key8"]}}</td>
        <td>{{data["key9"]}}</td>
      </tr>
    </tbody>
  </table>
</div>

“nodata.html”页面:

<div>No data yet.</div>

“data.json”:

{
  "key0": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f",
  "key1": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f",
  "key2": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f",
  "key3": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f",
  "key4": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f",
  "key5": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f",
  "key6": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f",
  "key7": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f",
  "key8": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f",
  "key9": "sdf sdf sdf sdf sdf sdf sdf sdf sdf sd fds fds fsd fds fds fds fds fsd fds fds f"
}

这是一个现场演示:

http://plnkr.co/edit/KGZVXIBws1kthgN2bxEJ?p=preview

当我使用chrome打开实时演示时,初始化内存使用量小于100M。然后我点击“加载数据”按钮,即使我点击“无数据”按钮加载“nodata.html”,它也会很快增长到300M并且永不退缩。

这是正常的吗? ng-include是否有内存泄漏或我遗漏了什么?或者内存使用情况很好,我不需要担心它?

截屏

我创建了一个截屏视频来展示它:

ng-include

2 个答案:

答案 0 :(得分:2)

尝试升级到1.0.5版。它似乎没有这个问题。 我相信这是因为当模板中存在顶级空白节点时,1.0.3 / 4中存在内存泄漏。

答案 1 :(得分:2)

Stackoverflow不是提交错误的地方。请在https://github.com/angular/angular.js/issues处提交问题并继续讨论。

我已将用例简化为单个文件:http://plnkr.co/edit/Wm4YRsLGJUDqUcww2DQZ?p=preview

这是我发现的。

  1. 仅在Windows上泄露,在Mac OS X上不泄漏
  2. 只有漏斗的泄漏。当我在它的外面运行时,它可以正常工作。
  3. 你能在plunker之外重现这个问题吗?