473,769 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dynamically creating/deleting content and memory leaks

If I code something like the following it results in a memory leak in IE
(as Leak 0.5 tells me):

var frm = document.create Element("FORM") ;
document.body.a ppendChild(frm) ;
fDeleteForm = function() {
document.body.r emoveChild(frm) ;
};

setTimeout( fDeleteForm, 100);
Is there a way to add and subsequently delete content without it causing
a memory leak in IE?

Andrew Poulos
Feb 9 '07 #1
6 3631
On Feb 8, 10:14 pm, Andrew Poulos <ap_p...@hotmai l.comwrote:
If I code something like the following it results in a memory leak in IE
(as Leak 0.5 tells me):

var frm = document.create Element("FORM") ;
document.body.a ppendChild(frm) ;

fDeleteForm = function() {
document.body.r emoveChild(frm) ;
};

setTimeout( fDeleteForm, 100);
You are calling a "global" variable (frm) inside fDeleteForm.
That's probably what's causing the memory leak.

Feb 9 '07 #2
On Feb 8, 4:14 pm, Andrew Poulos <ap_p...@hotmai l.comwrote:
If I code something like the following it results in a memory leak in IE
(as Leak 0.5 tells me):

var frm = document.create Element("FORM") ;
document.body.a ppendChild(frm) ;

fDeleteForm = function() {
document.body.r emoveChild(frm) ;

};

setTimeout( fDeleteForm, 100);

Is there a way to add and subsequently delete content without it causing
a memory leak in IE?

Andrew Poulos
Hi Andrew,

I have been recently trying to understand these memory leaks and I
think I understand yours. Please, if anyone sees fault in my
explanation, tell me so that we might all be further educated.

You have a variable referencing a DOM node. In IE, the DOM acts as
another variable. So, in your example, you have two references to the
same memory space: the form element you have created. Since, when you
remove it from the DOM you still have a reference to the created form
element via the variable frm, the Garbage Collector in IE does not
think this needs to be collected. If you wanted that memory space to
be released then set frm=null when you remove the element.

I suppose this is not a real memory leak since the data is not in an
orphaned space. It would be a leak if you had your example code in a
loop.

Feb 9 '07 #3
VK
On Feb 9, 7:54 pm, "getburl" <adampbr...@gma il.comwrote:
Please, if anyone sees fault in my
explanation, tell me so that we might all be further educated.

You have a variable referencing a DOM node. In IE, the DOM acts as
another variable.
No, but an element with ID attribute set will create global variable
with the same name as ID holding a reference to the said element. An
ugly IE's invention causing a bounch of hard-to-trace post-effects.
Alas adopted by many other UAs because of ugly written ASP pages
pollution on the Web. So they had to cope with the requirement "make
it compatible with..." It is not in direct relevance to the case
though.
So, in your example, you have two references to the
same memory space: the form element you have created.
No, just one reference JScript variable DOM element
Since, when you
remove it from the DOM you still have a reference to the created form
element via the variable frm, the Garbage Collector in IE does not
think this needs to be collected.
It does think that it should be collected, but it's directly
prohibited to do it. This kind of memory leak on IE is really special
because it's not caused by some engine failure - but directly
programmed by request to improve stability..
See
<http://groups.google.com/group/comp....avascript/msg/
1acf81f7d24d2ba 0>
for details.
In summary this leak is half-due to bad JavaScript programmers. If
they would follow specs and common sense and wouldn't create orphan
scripts - then Microsoft wouldn't have to fight with crashes caused by
scripting phantom DOM Tree out of orphan scripts.
>From the other hand if Microsoft would think in advance about Web
Application, kiosk mode etc. then they maybe choose some more elegant
fix than that.
If you wanted that memory space to
be released then set frm=null when you remove the element.
Amen!
Feb 9 '07 #4
On Feb 9, 9:20 am, "VK" <schools_r...@y ahoo.comwrote:
On Feb 9, 7:54 pm, "getburl" <adampbr...@gma il.comwrote:
Please, if anyone sees fault in my
explanation, tell me so that we might all be further educated.
You have a variable referencing a DOM node. In IE, the DOM acts as
another variable.

No, but an element with ID attribute set will create global variable
with the same name as ID holding a reference to the said element. An
ugly IE's invention causing a bounch of hard-to-trace post-effects.
Alas adopted by many other UAs because of ugly written ASP pages
pollution on the Web. So they had to cope with the requirement "make
it compatible with..." It is not in direct relevance to the case
though.
So, in your example, you have two references to the
same memory space: the form element you have created.

No, just one reference JScript variable DOM element
Since, when you
remove it from the DOM you still have a reference to the created form
element via the variable frm, the Garbage Collector in IE does not
think this needs to be collected.

It does think that it should be collected, but it's directly
prohibited to do it. This kind of memory leak on IE is really special
because it's not caused by some engine failure - but directly
programmed by request to improve stability..
See
<http://groups.google.com/group/comp....avascript/msg/
1acf81f7d24d2ba 0>
for details.
In summary this leak is half-due to bad JavaScript programmers. If
they would follow specs and common sense and wouldn't create orphan
scripts - then Microsoft wouldn't have to fight with crashes caused by
scripting phantom DOM Tree out of orphan scripts.>From the other hand if Microsoft would think in advance about Web

Application, kiosk mode etc. then they maybe choose some more elegant
fix than that.
If you wanted that memory space to
be released then set frm=null when you remove the element.

Amen!
VK,

Thank you for taking the time to correct the parts of my explanation
that were incorrect. It seems you left out enough to make me follow
your links and search on the terms you used. The MSDN article was very
interesting. If not for the value of the IE specific closure issues,
it illustrated the type of thinking that leads to buggy software
development.

getburl

Feb 9 '07 #5
VK
On Feb 9, 9:44 pm, "getburl" <adampbr...@gma il.comwrote:
Thank you for taking the time to correct the parts of my explanation
that were incorrect.
You are welcome.
What just came to my mind: the relevant "fix" was made approx 2003,
with IE 6 SP1 mass distribution. Undocumented CollectGarbage( ) method
in JScript is traced down to the same time IMO. Possibly this method
was added exactly for that fix, so "informed corporate customers"
could break the new leak.

That would be interesting to see if
CollectGarbage( );
instead of
frm = null;
breaks this kind of leak.

Feb 9 '07 #6
On Feb 9, 10:14 am, Andrew Poulos <ap_p...@hotmai l.comwrote:
If I code something like the following it results in a memory leak in IE
(as Leak 0.5 tells me):

var frm = document.create Element("FORM") ;
document.body.a ppendChild(frm) ;

fDeleteForm = function() {
document.body.r emoveChild(frm) ;

};

setTimeout( fDeleteForm, 100);

Is there a way to add and subsequently delete content without it causing
a memory leak in IE?
I think this thread explains it (despite the apparently irrelevant
subject):

"menus in JS or CSS - pros? cons?"
<URL: http://groups.google.com.au/group/comp.lang.javascript/
browse_frm/thread/15db740598c92a/89e410566f8b6b9 8?lnk=gst&q=ric hard
+cornford+circu lar+reference&r num=1&hl=en#89e 410566f8b6b98 >

The issue isn't simply closures or adding/removing nodes. It's about
circular references involving DOM nodes, usually created by adding
event hanlders.

--
Rob
Feb 9 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1654
by: Abhijeet | last post by:
I was just toying around idea of deleting this from a member function. Was expecting that any acess to member variable or function after deleting sould give me dump(segmetation violation).Cause now I am trying to access freed. memory. Is my assumption correct? I tried following code --------------------------------------------------- #include <iostream.h>
3
3612
by: Tony Johansson | last post by:
Hello Experts!! When you instansiate varaibles(object) you can do so in four different scops which are. Within a block which is called Local or block scope . Within a function which is called function scope. Whithin a class which is called class scope. Outside a function which is called global or file scope. Now to my question when you allocate memory dynamically you can't say
5
3502
by: Sam777 | last post by:
I was under the impression that creating the app_offline.htm file at the root of the webapp would cause all handles to be closed so that the app could be removed. Unfortunately, this isn't the case. One handle remains open. Debugging shows that it's actually the IIS cache and not ASP.NET that owns this handle. During IIS shutdown, the handle is closed with the following stack trace: ChildEBP RetAddr 0006fbe4 5a403e05...
3
1844
by: lars.uffmann | last post by:
Hi everyone! I am debugging a big piece of code on the search for memory leaks, using g++ under suse 9.3. Since I'm trying to eliminate ALL memory leaks, I now stumbled upon a class foo that is not ever instantiated (thus no constructor or destructor ever called), but instead, all of its member variables are defined as static in the according .cpp file, and it's methods are invoked by calling foo::bar(...) to invoke method "bar(...)".
94
4770
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
3
5327
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". Anybody know anything about this? Does *Javascript* leak memeory, or does the *browser* leak memory?
8
1884
by: NAdir | last post by:
Hi, thank you for your help. My VB.Net application contains a document that the user can refresh at any time. The refresh works fine and needs to loop through few datatables (hundreds of rows). This works fine until I delete some rows in two tables. Just after the delete if I do the refresh there is a huge memory allocated and the time needed to perform the refresh increase, the memory and time continue to increase on each refresh until I...
5
1354
by: news.microsoft.com | last post by:
I'm strongly considering abandoning the one-physical-file-per-page model and going with an arcitecture that simply loads content from classes dynamically. There will be only one page that a user will go to, and all variation from navigation will come from AJAX calls to load and replace content in a main frame. .... so my concern with this is, perhaps the browsers will leak memory or do something obscurely wierd with enough loading and...
10
2583
by: Jess | last post by:
Hello, If I create a temporary object using a dynamically created object's pointer, then when the temporary object is destroyed, will the dynamically created object be destroyed too? My guess is that it's not destroyed, but I'm not sure. I have the following program: #include<iostream>
0
9589
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
10216
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
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
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
7413
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
5310
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...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
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.