3

Does any one 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);
}

http://jsfiddle.net/V99Eb/11/

Reason why it should linearly increase memory allocation is: the 'check' method calls itself inside it's definition, so the closure variables will be available to the inside check method execution, which then again creates an execution context for the test function and so on.

Also, in every execution, we create a memory block of Uint16Array, which I believe is allocated in the heap, and should never get de-allocated since it is reachable from the closure.

Memory Profile: enter image description here

Looking at the memory timeline, it does not seem to increase memory allocation as the time increases. Is this the expected behavior ?

asked Apr 23, 2014 at 1:05
2
  • Why does your title claim a memory leak issue when in fact you're demonstrating that there is no memory leak? Commented Apr 23, 2014 at 1:51
  • 1
    uArr is only a local variable whose lifetime is only the current execution of check(). As soon as one invocation of check() finishes, that invocation's local variables are eligible for garbage collected because there is no longer any code that can reach them. The only variable that survives indefinitely in the closure is node. Commented Apr 23, 2014 at 2:21

1 Answer 1

3

uArr is just a local variable that is allocated, used, and then garbage collected after check() exits. And there is no closure inside check(). setTimeout() is invoked (but not defined) by check().

This page on Closures may be helpful.

While it is true that if there are N calls to check(), there would have been N closures created (as well as N copies of node), setTimeout() will release its reference to check() after it calls it. Therefore, there is no leak there either.

answered Apr 23, 2014 at 1:30
Sign up to request clarification or add additional context in comments.

2 Comments

Your first sentence is not quite true. If you put a call to an async function with an anonymous completion callback function as an argument in a for loop that does N iterations, you will get N separate closures. The layout determined that there would be a closure, but the N closures themselves were created as the code executed because of the path of execution.
@jfriend00 You are correct. Thanks! I've modified my answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.