 | 
September 5th, 2008, 05:55 AM
|  | Member | | Join Date: Jul 2007
Posts: 102
| | IE memory leak, how to resolve?
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>
-
-
| 
September 5th, 2008, 03:50 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,727
| |
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.
| 
September 6th, 2008, 05:26 AM
|  | Member | | Join Date: Jul 2007
Posts: 102
| | 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
| 
September 6th, 2008, 06:47 AM
|  | Member | | Join Date: Jul 2007
Posts: 102
| |
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>
-
-
| 
September 6th, 2008, 12:07 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,727
| |
One solution is to make 'a' global. If you don't want to do that, you can use a closure.
| 
September 7th, 2008, 01:11 PM
|  | Member | | Join Date: Jul 2007
Posts: 102
| | 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
| 
September 7th, 2008, 01:28 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,727
| |
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.
| 
September 8th, 2008, 04:10 AM
|  | Member | | Join Date: Jul 2007
Posts: 102
| |
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>
-
| 
September 8th, 2008, 06:37 AM
|  | Expert | | Join Date: Jun 2007 Location: Urbana Il, home of javascript. Age: 27
Posts: 360
| | -
-
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);
-
}
-
| 
September 8th, 2008, 07:02 AM
|  | Member | | Join Date: Jul 2007
Posts: 102
| |
Yes, that works. Great job man, thank you very much
| 
September 8th, 2008, 12:39 PM
|  | Site Moderator | | Join Date: Nov 2006 Location: UK
Posts: 12,727
| |
So simple that I didn't think of. Thanks rnd me ;)
|  |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
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 network members.
|