By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,462 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,462 IT Pros & Developers. It's quick & easy.

clearing variables, necessary?

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Andrew Poulos wrote:
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>
--
Rob
Jul 23 '05 #2

P: n/a
Andrew Poulos wrote:
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.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.