IE memory leak, how to resolve?  | Member | | Join Date: Jul 2007
Posts: 106
| |
I am working on an ajax chat application, done most of the work but when i tested it on IE (both on IE6 and IE7), found that there is a memory leak. The application works fine on FF. How can i fix this?
I have used ajax in the following way (this is a similar to how i am using ajax in my chat application); incrementer.php file on line # 34 simply prints an incremented value from the session -
<html>
-
<head>
-
<script language="javascript">
-
function getAjaxObject(){
-
try{
-
ajaxRequest = new XMLHttpRequest();
-
} catch (e){
-
// Internet Explorer Browsers
-
try{
-
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
} catch (e) {
-
try{
-
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
} catch (e){
-
// Something went wrong
-
alert("Your browser doesn't support ajax!");
-
return false;
-
}
-
}
-
}
-
return ajaxRequest;
-
}
-
function startCounting(){
-
var a=getAjaxObject();
-
a.onreadystatechange = function(){
-
if(a.readyState==4){
-
var text=a.responseText;
-
var e=document.getElementById('val');
-
if(e){
-
e.innerHTML=text;
-
}
-
}
-
}
-
a.open("GET", "incrementer.php", true);
-
a.send(null);
-
setTimeout(startCounting,1);
-
}
-
</script>
-
</head>
-
-
<body>
-
<input type="button" value="Send Object" onclick="startCounting()" />
-
<div id="val"> </div>
-
</body>
-
</html>
-
-
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: IE memory leak, how to resolve?
1 millisecond is a bit too small, isn't it?
Try using something like IEDrip to detect the leak. It is often caused by circular references, particularly closures - see link.
|  | Member | | Join Date: Jul 2007
Posts: 106
| | | re: IE memory leak, how to resolve? Quote:
Originally Posted by acoder 1 millisecond is a bit too small, isn't it?
Try using something like IEDrip to detect the leak. It is often caused by circular references, particularly closures - see link. Thanks for your reply. Setting the interval to 1 millisecond i can see quick increase in the use of memory.
I am unable to trace circular reference in my code. If there is any, please point to that & and how can i resolve this
|  | Member | | Join Date: Jul 2007
Posts: 106
| | | re: IE memory leak, how to resolve?
I have found a solution which leads to another problem. The solution is given below, i have got rid of inner function (see line # 38 ). Now there is an error at line# 28 as i have no access to variable 'a' within updateText The problem is how can i access variable 'a' in the updateText function. -
<!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>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
<title>Untitled Document</title>
-
<script language="javascript">
-
var x=0;
-
function getAjaxObject(){
-
try{
-
ajaxRequest = new XMLHttpRequest();
-
} catch (e){
-
// Internet Explorer Browsers
-
try{
-
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
} catch (e) {
-
try{
-
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
} catch (e){
-
// Something went wrong
-
alert("Your browser doesn't support ajax!");
-
return false;
-
}
-
}
-
}
-
return ajaxRequest;
-
}
-
function updateText(){
-
if(a.readyState==4){
-
var text=a.responseText;
-
var e=document.getElementById('val');
-
if(e){
-
e.innerHTML=text;
-
}
-
}
-
}
-
function startCounting(){
-
var a=getAjaxObject();
-
a.onreadystatechange = updateText;
-
a.open("GET", "incrementer.php", true);
-
a.send(null);
-
setTimeout(startCounting,1);
-
}
-
</script>
-
</head>
-
-
<body>
-
<input type="button" value="Send Object" onclick="startCounting()" />
-
<div id="val"> </div>
-
</body>
-
</html>
-
-
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: IE memory leak, how to resolve?
One solution is to make 'a' global. If you don't want to do that, you can use a closure.
|  | Member | | Join Date: Jul 2007
Posts: 106
| | | re: IE memory leak, how to resolve? Quote:
Originally Posted by acoder One solution is to make 'a' global. If you don't want to do that, you can use a closure. I can't use global variable and i have no idea how to use a closure. Please give me sample code
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: IE memory leak, how to resolve?
Basically creating a function which returns a function, e.g. - function updateText2(a) {
-
return function() {
-
updateText(a);
-
}
-
}
and calling that instead. See this link for an explanation of closures.
|  | Member | | Join Date: Jul 2007
Posts: 106
| | | re: IE memory leak, how to resolve?
I have used closure, but the problem persists. Please see the code below what i am doing wrong -
<html>
-
<head>
-
<script language="javascript">
-
var x=0;
-
function getAjaxObject(){
-
try{
-
ajaxRequest = new XMLHttpRequest();
-
} catch (e){
-
// Internet Explorer Browsers
-
try{
-
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
-
} catch (e) {
-
try{
-
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
-
} catch (e){
-
// Something went wrong
-
alert("Your browser doesn't support ajax!");
-
return false;
-
}
-
}
-
}
-
return ajaxRequest;
-
}
-
function updateText(a){
-
return function(){
-
if(a.readyState==4){
-
var text=a.responseText;
-
var e=document.getElementById('val');
-
if(e){
-
e.innerHTML=text;
-
}
-
}
-
};
-
}
-
function startCounting(){
-
var a=getAjaxObject();
-
var fun1=updateText(a);
-
a.onreadystatechange = fun1;
-
a.open("GET", "incrementer.php", true);
-
a.send(null);
-
setTimeout(startCounting,1);
-
}
-
</script>
-
</head>
-
-
<body>
-
<input type="button" value="Start Counting" onclick="startCounting()" />
-
<div id="val"> </div>
-
</body>
-
</html>
-
|  | Expert | | Join Date: Jun 2007 Location: Urbana IL
Posts: 411
| | | re: IE memory leak, how to resolve? -
-
function startCounting(){
-
var a=getAjaxObject();
-
a.onreadystatechange = function(){
-
if(a.readyState==4){
-
var text=a.responseText;
-
var e=document.getElementById('val');
-
if(e){
-
e.innerHTML=text;
-
}
-
a = null;
-
}
-
}
-
a.open("GET", "incrementer.php", true);
-
a.send(null);
-
setTimeout(startCounting,1);
-
}
-
|  | Member | | Join Date: Jul 2007
Posts: 106
| | | re: IE memory leak, how to resolve?
Yes, that works. Great job man, thank you very much
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 14,581
| | | re: IE memory leak, how to resolve?
So simple that I didn't think of. Thanks rnd me ;)
|  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|