Javascript responseXML memory leak
I've been tracking down some memory leaks in a Javascript file I've been working with, and I've found one of the last reamining culprits. When I get the responseXML from an XMLHttpRequest, I don't think the response ever gets deleted. I don't know how to delete it, I tried removing the child nodes and then setting it to null (Hoping garbage collection would work) and I also tried using the delete key word. Neither of these things seemed to help. Below is some of the offending code (I omitted some timeout stuff that removes the request itself, but that is not what's causing the memory 开发者_JAVA百科leak).
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = onReadyStateChange1;
request.send();
}
function onReadyStateChange1()
{
if (4 == request.readyState)
{
if (request.status == 200)
{
request.responseXML; //It leaks even if I just do this.
//me.ParseData(request.responseXML);
me.disconnected = 0;
} else
{
me.disconnected += 1;
}
request.onreadystatechange = noop; //noop is an empty function
request = null;
me = null;
}
}
As you can see in the code, I don't even call ParseData, I just put request.responseXML;
and it still leaks. If I comment that line out as well, though, the leak is gone. Meaning the leak is not in the ParseData function or elsewhere, it is the responseXML. I believe it has to do with the tree structure of responseXML, the DOM tree is probably not being cleaned properly. If anyone could help me that would be great.
The responseXML that IE returns is actually MSXML DOM instead of MSHTML DOM, so that developers can use selectNodes and selectSingleNode functions. MSXML DOM has its own garbage collection mechanism, so that you'll not notice memory free right after the DOM is out of scope. Therefore, I would say that this is probably not a true memory leak but a delay free.
If you put the response handler as an anonymous function sharing the same scope like:
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.onreadystatechange = function(){
if (4 == request.readyState){
...
}
}
Does it leak too?
精彩评论