Unfortunately using IE I have no plugins installed except flash. I also use drip-05.exe (a windows browser that checks for leaks). I also don't use any plugins on firefox.
Printable View
Unfortunately using IE I have no plugins installed except flash. I also use drip-05.exe (a windows browser that checks for leaks). I also don't use any plugins on firefox.
Have you monitored memory usage over the period of an hour? Browsers don't immediately reclaim memory used by JavaScript. It's actually perfectly natural to see memory usage go, then back down again. How long have you monitored the browser?
This is very interesting. I played around with it and learned some things. Basically, you are trying to kill or replace the function by altering the HTML tags that defined it. This doesn't work, unless you change the src property to something different (I think) Look at this code and play with the buttons and you will see what I mean. The javascript functions defined within the script tags persist even when the code in the script tags which defined them are purged from the document.
Code:<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
function killer(){ // this destroys the script tags with the id of "dead"
var q = document.getElementsByTagName('script');
alert("There are: " + q.length + " script tag(s) in this document");
var x = document.getElementById("dead");
if(x == (null || undefined)){
alert("I DIDN'T FIND ANY ELEMENT WITH THAT ID--THE TAG IS GONE FROM THE DOCUMENT");
return;
}
x.parentNode.removeChild(x);
// you would think the "dead" function was gone---it isn't
}
</script>
<script type="text/javascript" id="dead">
function dead(){alert("I AM COMING FROM THE FUNCTION YOU ARE TRYING TO KILL");}
</script>
</head>
<body>
<input type="button" onclick="dead()" value = "Fire function that you trying to kill"/><br />
<input type="button" onclick = "killer()" value="Try to kill that function" /><br />
<p>Make sure to click on the last button at least twice. The second time it will indicate that there is only one script element in the document. The first time it indicates there are two. But the function that was defined by the eliminated script tags does not die.</p>
</body>
</html>
My understanding is that comments do indeed take up space in javascript, and that is one reason why they should be removed for the actual workhorse code. What I also find interesting is that the javascript here is enclosed within a SPAN tag. It might be interesting to see the number of childnodes it has as the setInterval progresses.
tcobb, since javascript is reference memory based, I think the button is holding a reference to the function. Just a guess. If you set the button onclick method to null in killer, it should help. I may try it to see.
In my case, I am adding a purely dynamic entity that does not take up space in the original HTML, and am trying then to remove it.
I find this all very curious.
I think the route you're going now is the way to go. That's the way I would have gone, but I thought the approach you took was very fascinating. I know I didn't help you, but by trying to deal with your problem it did help me with my understanding of Javascript, for which I thank you. And just for cheap thrills, you might be interested in this as regards to dynamically created scripts:
That is what I really like about such forums as this. By trying to solve other people's problems we can all learn a lot. I'm sorry I couldn't help you with yours but trying to help you solve yours did teach me a lot.Code:<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
window.onload= function(){
setTimeout(doIt, 2000);
}
function doIt(){
var x = document.getElementById("s1"); // target the element to which the script tag is a child node
x.parentNode.removeChild(x); //now kill it and its child the SCRIPT tag
x = null; //now drive a stake in its heart
var str = 'setTimeout(zap, 5000)'; // the setTimeout process does not exist yet so there is no reference to bind to
// its just a string
eval(str); //now the setTimeout process DOES exist, coming into existence after what it is supposed to bind to has been officially killed
}
</script>
</head>
<body>
<span id = "s1">Here is the span with script within it. I'll be dead in two seconds.
<script type="text/javascript">
function zap(){alert("HERE--I'M GENERATED BY THE SCRIPT THAT'S SUPPOSED TO BE DEAD.");}
</script>
</span>
<p>The moral of the story, so far as I can see it, is that javascript code that is set by the code between script tags cannot be killed by deleting the script tags that put it into existence. It will still be there. The alert box of the function
that is supposed to have been destroyed will pop up soon.</p>
</body>
</html>
--Thanks
I was looking for a solution to the same problem. I read this thread and others, and I believe I found a solution here:
http://ajaxian.com/archives/dynamic-...d-memory-leaks
I hope it doesn't come too late for you.
P.S. I haven't tested this solution yet, but it makes some kind of sense... Good luck!