title: How to find memory leaks in angularJs applications
body: <p>It seems I have memory leak in our application and its time to bang my head against wall trying to find it hard but I think I am noob to do that since its my first time and being unfamiliar with chrome dev tool.</p>
  
  <p><strong>Overview of application architecture:</strong></p>
  
  <p>We have a single page application developed on angularjs framework. we are consuming API's to get data and do CRUD operations.
  each module have multiple pages (say we have banks, branches, employees, managers, accountants etc) and each module have multiple states (we are using <strong><em>angular UI-router</em></strong>). I have created a <code>angular factory Object</code> on my top module which needs to be stay alive in whole life cycle of application and each module will register their sub model on it in which I have my page settings API's and list of fields which map to object properties return by API.</p>
  
  <p>something like below:</p>
  
  <pre><code>angular
      .module('mainModule')
      .factory('model', apiService);
  
  /** @ngInject */
  function apiService()
  {
      var model= {};
  
      // some code
      return model;
  }
  </code></pre>
  
  <p>and for each sub module I am registering my subModel in run block as below:</p>
  
  <pre><code>(function() {
  'use strict';
  
  angular
      .module('subModule')
      .run(runBlock);
  
  /* @ngInject */
  function runBlock(model) {
  
      if (angular.isUndefined(model.subModel)) {
          model.subModel= {};
      }
       model.subModel.banks= {
          modelPath: 'subModel.banks',
          fields: [
              { field: 'Id', type: 'int', displayLabel: 'BANKS.FIELDS.ID', visible: false, creatable: false, editable: false, inputType: 'number',
                  validation: {isRequired: true, maxLen: -1, minLen: 1, pattern: '', messages: {isRequired: 'VALIDATIONS.REQUIRED', maxLen: 'VALIDATIONS.MAXLEN', minLen: '', pattern: ''}} },
               //more similer fields which translate API's object property to what I want to show in UI
              ],
          title: 'CATALOGS.NAV',
          icon: 'icon-book-open',
          idField: 'Id',
          lookupDisplayField: 'BankName',
          methods: {
              getList         :   getBanks
          },
          views: {
              mainList: {
                  columns         : [ 'Id', //and more columns],
                  //more settings
              },
              mainDetails: {
               //settings for detail page
              },
          }
      };
  
      function getBanks() {
             return promise
          }
      .......... and so on
  </code></pre>
  
  <p>(if there is a name for this architecture I would like to know)</p>
  
  <p>I have a directive which will create my UI page, this directive will get data returned by API, instance of subModel which is created on runBlock of sub Module and they are 2way binded to my directive</p>
  
  <p>[my controller look somtehing like this]</p>
  
  <pre><code>(function() {
  'use strict';
  
  angular
      .module('subModule')
      .controller('banksController', banksController);
  
  /* @ngInject */
  function banksController(model) {
      var vm = this;
  
      //Data
      vm.banksModel = model.subModel.banks;
  
      vm.banks= [];
      vm.isLoading = false;
  
  
      //Methods
      vm.refreshItems = refreshItems;
  
      activate();
  
      ////////////////
  
      function activate() {
          refreshItems();
      }
  
      function refreshItems() {
          vm.isLoading = true;
  
          vm.banksModel.methods.getList()
          .then(function (response) {
              vm.banks= response.data;
          })
          .finally(function () {
              vm.isLoading = false;
          });
      }
      .......... and so on
  </code></pre>
  
  <p>[template]</p>
  
  <pre><code>&lt;my-directive my-data="vm.banks" my-model="vm.banksModel"
              my-is-loading="vm.isLoading"...&gt;&lt;/my-directive&gt;
   ....
  </code></pre>
  
  <p>[directive]</p>
  
  <pre><code>(function() {
  'use strict';
  
  angular
      .module('highLvlModule')
      .directive('myDirective', myDirectiveDirective);
  
  
  function myDirectiveDirective() {
      var directive = {
          bindToController: true,
          controller: myDirectiveController,
          controllerAs: 'vm',
          link: link,
          restrict: 'E',
          scope: {
              myData: '=',
              myModel: '=',
              isLoading: '=?txIsLoading',
              ..//otherbindings
          },
          templateUrl: 'app/..../.../my-directive.html'
      };
      return directive;
  
      function link(scope, element) {
          scope.element = element;
  
          if (angular.isUndefined(scope.vm.isLoading)) {
              scope.vm.isLoading = false;
          }
  
      }
  }
  /* @ngInject */
  function myDirectiveController(model, $window, $scope, $rootScope, $document, $timeout, $localStorage, dataFactory,$filter, hotkeys, settingsFactory, dragulaService) {
  
  
      var vm = this;
  
      //data
  
      vm.fields = vm.myModel.fields;
      ...// and so on
  </code></pre>
  
  <p>myDirective will render the page based on the myModel and populate data based on myData
  .</p>
  
  <p>somewhere something is wrong in my application and I have memory leak.
  [Question]:does vm get destroyed when controller or directive controller get destroyed? if look at <code>refreshItems</code> method in <code>banksController</code> we are using vm.banks in context of a closure, is the the reason of leak?</p>
  
  <p><strong>Why I think we have a leak</strong>
  If I navigate from one page to another and go page to the previous page (after click on garbage collector) the memory usage do not reach to where it was and on repeating this action the memory keep extending by 30-34 MB which is the size of data in one of the module (I am keep switching between two module one having very light data and the other having very big data 80K records)
  following is Heap snapshot and did the following steps to create that</p>
  
  <ol>
  <li>refresh the browser on thin module wait for it to be loaded completely</li>
  <li>switch to fat module wait for it to be loaded completely</li>
  <li>switch back to thin module and took heap snapshot</li>
  <li>switch back to fat module and took heap snapshot</li>
  <li>switch back again to thin module and took heap snapshot</li>
  </ol>
  
  <p>and this is the result(objects created between 3 to 4 and still alive in steps 5 will be displayed in summery)</p>
  
  <p><a href="https://i.stack.imgur.com/NnKpU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NnKpU.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/R4naG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R4naG.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Z5GTe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Z5GTe.png" alt="enter image description here"></a></p>
  
  <p>if we see in above image Array @2145987 and Array @2145989 are the record created in steps 2 and for and still alive in step 5</p>
  
  <p>here is the timeline snapshot
  <a href="https://i.stack.imgur.com/zy1X4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zy1X4.png" alt="enter image description here"></a></p>
  
  <ol>
  <li>refresh on thin module and wait for 5 secs and start the timeline</li>
  <li>after 12 seconds click on GC</li>
  <li>after 8 sec (20000ms) navigate to fat module</li>
  <li>fat module loaded at 110000ms</li>
  <li>after 10 sec (120000ms) navigate to thin module and it loaded imidiatly</li>
  <li>after 10 sec (130000ms) navigate back to fat module and it loaded at 133000ms</li>
  <li>after 10 sec navigate back to thin module and it loaded immediately (143000ms)</li>
  <li>after 10 sec click on GC at 153000ms</li>
  </ol>
  
  <p>timeline snapshot below where taken after performing above steps (refresh on thin, navigate to fat, navigate to thin, navigate to fat then thin and then start timeline)</p>
  
  <p><a href="https://i.stack.imgur.com/0jDYG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0jDYG.png" alt="enter image description here"></a>
  above image is timeline snapshot of switching between those two modules without executing GC between each navigation</p>
  
  <p><a href="https://i.stack.imgur.com/AzTMY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AzTMY.png" alt="enter image description here"></a>
  above image is timeline snapshot of switching between those two modules with executing GC between each navigation</p>