473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jul 20 '05 #1
2 9343
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Daniel | last post by:
Hi, From what I read from the PHP manual, chmod on a Windows platform should have no effect, and that seems totally normal (unless someone on sourceforge has a windows port of that!). I...
4
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real...
11
by: Squid Seven | last post by:
I create a pointer to an item: CardSession *cardSession; Then, later, I use new to create an instance of the item and assign it to that pointer: cardSession = new CardSession(); In...
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
7
by: Scott Schluer | last post by:
Is there a way to use the Image class to convert a color photo (GIF or JPEG) to a B&W photo? Thanks, Scott
6
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
30
by: Alf P. Steinbach | last post by:
I once suggested in that SomeOne Else(TM) should propose a string value class that accepted literals and char pointers and so on, with possible custom deleter, and in case of literal strings just...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.