new & delete usage question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric

    new & delete usage question

    Hi,
    I'm used to C/C++ where if you "new" something you really need to "delete"
    it later. Is that also true in javascript? if i do "mydate = new date();"
    in a function and dont "delete mydate" when the function exits do i have a
    memory leak or other trouble brewing?

    ie:
    function MyFn() {
    var mydate;

    mydate = new date();
    }


    does the above function present a problem (what if its called over and over
    and over etc)? or is the allocation automaticaly freed on function exit?

    Thanks
    Eric
  • Richard Cornford

    #2
    Re: new & delete usage question

    Eric wrote:[color=blue]
    > I'm used to C/C++ where if you "new" something you really need to
    > "delete" it later. Is that also true in javascript? if i do "mydate =
    > new date();" in a function and dont "delete mydate" when the function
    > exits do i have a memory leak or other trouble brewing?[/color]

    Javascript uses automatic garbage collection, you cannot explicitly
    destroy an object. All you can do is arrange that there are no
    references to that object held in the properties of other objects (which
    includes global variables as they are properties of the global object)
    and leave it to the garbage collection to recognise that the object is
    no longer accessible and so free it.

    delete - in javascript removes a property form an object (along with its
    value) but even if that property referred to an object deleting the
    property that held a reference to it will not affect the object itself.
    Except if that reference was the last reference remaining, at which
    point (at least, at some future point) the garbage collector should
    recognise that the object is inaccessible and destroy it.

    It is best to remove references to object that are no longer needed so
    that the garbage collector is free to act upon them. Removing the
    references to objects does not necessarily (or usually) involve deleting
    properties that had referred to the object, instead assigning - null -
    to the properties has the desired effect.
    [color=blue]
    > ie:
    > function MyFn() {
    > var mydate;
    >
    > mydate = new date();
    > }
    > does the above function present a problem (what if its called over
    > and over and over etc)? or is the allocation automaticaly freed on
    > function exit?[/color]

    When execution leaves the "execution context" of the above function the
    reference to Activation/Variable object that holds the local variables
    (as its properties), including - mydate -, can be freed[1] and so the
    system should recognise that the reference to Date object is no longer
    accessible, and that there are no other references to that Date object,
    and so the Date object should also become available for garbage
    collection. Your example function should not cause problems.

    Garbage collection systems in web browsers are notoriously inefficient
    and low priority. But, with the exception of IE[2], they usually manage
    to clean up everything remaining once any given page is unloaded.

    Richard.

    [1] The Activation/Variable object can only be freed if the execution of
    the function does not form a closure. Which your example function does
    not.

    [2] IE's garbage collection fails if a _circular_ set of references is
    left that includes any DOM nodes or ActiveX objects. The consumed memory
    is not freed until the browser is shut down. Circular references
    exclusively between JScript objects can be handled successfully. Forming
    closures is the easiest method of provoking this problem, though not the
    only method.


    Comment

    • Eric

      #3
      Re: new & delete usage question

      Richard Cornford wrote:
      [color=blue]
      > Eric wrote:[color=green]
      >> I'm used to C/C++ where if you "new" something you really need to
      >> "delete" it later. Is that also true in javascript? if i do "mydate =
      >> new date();" in a function and dont "delete mydate" when the function
      >> exits do i have a memory leak or other trouble brewing?[/color]
      >
      > Javascript uses automatic garbage collection, you cannot explicitly
      > destroy an object. All you can do is arrange that there are no
      > references to that object held in the properties of other objects (which
      > includes global variables as they are properties of the global object)
      > and leave it to the garbage collection to recognise that the object is
      > no longer accessible and so free it.
      >
      > delete - in javascript removes a property form an object (along with its
      > value) but even if that property referred to an object deleting the
      > property that held a reference to it will not affect the object itself.
      > Except if that reference was the last reference remaining, at which
      > point (at least, at some future point) the garbage collector should
      > recognise that the object is inaccessible and destroy it.
      >
      > It is best to remove references to object that are no longer needed so
      > that the garbage collector is free to act upon them. Removing the
      > references to objects does not necessarily (or usually) involve deleting
      > properties that had referred to the object, instead assigning - null -
      > to the properties has the desired effect.
      >[color=green]
      >> ie:
      >> function MyFn() {
      >> var mydate;
      >>
      >> mydate = new date();
      >> }
      >> does the above function present a problem (what if its called over
      >> and over and over etc)? or is the allocation automaticaly freed on
      >> function exit?[/color]
      >
      > When execution leaves the "execution context" of the above function the
      > reference to Activation/Variable object that holds the local variables
      > (as its properties), including - mydate -, can be freed[1] and so the
      > system should recognise that the reference to Date object is no longer
      > accessible, and that there are no other references to that Date object,
      > and so the Date object should also become available for garbage
      > collection. Your example function should not cause problems.
      >
      > Garbage collection systems in web browsers are notoriously inefficient
      > and low priority. But, with the exception of IE[2], they usually manage
      > to clean up everything remaining once any given page is unloaded.
      >
      > Richard.
      >
      > [1] The Activation/Variable object can only be freed if the execution of
      > the function does not form a closure. Which your example function does
      > not.
      >
      > [2] IE's garbage collection fails if a _circular_ set of references is
      > left that includes any DOM nodes or ActiveX objects. The consumed memory
      > is not freed until the browser is shut down. Circular references
      > exclusively between JScript objects can be handled successfully. Forming
      > closures is the easiest method of provoking this problem, though not the
      > only method.[/color]

      OK, thanks
      Eric

      Comment

      Working...