【问题标题】:Memory-leak at a wrapped XMLHttpRequest function包装的 XMLHttpRequest 函数的内存泄漏
【发布时间】:2011-11-20 03:35:28
【问题描述】:

我写了以下内容:

function ao(){
this.count=0;
this.flag=0;
this.tmr=0;
var self = this;
this.make=function(){
    //log("before: "+this.url+" "+this.xhr);
    self.xhr = (window.XMLHttpRequest)
        ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    //log("after: "+this.xhr);
}
this.request = function (method, url, sendStr, delay){
    this.delay=delay;
    if(delay && self.tmr==0){
        self.start();
    }
    if(self.flag==0){
        this.method = method;
        this.url = url;
        this.sendStr = sendStr;
        self.make();
        this.xhr.open(method, url, true);
        this.xhr.onreadystatechange = this.stateChange;
        this.xhr.onabort=this.rrr;
        this.xhr.onerror=this.rrr;
        this.xhr.setRequestHeader("Cache-Control","no-cache");
        this.xhr.send(sendStr);
    }
};
this.repeat=function(){
    if(this.flag==0){
        this.flag=1;
        this.count++;
        this.xhr.open(self.method, self.url+"?"+this.count, true);

        this.xhr.onreadystatechange = this.stateChange;
        this.xhr.onabort=this.rrr;
        this.xhr.onerror=this.rrr;
        this.xhr.setRequestHeader("Cache-Control","no-cache");

        this.xhr.send(self.sendStr);
    }
    return 0;
}
this.stop=function(){
    window.clearInterval(this.tmr);
    this.tmr=0;
    this.flag=0;
}
this.start =function(){
    self.tmr=window.setInterval(function(){self.repeat();},self.delay);
}
this.stateChange = function(){
    if (self.xhr.readyState <= 1){
        return;
        self.log("404 errors");
    } else {
        if (self.xhr.readyState == 4 && self.xhr.status == 200){
            self.resp = self.xhr.responseText;
            if (self.callback != null)
                self.callback(self.xhr.readyState, self.xhr.status);
            else {
                if (self.getHTML) {
                    self.getHTML(self.resp);
                    this.xhr=null;
                } else {
                    if (self.xhr.readyState == 4 && self.xhr.status == 200){
                        self.parseJSON();
                        self.traverse();
                        this.ro=null;
                        this.xhr=null;
                    }
                }
            }
        }
    }
    self.flag=0;
    return 0;
};

在 windows ff 中存在内存泄漏。我花了几天时间试图修复它,但我很难过。

以下作品:

var x=new ao();
ao.request("POST","/cgi-bin/sdf.cgi","text",1000)

如果先前的请求完成,则每 1000 毫秒后,它会发出新的请求。

【问题讨论】:

  • Firefox 内存泄漏
  • 你是如何确定它泄漏内存的?
  • self.callback/self.getHtml/self.parseJSON/self.traverse 方法中发生了什么
  • 我用windows任务管理器确定内存使用情况
  • @Arun P Johny 这些方法不包含内存泄漏。内存泄漏在 http 请求中。

标签: javascript memory-leaks xmlhttprequest


【解决方案1】:

开发者在使用 XMLHttpRequest 对象的 onreadystatechanged 事件。如果处理程序 是关闭对同一 XMLHttpRequest 的引用的闭包 对象,可以创建另一个循环依赖。这不是 必须由上述工具检测到,因为对象不是部分 的DOM。 Link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2012-04-06
    • 2015-02-17
    • 1970-01-01
    • 2021-03-30
    • 2017-07-25
    • 2018-03-03
    相关资源
    最近更新 更多