CoderGLM CoderGLM - 1 year ago 54
Javascript Question

how to release closure's memory in javascript?

A closure:

function test() {
var count = 0;

return function() {

As we all know, the
won't release after
be called, and now if the closure is useless to me, how could I release memory of it?

Answer Source

All objects in javascript is garbage collected, regardless of weather they're involved in a closure or not. In your code above, if you do:

var x = test();

then of course the memory for count cannot be released because it may be used later by x. However if you do:

var x = test();
x = "";

the memory for count will be garbage collected sooner or later.