Garbage collection on script element

A

Andrew Poulos

I have a SCRIPT element with the ID of "dhtml". I'm dynamically
replacing it with a new script. Before I add the new script I was "told"
to do some garbage collection on the existing script, and was "given"
the following example:

var h = document.getElementsByTagName("head")[0];
var d = document.getElementById("dhtml");
if (d) {
// garbage collection
for (var prop in d) {
try {
delete d[prop];
} catch (err) {
// an error
}
}
h.removeChild(d);
}

When testing in IE 6 I get the error that 'object doesn't support this
action' even though I've wrapped it in a try/catch.

My questions are:
1. Do I actually need garbage collection when removing a SCRIPT element?
2. If it is needed, is there a way to get it to also work in IE 6?

Andrew Poulos
 
R

RobG

I have a SCRIPT element with the ID of "dhtml".

The id attribute is not valid for script elements in HTML, however
browsers seem to tolerate it and getElementById works (at least in the
couple of browsers I tested).

I'm dynamically
replacing it with a new script. Before I add the new script I was "told"
to do some garbage collection on the existing script, and was "given"
the following example:

I don't think that was good advice.

var h = document.getElementsByTagName("head")[0];
var d = document.getElementById("dhtml");

See above comment about script elements and the id attribute.
if (d) {
// garbage collection

Javascript garbage collection is not specified to run at any
particular time, nor does a script have any control over it[1] other
than perhaps to create a condition where if it ran, an object might be
removed.

Any object that is no longer referenced by any other property or
variable is a candidate for garbage collection. Therefore the best you
can do is remove all references to an object and hope that the garbage
collector will get rid of it at some time in the future (probably at
the latest when the page is closed, but might not happen until the
browser itself is closed).

For a DOM element, that means at least removing it from the DOM - but
all other references must be removed too (if you've created any).
for (var prop in d) {
try {
delete d[prop];

There is no guarantee that you can delete any property of a host
object.

But even if you can remove them all, it's pointless - it is references
*too* the object that must be cleared. Deleting the object's own
properties has no effect on that.

} catch (err) {
// an error
}
}
h.removeChild(d);

There is no need for a reference to the head element. Assuming d is a
reference to a DOM element, then:

d.parentNode.removeChild(d);

will do the job and doesn't require d's parent node reference to be
hard-coded.

Don't forget to remove your own reference to the element so that it
becomes a candidate for gargage collection:

d = null;
When testing in IE 6 I get the error that 'object doesn't support this
action' even though I've wrapped it in a try/catch.

Which version? Doesn't happen in IE 6 for me - perhaps your script
element isn't in the head.
My questions are:
1. Do I actually need garbage collection when removing a SCRIPT element?

No. Once a script element's content has been parsed and executed, the
objects and properties it creates are no longer influenced by the
script (i.e. the program code). Removing it has no effect on the
current script environment, it just removes a (now virtually useless)
DOM element.

Try this:

window.onload = function() {
var scripts = document.getElementsByTagName('script');
var i = scripts.length;
while (i--) {
scripts.parentNode.removeChild(scripts);
}
};

2. If it is needed, is there a way to get it to also work in IE 6?

No. The following reference might be helpful:

<URL: http://jrdodds.blogs.com/blog/2006/05/javascript_clos.html >

1. There is a method in JScript to run the garbage collector, but its
use is not recommended.
 
R

RobG

No. Once a script element's content has been parsed and executed, the
objects and properties it creates are no longer influenced by the
script (i.e. the program code). Removing it has no effect on the
current script environment, it just removes a (now virtually useless)
DOM element.
Try this:
  window.onload = function() {
    var scripts = document.getElementsByTagName('script');
    var i = scripts.length;
    while (i--) {
      scripts.parentNode.removeChild(scripts);
    }
  };
2. If it is needed, is there a way to get it to also work in IE 6?

No. The following reference might be helpful:

I suppose this idea originates from old Crockford's experiment about
nulling methods of DOM elements by iterating through their attributes
(and so on recursively through `childNodes`) -http://javascript.crockford..com/memory/leak.html


1. There is a method in JScript to run the garbage collector, but its
use is not recommended.

Why is not recommended?


Primarily from a design perspective. I'd be asking "what is it about
the design or implementation of this script that makes me want to call
the garbage collector?".

If a call to garbage collection is required in IE, then it will likely
also be required in other browsers. Since others don't have that
available, it seems unreasonable to saddle them with poor memory
management just because I was too lazy to things properly.

Secondly, I don't see IE's garbage collector being used widely even
though it seem to be a common opinion (rightly or wrongly) that IE has
poor memory management. That gives me a hint that those who have
tried it haven't found it useful enough to make its use common.

Lastly, Eric Lippert says on his blog:

"...you can force the JScript garbage collector to ... but I
don't recommend it. The whole point of JScript having
[an automatic] GC is that you don't need to worry about object
lifetime. If you do worry about it then you're probably using
the wrong tool for the job!"

<URL: http://blogs.msdn.com/ericlippert/archive/2003/09/17/53038.aspx

and that's enough for me. :)
 
S

SAM

Le 9/9/09 1:42 AM, Andrew Poulos a écrit :
I have a SCRIPT element with the ID of "dhtml". I'm dynamically
replacing it with a new script.

Wouldn't it be enough to only add the new script.
The functions and variables of new script that are same as in previous
script will replace the old one (without deleting them).

No ?

Your scripts are so heavy that you need to delete some of them when you
add some more ?
Maybe you work on a unique page adding and adding scripts more and more
(new data probably replace older when they are called) ?

If deleting variables and functions can be done I don't see how deleting
a DOM script element could do the same thing.
It seems to me that it is almost as you say: I want to forget this
message so I will throw out its sheet of paper.
(functions and global variables have been read(parsed?) and are in
memory, deleting their script will not push them out of memory, no?).
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top