Click to See Complete Forum and Search --> : How to stop a memory leak in javascript? (appendChild/removeChild)
Doug_Dyer
12-17-2002, 08:35 AM
My application performs the following on a time interval of 1/2 sec:
myTimeIntervalRoutine(objindex)
{
var myObj = objList[objIndex];
if (myObj.m_scriptNode)
myObj.m_span.removeChild(myObj.m_scriptNode);
myObj.m_scriptNode=document.createElement('script');
myObj.m_scriptNode.type='text/javascript';
myObj.m_scriptNode.src = myObj.m_url;
myObj.m_span.appendChild(myObj.m_scriptNode);
}
The script pointed to by m_url contains only a comment (in order to eliminate that script as a possibility). When I run my application, this time interval routine is the only javascript being exercised and internet explorer slowly increases in size perhaps 4k a second.
Any ideas? I don't see anything wrong with the code but Im still very new at JS.
khalidali63
12-17-2002, 09:42 AM
It seems like you are calling any method recursively and it does not stop.
The code snippet you posted here is not descriptive enough to see which element is calling which method,can paste more code including the HTML part as well.
Doug_Dyer
12-17-2002, 10:51 AM
There is no recursion.
Here is an example HTML snippet:
<HTML>
<SCRIPT>
var myObj = MyObj();
myObj.m_url = "crap.js";
window.setInterval("myTimeIntervalRoutine(1);", 500);
</SCRIPT>
</HTML>
Here is an example of the crap.js file:
//hello
just grows and grows. The MyObj is a javascript object with a few member fields.
khalidali63
12-17-2002, 11:07 AM
I guess you missed the crap.jas file
Can you please put all the code(html/javascript) here or print a url here to the actual page?
Doug_Dyer
12-17-2002, 11:48 AM
Crap.js:
//hello
(thats it)
khalidali63
12-17-2002, 11:56 AM
crap.js,,,I guess really is crap,because it is not being attached to the page..:-)
please make sure it is attached
Doug_Dyer
12-17-2002, 01:09 PM
khalidali63,
Its attached to the page in the following way (which may be wrong, I don't know):
var span = document.createElement("SPAN");
span.id = "myidtag" + this.m_index; // 0
this.m_span = span;
document.body.appendChild(span);
Crap.js gets executed without problems by adding to the myobj.m_span field. I made it all comments for now avoid any allocation issues that it may have been causing.
khalidali63
12-17-2002, 01:14 PM
heck...emial me the file
k_ali@shaw.ca
Doug_Dyer
12-18-2002, 08:01 AM
Here. Any help from anyone would be greatly appreciated.
File 1, 'Crap.js':
//hello
File 2, 'memLeak.htm':
<HTML>
<SPAN id="myScript">
<SCRIPT language="javascript">
var scriptNode = null;
function memLeak()
{
var jsFile = document.getElementById('myScript');
if (scriptNode) {
jsFile.removeChild(scriptNode);
}
scriptNode=document.createElement('script');
scriptNode.type='text/javascript';
scriptNode.src = 'crap.js';
jsFile.appendChild(scriptNode);
}
window.setInterval("memLeak();", 100);
</SCRIPT>
</SPAN>
</HTML>
chuahyen
02-06-2003, 09:37 AM
Hi
I am also having the same problem with IE5.5. The memory is leaking whenever i am using the DHTML to update the contents into the node.
Is there any suggestion on how to release memory?
Thanks
Chris
khalidali63
02-06-2003, 10:04 AM
Would you like to email me the contents html/JavaScript,
k_ali@shaw.ca
Doug_Dyer
02-06-2003, 10:18 AM
Originally posted by khalidali63
Would you like to email me the contents html/JavaScript,
k_ali@shaw.ca
Its right there (My post above his):
crap.js (which gets loaded)
memleak.htm (which loads crap.js)
The leak occurs in netscape and explorer
khalidali63
02-06-2003, 10:20 AM
Originally posted by Doug_Dyer
Here. Any help from anyone would be greatly appreciated.
File 1, 'Crap.js':
//hello
Thats all I see for crap.js?
is that all?
Khalid
Doug_Dyer
02-06-2003, 10:22 AM
Originally posted by khalidali63
Thats all I see for crap.js?
is that all?
Khalid
Yep thats it. I removed any meaningful contents so as to elminate that script as being the cause of the leak.
You can put whatever you want there, it will still leak.
khalidali63
02-06-2003, 10:37 AM
How long you have to run the script to notice a significant memory loass.,I have it running for last 5 mins,memory usage and CPU usage are normal,
Win2000 pro
khalidali63
02-06-2003, 10:45 AM
BTW I am using IE6 and NS6+/Mozilla 1.21 browsers.
I am running the script for lastmore then 10 mins.No signs of mem leak or extra CPU usage.
Did I miss anything?
Doug_Dyer
02-06-2003, 12:42 PM
I see mine slowly growing by about 4K a second with no stopping in sight (looking at the iexplorer.exe memusage in the task manager).
I let mine run here for 1 minute and it has gone from 18,600K to 20,000K with no sign of letting up. If you are impatient, you can decrease the timer for loading the script.
IE v6.0.2800
Win2k service pack 3
khalidali63
02-06-2003, 02:26 PM
Thats interesting.
while playing with it,it seems like a line of comment in the crap.js file
will increase the mem consumption by 6k,adding 4 lines of comment resulted
24k jump each time setInterval is called.???????????????????
Doug_Dyer
02-06-2003, 02:50 PM
Originally posted by khalidali63
Thats interesting.
while playing with it,it seems like a line of comment in the crap.js file
will increase the mem consumption by 6k,adding 4 lines of comment resulted
24k jump each time setInterval is called.???????????????????
Hey, maybe its the comments :) You are seeing what I am seeing though.