闭包+ XMLHttpRequest +内存泄漏 [英] Closures + XMLHttpRequest + Memory Leak

查看:114
本文介绍了闭包+ XMLHttpRequest +内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道IE /闭包的循环引用内存泄漏问题。

我不确定在这种情况下如何解决它。此外,Firefox

似乎使用相同的代码增加其内存大小。所以我想知道我是否错过了某些东西?


我的测试代码如下:


函数myObj(){

var req = new Object();

req.temp = 0;

if(window。 XMLHttpRequest){req.xmlHttpRequest = new XMLHttpRequest(); }

else if(window.ActiveXObject){req.xmlHttpRequest = new

ActiveXObject(" Msxml2.XMLHTTP"); }

req.xmlHttpRequest.onreadystatechange =

function(){

if(req.readyState == 4){

req.temp = req.xmlHttpRequest.responseText;

}

};

req.xmlHttpRequest.open(" GET" ,/,真实;

req.xmlHttpRequest.send(null);

return req;

}

//创建一大堆这些对象来检查内存泄漏

for(var i = 0; i< 1000; i ++){

var x = new myObj();

}


在这个例子中避免内存泄漏的最佳方法是什么?


-

Matt Kruse
http:// www .javascriptToolbox.com

I''m aware of the circular reference memory leak problem with IE/closures.
I''m not sure exactly how to resolve it in this situation. Also, Firefox
appears to grow its memory size with the same code. So I''m wondering if I''m
missing something?

My test code is as follows:

function myObj() {
var req = new Object();
req.temp = 0;
if (window.XMLHttpRequest) { req.xmlHttpRequest = new XMLHttpRequest(); }
else if (window.ActiveXObject) { req.xmlHttpRequest = new
ActiveXObject("Msxml2.XMLHTTP"); }
req.xmlHttpRequest.onreadystatechange =
function() {
if (req.readyState==4) {
req.temp = req.xmlHttpRequest.responseText;
}
};
req.xmlHttpRequest.open("GET","/",true);
req.xmlHttpRequest.send(null);
return req;
}
// Create a whole bunch of these objects to check for memory leak
for (var i=0; i<1000; i++) {
var x = new myObj();
}

What is the best way to avoid memory leaking in this example?

--
Matt Kruse
http://www.JavascriptToolbox.com

推荐答案

Matt Kruse写道:
Matt Kruse wrote:
我知道循环参考IE /闭包的内存泄漏问题。
我不确定如何在这种情况下解决它离子。此外,Firefox
似乎使用相同的代码增加其内存大小。所以我想知道我是否遗失了什么?

我的测试代码如下:

函数myObj(){
var req = new Object();
req.temp = 0;
if(window.XMLHttpRequest){req.xmlHttpRequest = new XMLHttpRequest(); }
else if(window.ActiveXObject){req.xmlHttpRequest = new
ActiveXObject(" Msxml2.XMLHTTP"); }
req.xmlHttpRequest.onreadystatechange =
function(){
if(req.readyState == 4){
req.temp = req.xmlHttpRequest.responseText;
}
};
req.xmlHttpRequest.open(" GET"," /",true);
req.xmlHttpRequest.send(null);
返回req;
}
//创建一大堆这些对象来检查内存泄漏
for(var i = 0; i< 1000; i ++){
var x =新的myObj();
}

在这个例子中避免内存泄漏的最佳方法是什么?

-
Matt Kruse http://www.JavascriptToolbox.com


不确定这是否能解决您的问题,但我注意到了一些浪费了

资源和不清楚的代码。


首先,当你说:function myObj (){
var req = new Object();


实际上,每次调用new myObj都会实例化两个对象。


其次:req.xmlHttpRequest.onreadystatechange =
函数(){
if(req.readyState == 4){
req.temp = req.xmlHttpRequest.responseText;
}
};
I''m aware of the circular reference memory leak problem with IE/closures.
I''m not sure exactly how to resolve it in this situation. Also, Firefox
appears to grow its memory size with the same code. So I''m wondering if I''m
missing something?

My test code is as follows:

function myObj() {
var req = new Object();
req.temp = 0;
if (window.XMLHttpRequest) { req.xmlHttpRequest = new XMLHttpRequest(); }
else if (window.ActiveXObject) { req.xmlHttpRequest = new
ActiveXObject("Msxml2.XMLHTTP"); }
req.xmlHttpRequest.onreadystatechange =
function() {
if (req.readyState==4) {
req.temp = req.xmlHttpRequest.responseText;
}
};
req.xmlHttpRequest.open("GET","/",true);
req.xmlHttpRequest.send(null);
return req;
}
// Create a whole bunch of these objects to check for memory leak
for (var i=0; i<1000; i++) {
var x = new myObj();
}

What is the best way to avoid memory leaking in this example?

--
Matt Kruse
http://www.JavascriptToolbox.com

Not sure if this will solve your problem, but I noticed a few wasted
resources and unclear code.

First, when you say: function myObj() {
var req = new Object();
You are actually instantiating two objects for every call to new myObj.

Second: req.xmlHttpRequest.onreadystatechange =
function() {
if (req.readyState==4) {
req.temp = req.xmlHttpRequest.responseText;
}
};




对于匿名函数中的所有内容,req应该是''this''。


这就是我要改写它的方式:

函数makeXMLRequest(URI){

var x =(

window.XMLHttpRequest?

(new XMLHttpRequest())

:(新的ActiveXObject(''Msxml2.XMLHTTP''))

);


x.open(''GET' ',encodeURI(URI ||''about :blank''),true);

x.send(null);


返回x;

}

现在调用x = makeXMLRequest(不是新的makeXMLRequest)将返回

XMLHttpRequest的实例或者一个典型的新ActiveXObject e $>
''Msxml2.XMLHTTP''。


这样可以带来更高效的垃圾收集。


真正的问题(我认为)是你正在创建所有这些

对象,而不一定给他们时间来完成HTTP

请求。这可能会阻止必要的垃圾回收

并创建1000个同时的HTTP请求。哎哟。


希望有所帮助。



For everything within the anonymous function, req should be ''this''.

This is how I would I would rewrite this:
function makeXMLRequest( URI ) {
var x = (
window.XMLHttpRequest ?
( new XMLHttpRequest() )
: ( new ActiveXObject( ''Msxml2.XMLHTTP'' ) )
);

x.open( ''GET'', encodeURI( URI || ''about:blank'' ), true );
x.send( null );

return x;
}
Now a call to x = makeXMLRequest (NOT new makeXMLRequest) will return
an instance of either XMLHttpRequest OR a new ActiveXObject of type
''Msxml2.XMLHTTP''.

This should result in more efficient garbage collection as well.

The real problem (I think) is that you are creating all of these
objects and not necessarily giving them time to complete the HTTP
request. This is probably preventing the necessary garbage collection
and creating 1000 simultaneous HTTP requests. Ouch.

Hope that helps.


Random写道:
首先,当你说:
function myObj(){
var req = new Object();实际上,每次调用new
myObj都会实例化两个对象。
function myObj() {
var req = new Object(); You are actually instantiating two objects for every call to new
myObj.




这是我的实例的简化示例,需要这样做。但

那是'无关的......

这就是我要改写的方式:


嗯,那个不是完全不同的东西。你不能通过重写和删除功能来解决

问题,对吗? :)

现在调用x = makeXMLRequest(不是新的makeXMLRequest)将返回
XMLHttpRequest的实例或者类型为
''Msxml2.XMLHTTP'的新ActiveXObject' 。
这也可以带来更有效的垃圾收集。



This is a simplified example of my real case, which needs to do this. But
that''s unrelated...
This is how I would I would rewrite this:
Well, that doesn''t something entirely different. You can''t really solve a
problem by rewriting it and removing functionality, can you? :)
Now a call to x = makeXMLRequest (NOT new makeXMLRequest) will return
an instance of either XMLHttpRequest OR a new ActiveXObject of type
''Msxml2.XMLHTTP''.
This should result in more efficient garbage collection as well.




那是因为你完全删除了处理功能! />

-

Matt Kruse
http://www.javascriptToolbox.com


Matt Kruse写道:
Matt Kruse wrote:
if(req.readyState == 4 ){
if (req.readyState==4) {




糟糕,这实际上是:


if(req.xmlHttpRequest.readyState == 4){


当然:)


-

Matt Kruse
http://www.JavascriptToolbox.com


这篇关于闭包+ XMLHttpRequest +内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆