Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using Angular (1.1.5) as the front end for an ASP.Net MVC3 project and am having problems with memory consumption in the browser when using ng-repeat.

It looks as though when the route changes and the ng-view tag is updated, the old dom elements are not garbage collected when ng-repeat is used.

Setup is as follows:

  • _layout.cshtml page with <html ng-app="MyApp">, scripts, css and the header with a link to the angular app
  • index.cshtml partial using the layout page and contains a single <ng-view></ng-view> tag
  • index.html containing my content with the ng-repeat

    index.html:

    <div ng-controller="MyCtrl">
        <div ng-repeat="item in items">
            <p>Blah Foo Bar lorem ipsum gack</p>
            <hr />
        </div>
    </div>
    
  • app.js containing the main app module, with routing config and controller

app.js

var app = angular.module('MyApp', ['MyApp.content'])
    .config(['$routeProvider', function($routeProvider) {
        $routeProvider
            .otherwise({ templateUrl: '/index.html' });
    }]);

angular.module('MyApp.content', []);

angular.module('MyApp.content')
    .controller("MyCtrl", ['$scope', function ($scope) {
            $scope.items = [{}, {}];
        }]);

So clicking on the link in the header (which always produces a route change as no specific routes are defined) repeatedly and then calling the garbage collector produces the following in chrome timeline:

Memory Consumption

As you can see the memory baseline does not return to the initial and this continues to increase the more route changes are made. Also the dom element count continues to increase. Looking at a heap snapshot, these are detached but still hanging around.

Removing the ng-repeat results in the dom count reseting each route change.

Anyone know why? How to prevent these elements from hanging around?

Update: I created a new MVC3 project in VS2010 and didn't have the issue. Created a new MVC3 project in VS2012 and did. :/

share|improve this question
1  
Which version of AngularJS are you using? There is an ng-repeat issue with the framework, however it applies only to nested ng-repeats. –  Praveenram Balachandar Sep 12 '13 at 19:29
    
1.1.5 and also tried the 1.2rc2 but same thing. I have created a new MVC3 project and am having trouble recreating the issue, so I guess I may be missing something else in my existing project. –  Breandán Sep 13 '13 at 13:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.