5

If one made a web application that never refreshed a page but was built completely from the first page plus Javascript requests, thereby creating and destroying elements as required, would any of the browsers reuse the memory used by the obsolete dom elements?

Is this planned in any browsers yet?

I'm thinking that full blown extJS apps would be very sensitive to this kind of memory leakage.

Is there any truly effective re-use strategy to mitigate this problem?

I'm not referring to Javascript object garbage collection here, only removed DOM elements, but I'm not sure if that is essentially the same thing in the end.

barrymac
  • 2,750
  • 1
  • 20
  • 32
  • A related question with some good answers: http://stackoverflow.com/questions/3785258/how-to-remove-dom-elements-without-memory-leaks – barrymac Nov 15 '11 at 16:11
  • more information, see description: https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special_Operators/delete_Operator – barrymac Nov 15 '11 at 16:45

2 Answers2

2

It looks like Chrome does this: http://jsfiddle.net/GaPLT/1/.

Memory usage:

  1. Start: 45K
  2. After adding: 60K
  3. After removing: 49K
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    Very illustrative tool there, can we account for the where did the 4K come from? – barrymac Nov 15 '11 at 15:59
  • @barrymac: I can't exactly tell you, but I'd guess that hovering the button (i.e. new graphics) and handling the clicking took some memory as well. When you even move the mouse things are happening behind the scenes which may use memory as well, like the fading labels of the textareas on jsFiddle. – pimvdb Nov 15 '11 at 16:01
  • It's a bit pedantic I know, but I'm sure someone knows, or perhaps there's a more minimal and controlled test to remove these ambiguities. – barrymac Nov 15 '11 at 16:29
-1

Short answer is it depends on your JavaScript engine.

This is how Chrome's V8 does it http://code.google.com/apis/v8/design.html#garb_coll

Andrew Kolesnikov
  • 1,920
  • 1
  • 14
  • 20
  • 2
    I would have expected that DOM garbage collection is not the same as Javascript object garbage collection, and I know that V8 has some interesting ways around Javascript's prototype design. Would this their optimisations also apply to actual DOM elements? – barrymac Nov 15 '11 at 15:43
  • 3
    This article is just about JS memory. It doesn't even mention DOM. Chrome stores DOM elements as native objects and creates JS wrapper objects for each and the JS wrapper objects are GC like other JS objects, except they're kept in groups by DOM subtree. I searched and couldn't find how they do memory tracking for the actual underlying native DOM objects. Firefox and IE both use reference counting (or at least did up through about 2010, I'm not sure if it changed since). – Samuel Neff Mar 21 '13 at 15:11