Garbage Collection and User Interfaces?

Discussion in 'Javascript' started by petermichaux@gmail.com, Mar 19, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. RobG Guest

    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
    RobG, Mar 19, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laser Lu

    Garbage Collection and Manage Code?

    Laser Lu, Jan 26, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    710
    Gaurav Khanna [C# MVP]
    Jan 27, 2004
  2. =?Utf-8?B?RnJhbms=?=

    ASP.NET Session State Server and garbage collection

    =?Utf-8?B?RnJhbms=?=, Jan 29, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    3,563
    Alvin Bruney [MVP]
    Jan 31, 2004
  3. Andy
    Replies:
    6
    Views:
    3,668
  4. Replies:
    1
    Views:
    437
    mrstephengross
    Jul 25, 2005
  5. Øyvind Isaksen
    Replies:
    1
    Views:
    960
    Øyvind Isaksen
    May 18, 2007
Loading...

Share This Page