Click to See Complete Forum and Search --> : memory leaking simple script


krisvdc
07-01-2007, 12:21 PM
Hi all,

I am working on some simple json fetching javascript. Seems now it is leaking memory faster than I can buy it ;)

Does anybody see why ?

Thanks a lot,


Kris



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script>
var sequenceNr = 0;
var requestObject = null;
function Request(url){
header = document.getElementsByTagName("head").item(0);
enclosure = document.createElement("script");
enclosure.type = "text/javascript";
enclosure.charset = "utf-8";
enclosure.src = url + "&id=" + sequenceNr++;
enclosure.id = "streamer" + sequenceNr;
};
Request.prototype.removeEnclosure = function () {
header.removeChild(enclosure);
enclosure = null;
header = null;
};
Request.prototype.addEnclosure = function () {
header.appendChild(enclosure);
};
function load() {
connect();
}
function connect() {
requestObject = new Request('http://192.168.1.3:6969/refresh.html');
//this returns for example : callback({"Total":339});reconnect();
requestObject.addEnclosure();
}
function reconnect() {
requestObject.removeEnclosure();
requestObject = null;
connect();
}
function setCount(data) {
document.getElementById('Total').innerHTML = data.Total;
}
function callback(data) {
setCount(data);
}
</script>
</head>
<BODY onLoad="load()">
<span id="counter">
<span class="title">Counter: </span><span id="Total">0</span><br/>
</span>
</BODY>
</HTML>

Banana Ananda
07-01-2007, 02:47 PM
I haven't tested the script yet, but the first thing a noticed is that the function, Request is treated like a constructor, with protototyped methods, and is executed using the new operator, yet all the information is kept in global variables. Is this intentional ? Could it have something to do with the problem ?

krisvdc
07-01-2007, 03:06 PM
It is the result of my messing around with that code, I have spend two days trying to find those leaks. I narrowed it down as much as possible.

Thanks for your time/expertise !

Kris

\\.\
07-01-2007, 03:08 PM
I read an article on some Memory leak issues which can often be resolved by a return; at the end of a function even if it does not return anything, you need to return control back to the main function in order for that block of memory that was in use to be reallocated.

krisvdc
07-01-2007, 03:23 PM
Nope, doesn't seem to help. Too bad ;)
I think it never deletes the result of what it gets back from the server, due to some link somewhere.

Sorry about the double post, I was a bit confused :o

Kris

Banana Ananda
07-01-2007, 04:44 PM
I tried on a local server. I had to introduce a setTimeout (1sec), because the tight loop was crashing the browser within 6-7 calls.

There was a little leak, even with the timer. The script tags are being removed. though.

I tried a version that just uses a single SCRIPT tag. Instead of removing & creating, the single tag's src was changed (with the incremented counter, and the time). This worked perfectly in IE, with no apparent leak.

One conclusion could be that the browser isn't effectively freeing up the memory associated with the created SCRIPT tags, even though there is no reference to them - not giving them unique ids might solve this.

Unfortunately the "single tag" approach didn't work in Firefox.

krisvdc
07-01-2007, 05:33 PM
I guess there is no other way to do the same thing, but with no leaks ?

I had a look at pretty much everything by now : XMLHttpRequest is not cross-domain, iframes are not strict xhtml2, flash or applets are also not an option due to firewall restrictions.

Banana Ananda
07-01-2007, 05:57 PM
You could cover IE with the single tag technique. It doesn't seem to leak at all. That's most people covered (The others can leak).

I think there's a way with images and cookies.

iframes are not strict xhtml2

(Browsers don't use validating parsers anyway, but ..)

I don't think that's true. If being valid matters, then you can add the iframes DTD.

krisvdc
07-02-2007, 07:06 AM
How did you get that single script tag to work ?

My code doesn't upgrade at all :(

Kris

Banana Ananda
07-02-2007, 07:31 AM
I have attached the files.
- I left it in its "was object oriented, but currently not" state.

krisvdc
07-02-2007, 08:17 AM
setTimeout did the trick. But it is no longer what i was hoping for. The browser shows all the back end stuff in the status bar.

Kris

Banana Ananda
07-02-2007, 09:40 AM
The setTimeout isn't the intended change. It's only there because I was testing from a local server. It doesn't remove the memory leak in the original version either.

Remove the setTimeout, run it normally, and see what you think (IE only though).