drgdrg
asked on
Memory leak with Ajax?
I'm sorry for the overlap with ColdFusion but we have not been able to get any answers there....
We're using CFAJAX and javascript to update a page every X seconds with information. As a test, we have a page that just updates itself with the time, being pulled via AJAX from another script.
Each time it runs, IE consumes more memory until it finally explodes....
We're doing an implementation of AJAX with a test script, based on the engine found at http://www.indiankey.com/cfajax/project.asp
and a modified version of the script from coldfusionjournal (Sept 2005, "What's Ajax, Part 1")
The code is simple - it triggers an AJAX request to a CF script that returns the current time. For testing purposes, we set a javascript setTimeout
to loop every 50 milliseconds to update the date/time. The problem is that each request increases the amount of RAM being used on
the desktop machine by IE until the machine grinds to a hault.
We are NOT concatonating the new value to an old value (ie. building a huge string). Each request replaces the value.
Here's the script:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">_cf scriptLoca tion="mode l.cfm";</s cript>
<script type="text/javascript" src="core/engine.js"></scr ipt>
<script type="text/javascript">
function getGreetings() {
DWREngine._execute(_cfscri ptLocation , null, 'getGreetings', getGreetings_result);
}
function getGreetings_result(msg) {
immsg.innerHTML = msg;
setTimeout('getGreetings() ',50);
}
</script>
</head>
<body onload="getGreetings();">
<div id="immsg" style="height:30px;"></div >
</body>
</html>
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
... and here is what it is calling:
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- --
<cfinclude template="core/cfajax.cfm" >
<cffunction name="getGreetings" returntype="string">
<cfreturn "The time is " & timeformat(now(),'h:MM:SS tt') & ", Greetings." />
</cffunction>
Any suggestions? In the real world we are not going to be checking time every 50 ms, but we may be checking items every 10-15 seconds and have one screen up for hours or more.
Is this a known issue with AJAX and IE? The ColdFusion portions are all server side; the memory being consumed is client side.
Thanks
We're using CFAJAX and javascript to update a page every X seconds with information. As a test, we have a page that just updates itself with the time, being pulled via AJAX from another script.
Each time it runs, IE consumes more memory until it finally explodes....
We're doing an implementation of AJAX with a test script, based on the engine found at http://www.indiankey.com/cfajax/project.asp
and a modified version of the script from coldfusionjournal (Sept 2005, "What's Ajax, Part 1")
The code is simple - it triggers an AJAX request to a CF script that returns the current time. For testing purposes, we set a javascript setTimeout
to loop every 50 milliseconds to update the date/time. The problem is that each request increases the amount of RAM being used on
the desktop machine by IE until the machine grinds to a hault.
We are NOT concatonating the new value to an old value (ie. building a huge string). Each request replaces the value.
Here's the script:
--------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">_cf
<script type="text/javascript" src="core/engine.js"></scr
<script type="text/javascript">
function getGreetings() {
DWREngine._execute(_cfscri
}
function getGreetings_result(msg) {
immsg.innerHTML = msg;
setTimeout('getGreetings()
}
</script>
</head>
<body onload="getGreetings();">
<div id="immsg" style="height:30px;"></div
</body>
</html>
--------------------------
... and here is what it is calling:
--------------------------
<cfinclude template="core/cfajax.cfm"
<cffunction name="getGreetings" returntype="string">
<cfreturn "The time is " & timeformat(now(),'h:MM:SS tt') & ", Greetings." />
</cffunction>
Any suggestions? In the real world we are not going to be checking time every 50 ms, but we may be checking items every 10-15 seconds and have one screen up for hours or more.
Is this a known issue with AJAX and IE? The ColdFusion portions are all server side; the memory being consumed is client side.
Thanks
I'm not familiar with CF, but what is likely happenning is that a new request object is created each time getGreetings() is called. I had a problem similar to this.
What would be easier on the RAM is creating 1 request object, then simply setting the .open() and then .send() each time the function is called. I'm not sure if the library you are using can do that though.
<script type="text/javascript">
var req = new RequestObject() // changes depending on IE6, IE5- or FF/Opera/Safari
req.onreadystatechange = function() {
if (this.readyState == 4 && (this.status == 200 || this.status == 304)) { // 304 means OK, but pulled from cache
getGreetings_result(this.d ocumentEle ment.first Child.node Value)
};
};
function getGreetings() {
req.open(method,_cfscriptL ocation,tr ue);
req.send();
};
function getGreetings_result(msg) {
immsg.innerHTML = msg;
setTimeout('getGreetings() ',50);
};
</script>
I haven't tested this code, but it's basically what I think you need.
What would be easier on the RAM is creating 1 request object, then simply setting the .open() and then .send() each time the function is called. I'm not sure if the library you are using can do that though.
<script type="text/javascript">
var req = new RequestObject() // changes depending on IE6, IE5- or FF/Opera/Safari
req.onreadystatechange = function() {
if (this.readyState == 4 && (this.status == 200 || this.status == 304)) { // 304 means OK, but pulled from cache
getGreetings_result(this.d
};
};
function getGreetings() {
req.open(method,_cfscriptL
req.send();
};
function getGreetings_result(msg) {
immsg.innerHTML = msg;
setTimeout('getGreetings()
};
</script>
I haven't tested this code, but it's basically what I think you need.
ASKER
It seems to be a glitch in the Ajax engine - there are many out there. We found a tag wddxAJAX which uses a different engine and it has no memory leak. Don't be misled by the name - you don't have to do everything in wddx or xml.... But the tag works really well and seems to have no memory issues.
Thanks anyway
Thanks anyway
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
function getGreetings_result(msg) {
immsg.innerHTML = msg;
msg = null;
setTimeout('getGreetings()
}