clearing variables, necessary?

A

Andrew Poulos

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
 
R

RobG

Andrew said:
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>
 
R

Richard Cornford

Andrew said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top