473,609 Members | 2,766 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9360
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
16805
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 create a directory on my Windows box, and set chmod 777 on it (that should be full access for everyone if my memory serves me correctly), but when I want to rmdir that directory, I get a permission denied message (I can delete contents from that...
4
4200
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 implementation usage of these concepts? Where these concepts will be used. Please provide some illustration (real-time), so that it can be easily comprehended. Thanks, wittyGuy
11
23047
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 another function, I want to test if an object is assigned to that
27
3821
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 failed assertions for assertions that at first thought ought to hold, but that's not important.) At the end is a full program containing the above seemingly
72
4174
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 c?
7
3385
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
12453
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 delete operator. Let me use an example to illustrate this: /********************************************************/ #include <new> #include <iostream>
30
2445
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 carrying the original pointer. In other words, for the simplest usage code: * no overhead (just carrying a pointer or two), and * no possibility of exceptions (for that case).
30
3808
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 the end of the app for example: class test { public: int x;
0
8112
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8552
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6975
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6044
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4006
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1636
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.