469,281 Members | 2,450 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,281 developers. It's quick & easy.

Garbage Collection and User Interfaces?

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

Mar 19 '06 #1
1 1012
pe**********@gmail.com 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. :-)
--
Rob
Mar 19 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by mike2036 | last post: by
28 posts views Thread by Goalie_Ca | last post: by
56 posts views Thread by Johnny E. Jensen | last post: by
72 posts views Thread by Sebastian Nibisz | last post: by
350 posts views Thread by Lloyd Bonafide | last post: by
158 posts views Thread by pushpakulkar | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.