4

What happens to Javascript when page navigates away? A book I am reading is instructing to clear things on "unload" event.

But what's the point? doesn't everything get lost and released when pages moves away in browser?

Thanks,

Sean

Boaz
  • 19,892
  • 8
  • 62
  • 70
born2net
  • 24,129
  • 22
  • 65
  • 104

3 Answers3

1

The only case where this is useful is in clearing event handlers that have cyclical references between the DOM and JS engines (not always event handlers, but it's the most common way) and therefore caused memory leaks. Everything else is garbage collected when you unload the page

See this post Javascript memory leaks after unloading a web page

Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
1

Usually the browser does a good job of cleaning up memory after page navigation. Howerver, there are things you can do to confuse the browser and cause it to hang on to memory between page transitions. This is particularly true when you are storing data in DOM elements, or have circular references between the DOM and your script.

This was a bigger issue a few years ago in older browsers. Current browser releases are better about memory cleanup between page transitions.

It may make sense to do some clean-up during page unload, but it's even better to modify your design to eliminate circular references. Don't store data in the DOM.

Here's a good article for more detail.

An article at MDN regarding JavaScript leaks in FireFox.

Here's a Microsoft Knowledge Base article with IE specific detail.

gilly3
  • 87,962
  • 25
  • 144
  • 176
-1

Sure. But you could free some memory in the browser by unloading (not really useful nowadays, but useful in the past). Or you can do some action on unload (i.e. asking user confirmation, saving stuff, etc...).

napolux
  • 15,574
  • 9
  • 51
  • 70
  • 1
    As far as I know you can **not** free some memory in the browser by unloading stuff in any way, I would love to see a reference indicating otherwise. – Benjamin Gruenbaum Feb 04 '13 at 18:52
  • @BenjaminGruenbaum You may not immediately free memory, by by using the `delete` operator, you can remove the last reference to an object so it can be garbage collected. – Ruan Mendes Feb 04 '13 at 22:39
  • @JuanMendes delete is a horrible operator and should not be used. Modern browsers extract classes out of objects you create which is a big performance gain, as soon as a delete is there the browser will be unable to optimize. You will see a 10 fold performance hit when using delete. – Benjamin Gruenbaum Feb 04 '13 at 23:34
  • @BenjaminGruenbaum If that's your concern, you can set your references to `null`. Make your optimizer and your garbage collector happy – Ruan Mendes Feb 04 '13 at 23:49
  • Setting object reference is considered by some bad practice, you should just keep variables as local as possible, see http://coding.smashingmagazine.com/2012/11/05/writing-fast-memory-efficient-javascript/ – Benjamin Gruenbaum Feb 04 '13 at 23:55
  • @BenjaminGruenbaum Nulling references is the only way to break cyclical references. The article you mentioned does talk about nulling references to allow the gc to reclaim memory. I think you're confusing things and over optimizing the wrong thing. My suggestion to null things is in line with the article you talked about (reuse those hidden classes) but still lets the gc do its work. You can't possibly expect your code to run only using the stack – Ruan Mendes Feb 08 '13 at 07:58
  • The article says that nulling things is better than deleting them but the correct thing to do is to not do either and just use correct scope. – Benjamin Gruenbaum Feb 08 '13 at 08:28