使用XMLHtt prequest自动网页刷新内存泄漏 [英] Automatic web-page refresh memory leak using XMLHttpRequest

查看:135
本文介绍了使用XMLHtt prequest自动网页刷新内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,
我一直工作在一个网络接口,使用8位微控制器的一些硬件。该网页使用HTML,JavaScript的,JSON和XHR(XMLHtt prequest)为其通信。我想要做的就是创建一个页面,更新每隔250毫秒与使用的setInterval使网页被更新实时,使其感觉更像是一个应用程序给用户控制器新值。

Greetings,
I've been working on a web-interface for some hardware that uses an 8-bit microcontroller. The web page uses HTML, javascript, JSON and XHR (XMLHttpRequest) for its communications. What I'm trying to do is create a page that updates every 250mS with new values from the controller using setInterval so that the web page gets updated 'real-time' to make it feel more like an application to the user.

我已经得到它的工作在大多数情况下,却发现有某处的code内存泄漏与我测试过,IE和Chrome两种浏览器。

I've gotten it to work for the most part, but found that there's a memory leak somewhere in the code with both browsers that I've tested, IE and Chrome.

我已经研究了这个网上,它似乎像其他人有同样的问题,我试图实现,但没有成功不同的修复。

I've researched this online and it seems like other people have had the same problem and I've tried to implement different fixes with no success.

下面是几个单元的镜头code,希望能解释的东西更好一点,我已经修改变量,所以他们更有意义,没有看到完整的应用程序。

Here are a few snap-shots of code that will hopefully explain things a little better, I've modified variables so they make more sense without seeing the full application.

// start the pageRefreshTimer to update values
var pageRefreshTimer = window.setInterval(updateValues, 250);

// Standard XHR opener
HTTP.getText = function(url, callback) {
    var request = HTTP.newRequest(); // Searches array of standard XMLHttpRequest functions to try, code not shown...
    request.onreadystatechange = function () {
        if (request.readyState == 4 && request.status == 200) {
            callback(request.responseText) // responseText becomes JSONText below
        }
    }
    request.open("GET", url);
    request.send(null);
}

// Function that is constantly refreshed by HTML page to simulate real-time application
updateValues = function(parameter, value) {

    newURL = newURL + "?" + parameter; // newURL is defined elsewhere in the code...

    // Send the url and create the JSONObject
    HTTP.getText(newURL, function(JSONText) {
                    var JSONObject = eval('(' + JSONText + ')'); // Specific notation for JSON

                    // Load object values into Javascript variables
                    Controller.detectorPosition = JSONObject.detectorPosition;
                    Controller.offset = JSONObject.offset;
                    Controller.actuatorPosition = JSONObject.actuatorPosition;
    });

    delete JSONObject; // My attempt at manual garbage collection, didn't resolve the memory leak
}

作为参考,这将是从微控制器发送到浏览器会是这个样子的JSON文件...

For your reference, the JSON file that would be sent from the microcontroller to the browser would look something like this...

{ "offset": "1500", 
"detectorPosition": "1558", 
"actuatorPosition": "120" }

这看起来像一个问题,在code倒闭潮?

Does this look like an issue with "closures" in the code?

使用开发工具在Chrome(按Ctrl-Shift键-J),我注意到,有多个调用ParameterValues​​.json文件(350B大小)像它应该,因为这从微控制器存储的值,这是JSON对象;但浏览器以某种方式存储/内存中缓存的每一页?

Using the Developer Tools in Chrome (Ctrl-Shift-J), I noticed that there are multiple calls to the ParameterValues.json file (350B size) like it should since this is the JSON object that stores the values from the microcontroller; but is the browser somehow storing/caching every page in memory?

附加在我的意见是问题的两个屏幕截图。第二个是我设置一个断点在XMLHtt prequest循环,它看起来像有在右手边的封闭面板中的循环引用。任何人看到一个问题与此?

Attached in my comments are two screen shots of the issue. The second one is where I setup a break-point in the XMLHttpRequest loop, and it looks like there's a circular reference in the "closure" panel on the right hand side. Anyone see an issue with this?

我能做些什么,以深入挖掘,并获得更多的信息?

What can I do to dig deeper and get more information?

在此先感谢!

推荐答案

目前已经建立了一个跨浏览器实现的 XMLHtt prequest 。他们还保持本地XMLHtt prequest错误的这可能是对你有用。

There is a Google Code project that has created a cross-browser implementation of XMLHttpRequest. They also maintain a small list of native XMLHttpRequest bugs that might be useful to you.

下面的错误似乎可能适用于您的情况:

The following bug seems potentially applicable to your situation:

错误:对XMLHtt prequest实例不会被垃圾回收的情况下,   你有一个参考的实例   或一个其他[原文如此] COM对象(为   例如:DOM节点等)在其   onreadystatechange的处理程序,从而   生产运行时内存泄漏。

Bug: The instance of XMLHttpRequest doesn't get garbage collected in case you have a reference to the instance or to an other [sic] COM object (for example: DOM Node etc.) in its onreadystatechange handler, thus producing runtime memory leaks.

这篇关于使用XMLHtt prequest自动网页刷新内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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