Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Garbage Collection and User Interfaces?

Reply
Thread Tools

Garbage Collection and User Interfaces?

 
 
petermichaux@gmail.com
Guest
Posts: n/a
 
      03-19-2006
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

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      03-19-2006
http://www.velocityreviews.com/forums/(E-Mail 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.


--
Rob
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Templates - Garbage In Garbage Not Out ramiro_b@yahoo.com C++ 1 07-25-2005 04:48 PM
Memory Usage and Garbage Collection Andy ASP .Net 6 03-05-2004 09:08 AM
ASP.NET Session State Server and garbage collection =?Utf-8?B?RnJhbms=?= ASP .Net 5 01-31-2004 03:51 PM
Garbage Collection and Manage Code? Laser Lu ASP .Net 5 01-27-2004 03:48 AM



Advertisments