Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>1) No. The browser clears everything between page loads.</p> <p>2) In its current form there will be no memory leak, since jquery's <code>.each()</code> function doesn't bind anything, so once its execution is finished, the anonymous function it was passed is no longer reachable, and therefore the environment it closed (i.e. the closure as a whole) is also not reachable. So the garbage collection engine can clean everything up - including the reference to <code>objects</code>. </p> <p>However, if instead of <code>.each()</code> you had something harmless, like <code>$('div:eq(0)').bind()</code> (I'm trying to emphasise that it needn't be a reference to the large <code>objects</code> variable, enough that it is even a single unrelated element), then since the anonymous function sent to <code>.bind()</code> closes the <code>objects</code> variable, it will remain reachable, and therefore not garbage collected, allowing memory leaks. </p> <p>A simple way to avoid this problem, is to <code>objects = null;</code> at the end of the executing function.</p> <p>I should note that I'm not familiar with the JS garbage collection engines, so it is possible that there are reasonably intelligent optimisations. For example it can be checked whether or not the anonymous function tries to access any variables closed in with it, and if not, it might pass them to the garbage collector, which would solve the problem.</p> <p>For further reading, look for references to javascript's memory model, specifically the environment model and static binding.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload