Javascript Memory Management Leaks -
currently creating web application. users should able running application day. having memory issues. browser seems crash. using kind of structure:
function module() { var _me = this; this.init = function(){ _me.setbindings(); // using reference module instead of } // kind of functions this.init(); }
which changed this
.
so more complex situation (which part of code atm):
$.modules.dynamic_static_webpage.prototype.addredirect = function (anum, aeditor) { $.prompt( $.utils.gettranslation("redirect"), $.utils.gettranslation("geef de url op waar naar toe geredirect moet worden"), $.proxy(function (num, editor, input) { this.clearredirect(editor); var val = input.val(); if (val.indexof("www") == 0) { val = "http://" + val; } // timeout needed, because otherwise clear not finished settimeout($.proxy(function (n, e, v) { $.htmltexteditorfield.setiframeselectionhtml.call(e, "{cms-redirect" + n + "_" + v + "}"); this.redirectshow(n, v); }, this, num, editor, val), 200); }, this, anum, aeditor) ); };
now i've added $.proxy
lot. seems bit strange.
i have lot of "using variables outside scope, inside scope". rewrite above code. i've looked on different sites this, can't figure out:
can explain me if correct approach avoid memor leaks? or there better solution?
there's nothing in code indicate me you'll have memory leak issues. more concerningly, however, style of code you're using avoid memory leaks making difficult tell whether there memory problems going forward. you've posted few dozen lines of code; codebase few thousand lines of code written in style might impossible audit memory leaks.
some memory management techniques use:
- if object no longer needs used, dereference it. garbage collector clean object you.
- if dynamically call
addeventlistener
, callremoveeventlistener
unless dom node you're binding events gets destroyed later. - if reference object within function, reference function somewhere, still have reference object. avoid keeping references things no longer need.
following 3 guidelines of way through career dealing javascript without memory-related issues :)
Comments
Post a Comment