If I'm using DOM methods to dynamically create elements in a page is it
necessary for me to clear whatever variables I used to create the
elements when the page unloads? I'm reading bits and pieces which seems
to indicate that they'll be memory issues if I don't.
Andrew Poulos
RobG - 30 Jan 2005 23:52 GMT
> If I'm using DOM methods to dynamically create elements in a page is it
> necessary for me to clear whatever variables I used to create the
> elements when the page unloads? I'm reading bits and pieces which seems
> to indicate that they'll be memory issues if I don't.
No.
" In fact, all user-defined properties (which includes all
variables) are erased whenever a web page is unloaded. The
scripts in a freshly loaded document start with no variables
defined, and no properties in their Window object, except
for the standard properties defined by the system. What this
means is that the lifetime of scripts and of the variables
they define is the same as the lifetime of the document that
contains the script. This is potentially much shorter than
the lifetime of the window or frame that displays the
document that contains the script."
<URL:http://www.web-developer-india.com/web/jscript/ch11_06.html#ch11-SECT1-AUTOID.6>

Signature
Rob
Richard Cornford - 31 Jan 2005 00:57 GMT
> If I'm using DOM methods to dynamically create elements in a
> page is it necessary for me to clear whatever variables I used
> to create the elements when the page unloads? I'm reading bits
> and pieces which seems to indicate that they'll be memory issues
> if I don't.
Javascript uses automatic garbage collection and should free memory when
pages unload. There is, however, a running problem with IE browsers. If
_circular_ references are constructed between ECMAScript native objects
and DOM/COM objects then IE's garbage collection cannot recognise when
it is appropriate to free those objects. This can result in memory
leaks, but the references have to be circular before this is an issue.
One of the strategies for dealing with this problem is to break circular
references onunlod. Others include never creating such references or
breaking such references at the end of functions that create them.
Javascript closures are the most common source of problematic circular
references between javascript objects and DOM/COM objects (the c.l.js
FAQ includes an article on closures with some coverage of the memory
leak problem).
Richard.