Garbage Collection and User Interfaces?

P

petermichaux

Hi,

I'm working on a JavaScript drop-down menu and have come across a
general question about garbage collection. Often in user interfaces you
don't need to keep a reference to a object that is controlling the
widget. You just need to set up the widget and let it live out it's
life being controlled by the object. It could be something like this

<ul id="tinyMenu">
<li>One</li>
<li>Two</li>
</ul>

<script>
function Menu(element) {
// initialize the menu
}

new Menu(document.getElementById("tinyMenu"));
</script>

Could garbage collection cause trouble in this case?

I don't really want to set some dummy variable equal to the new Menu
object. I just don't need to know about the menu anymore. I have this
same issue nested deeper in my code and if garbage collection won't
hurt me then I'd be happier with the simpler code.

Thanks,
Peter
 
R

RobG

(e-mail address removed) said on 19/03/2006 6:47 PM AEST:
Hi,

I'm working on a JavaScript drop-down menu and have come across a
general question about garbage collection. Often in user interfaces you
don't need to keep a reference to a object that is controlling the
widget. You just need to set up the widget and let it live out it's
life being controlled by the object. It could be something like this

<ul id="tinyMenu">
<li>One</li>
<li>Two</li>
</ul>

<script>
function Menu(element) {
// initialize the menu
}

new Menu(document.getElementById("tinyMenu"));
</script>

Could garbage collection cause trouble in this case?

What do you mean by 'cause trouble'? In the above, calling - new Menu()
- creates a new instance of the Menu object. But since it's not
assigned to anything, nothing has a reference to it unless you set up
some closures internally. After it's finished, it will be destroyed
when the garbage collector next runs. Even if it wasn't, because you
haven't assigned it to any variable you can't get a reference to it
(except for closures).

I don't really want to set some dummy variable equal to the new Menu
object. I just don't need to know about the menu anymore. I have this
same issue nested deeper in my code and if garbage collection won't
hurt me then I'd be happier with the simpler code.

You want the new Menu object to persist, but you don't use it? Why
bother with the 'new' keyword at all? I can only presume you are using
'new' because you also use 'this' inside Menu(). Internally, Menu can
refer to itself with arguments.callee, which is not quite the same as
using 'this' but there are ways to make it work.

Alternatively, you could use:

var tempObj = new Menu();

tempObj = null;

In which case the instance of Menu formerly referred to be tempObj no
longer has anything referencing it and it can be garbage collected.

If, on the other hand, you want the new instance of Menu to persist,
then you have to keep a reference to it. Post a bit more of what you
are trying to do that shows how Menu is used and maybe you'll get more
precise advise. :)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top