By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

new & delete usage question

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Eric wrote:
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.
Jul 20 '05 #2

P: n/a
Richard Cornford wrote:
Eric wrote:
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
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.