?
Solved

Need help cleaning up Javascript / AJAX script (memory leaks)

Posted on 2006-11-27
9
Medium Priority
?
327 Views
Last Modified: 2012-05-12
I'm using an AJAX script provided by w3schools.org, which grabs some data from an ASP page, based on the query string I send through the XMLHttpRequest.  Here is a copy of the sample Javascript code provided by w3schools.org:

var xmlHttp

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="gethint.asp"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}

function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}


This script works fine, and returns the data contained in the "gethint.asp" script, however, here is my problem:

It seems that this causes memory problems on the Windows 2003 server hosting these scripts.  They cause the IEXPLORE.EXE process on the server to keep pushing up its memory usage, causing other server components to fail as a result of "too many socket connections" errors.

MY QUESTION: How can I thoroughly clean up the object created when this javascript is executed?  I would assume that killing/clearing the "objXMLHttp" object created in the script above would do the trick, but I do not know how to do this properly.  Can somebody post some code that will either replace the JS code above, or help clean up the object (and kill that IEXPLORE.EXE process on the server)?

Thanks in advance,
RickTJ
0
Comment
Question by:RickTJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 3

Expert Comment

by:jay_gadhavi
ID: 18025913
U can try something like :


getTransport = function(){
 var __transport = new Array(
                             function(){ return XMLHttpRequest ? new XMLHttpRequest() : null },
                             function(){ return ActiveXObject  ? new ActiveXObject('Msxml2.XMLHTTP') : null },
                             function(){ return ActiveXObject  ? new ActiveXObject('Microsoft.XMLHTTP') : null }
                            );  

 for (var i = 0; i < __transport.length; i++)
   try {
     if (__transport[i]())
       return __transport[i];
   } catch(e) {}

 return function(){ return null };

}();

See..................For information

http://blogs.msdn.com/ie/archive/2006/06/08/619507.aspx
0
 

Author Comment

by:RickTJ
ID: 18025950
Thanks Jay, in your code, is it this line performing the cleanup?

 return function(){ return null };

Thanks for the article, too.  I know that the sample script above checks for the native XMLHttpRequest before it opens a new ActiveXObject, but in either case, how can I be sure that object is cleared out?

I'm looking for something similar to the "set obj = nothing" command in ASP, for good cleanup.

Thanks
RickTJ
0
 
LVL 6

Expert Comment

by:aescnt
ID: 18026114
Well jay just posted a code that's a cleaner way of returning an XMLHttpRequest object.
The leak probably happens because you don't delete() your XMLHTTP objects after use.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:RickTJ
ID: 18028617
Hello aescnt, I agree with you - that's exactly what I need.  What would need to be added to my original code (above) in order to properly delete() the XMLHTTP objects after use?  

Where would the delete() lines be placed (within the stateChanged function or the GetXmlHttpObject function)?  Or should I create another function to delete any open objects?

Thanks in advance,
RickTJ
0
 
LVL 6

Expert Comment

by:aescnt
ID: 18028686
Get rid of the line "var xmlHttp", and then after the line "document.getElementById("txtHint").innerHTML=xmlHttp.responseText", add the line "delete xmlHttp". That should do it. Try alert(xmlHttp); after it's been deleted -- it should give an error (i.e., "xmlHttp is not defined")

Reference:
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Operators:Special_Operators:delete_Operator
0
 

Author Comment

by:RickTJ
ID: 18029589
Thanks, aescnt.

Do you think that deleting the xmlHttp variable (on the client side) will kill that IEXPLORE.EXE process on the server?

Has anybody experienced/heard of this issue before (the IEXPLORE.EXE process hogging memory) as a result of creating the ActiveXObject through Javascript?

Thanks
Rick
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 750 total points
ID: 18030285
Hi,

try this...and call this function....i hope it might help you

function clearIt()
{
// Make all u r variable to null
v1=null
v2=null
alert("collecting")
CollectGarbage()
}

CollectGarbage() is IE method of the window object. It deletes all objects from memory which aren't assigned to some variable.

R.K
0
 

Author Comment

by:RickTJ
ID: 18031411
Thank you R.K., should I call this function right after the "document.getElementById("txtHint").innerHTML=xmlHttp.responseText" line?  Also would I replace the variable names accordingly?

function clearIt()
{
// Make all u r variable to null
url=null
xmlHttp=null
objXMLHttp=null
alert("collecting")
CollectGarbage()
}

Please let me know if this would cover everything.  Thanks!!
0
 
LVL 6

Accepted Solution

by:
aescnt earned 750 total points
ID: 18033737
Here's something related to your question: (about CollectGarbage() and XMLHTTPRequest)
http://www.thescripts.com/forum/thread92656.html

I haven't used CollectGarbage() before, but I guess you can put "xmlHttp = null; CollectGarbage();" at the point that you mentioned.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question