new & delete usage question

E

Eric

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
 
R

Richard Cornford

Eric said:
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?

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.
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?

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.
 
E

Eric

Richard said:
Eric said:
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?

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.
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?

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.

OK, thanks
Eric
 

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,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top