3

I'm making a lightbox, do I need to use the

$(window.top).unload(function() { .. });

to do clean up of variables, or will all browsers automatically clean up everything and reset memory and what not?

Do I need to bother or will the browser do it for me? What with all the memory leaks and all..

Edit: I attached a lot of data to div elements. So do I need to do removeData at unload?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 1
    Nothing of importance can/should be done in the unload event. It is not always guaranteed to fire. "The exact handling of the unload event has varies... some versions of Firefox trigger the event when a link is followed, but not when the window is closed." –  Jul 28 '11 at 20:04
  • Ok thanks, but for reference, don't really care when they close the window: only when they continue navigating my site do I care about memory leaks ;) –  Jul 28 '11 at 20:06

3 Answers3

5

(Yes the browser does it for you | No you do not have to do any cleanup), Javascript is garbage collected.

Bob Fincheimer
  • 17,978
  • 1
  • 29
  • 54
robbrit
  • 17,560
  • 4
  • 48
  • 68
0

Take a look at this, it might help you understand memory leak in using jquery and plugins based on jquery.

jQuery memory leak with DOM removal

Community
  • 1
  • 1
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
0

There are a couple different ways to create memory leaks in javascript. Most have to do with creating circular references between javascript objects and the DOM. see:

http://www.ibm.com/developerworks/web/library/wa-memleak/

but otherwise, no, you do not have to explicitly delete references to things - the javascript garbage collector will do this for you.

The code snippet that you posted has a side effect that you should be aware of, however. If you attach anything to the onunload handler for the document, then browsers that support caching the state of the DOM/javascript engine for fast retrieval when navigating backwards/forwards are no longer able to keep the page in the bfcache (see http://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/). This can have performance implications.

digitalbath
  • 7,008
  • 2
  • 17
  • 15
  • The circular references between DOM/JS present in some browsers should still be broken when the page is unloaded -- at that point the particular DOM document is destroyed (as well as the JS context). Thus it's outside of the unload usage ;-) –  Jul 28 '11 at 20:06
  • Interesting, I basically add a lot of .data (title, description, options) to certain document elements. I guess there is no harm in that? –  Jul 28 '11 at 20:07
  • @Westley as long as you are not creating a circular reference, eg `e=document.getElementById('foo');document.getElementById('foo').bla = e` you should be OK. – digitalbath Jul 28 '11 at 20:10
  • @pst actually, many browsers keep the entire state of the page (including the DOM and the state of the JS engine) around in the `pagecache` even when the user navigates away in order to support fast back/forward behavior. – digitalbath Jul 28 '11 at 20:11
  • Thanks pst - @digitalbath - but I presume this is some sort of first in first out cache? It's not like they keep that cache around, and if it hits a specific limit they're emptying it one page at a time? –  Jul 28 '11 at 20:39
  • I am not sure - that sounds like an implementation detail that may vary from browser to browser. – digitalbath Jul 28 '11 at 20:44