javascript - Garbage collection in Backbone -


* updated - added sample code 1 of view *

this has been discussed number of times , have gone through lot of suggestion on topic still don't have luck.

my application tab based i.e. user searches entity in global search box , on selection of entity view/model generated , view rendered under new tab.user can open multiple tabs repeating above process.

the problem facing every time new tab opened can see browser memory consumption goes approx 6 mb (the data fetched & displayed each tab max 60kb).

not when close tab can see custom close function (copied below) being called each view under tab somehow browser memory not go down. me means garbage collection not working or view/models have not been cleaned properly.

any appreciated.

define([     "hbs!modules/applications/templates/applications",     "vent" ], function (tpl, vent) {      var view = backbone.marionette.itemview.extend({          classname: 'modapplications',          template: {             type: 'handlebars',             template: tpl         },          refresh: function(){             self.$('.body-of-table').css('visibility', 'hidden');             self.$('.application-panel .spinnerdiv').addclass('loading');             this.model.fetch().always(function(){                 self.$('.application-panel .spinnerdiv').removeclass('loading');             });         },          initialize: function(){             this.model.on('change', this.render, this);             vent.bindto(vent, 'updateapplications', this.refresh, this);         },          onshow: function(){             var self = this;             this.$el.on('click', '.action-refresh', function(e) {                 self.refresh();                 e.preventdefault();             });         },          close: function() {             _.each(this.bindings, function (binding) {                 binding.model.unbind(binding.ev, binding.callback);             });             this.bindings = [];             this.unbind();             this.off();             this.model.off('change');             this.model.unbind('change', this.render, this);             this.remove();             delete this.$el;             delete this.el;             if(console) console.log("kill : view.applications");         }      });      return view;  }); 

found problem & fix.

the issue was using 1 global marionette.eventaggregator new tabs. used means communication between various sections within tab. now, when tab closed main.js file still held reference global vent other tabs still using it. reference closed tab still held , hence views/models not gc'ed.

to fix created separate vent each tab , used vent object trigger event within tab. on tab close action unbindall events , assign null reference vent tab closed.

hope helps in future.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -