performance - Javascript memory leak setTimeout issue -
does 1 know why memory consumption stays constant here ?
var count = 0; $(init); function init(){ var node = document.queryselector('.logs'); function check(){ var uarr = new uint16array(100); log(node, uarr.length); settimeout(check,100); } settimeout(check,100); } function log(node, text){ if( count % 30 == 0 ){ node.innerhtml = ''; } var child = document.createelement('div'); child.innertext = 'count ' + (count++) + " arr len " + text; node.appendchild(child); }
reason why should linearly increase memory allocation is: 'check' method calls inside it's definition, closure variables available inside check method execution, again creates execution context test function , on.
also, in every execution, create memory block of uint16array, believe allocated in heap, , should never de-allocated since reachable closure.
memory profile:
looking @ memory timeline, not seem increase memory allocation time increases. expected behavior ?
uarr
local variable allocated, used, , garbage collected after check()
exits. , there no closure inside check()
. settimeout()
invoked (but not defined) check()
.
this page on closures may helpful.
while true if there n calls check()
, there have been n closures created (as n copies of node
), settimeout()
release reference check()
after calls it. therefore, there no leak there either.
Comments
Post a Comment