• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 376
  • Last Modified:

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

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
RickTJ
Asked:
RickTJ
2 Solutions
 
jay_gadhaviCommented:
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
 
RickTJAuthor Commented:
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
 
aescntCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RickTJAuthor Commented:
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
 
aescntCommented:
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
 
RickTJAuthor Commented:
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
 
rama_krishna580Commented:
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
 
RickTJAuthor Commented:
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
 
aescntCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now